first off I am a rookie so please forgive my basic question. I am trying to use an html checkbox to let me know when to check a serial number. The problem is no matter what i do I get an error telling me “Notice: Undefined index: sncheck in XYZ”.
I tried using a ternary operator to set the value as it was posted (If the checkbox was not checked) but it still fails on me. Any suggestions would be
Here is the code, below im using a checkbox with the name sncheck and a value of ok. thanks!
if (isset($_POST['submit'])){
$gov = mysqli_real_escape_string( $dbc, trim($_POST['gov']));
$pctype = mysqli_real_escape_string( $dbc, trim($_POST['pctype']));
$compname = mysqli_real_escape_string( $dbc, trim($_POST['compname']));
$username = mysqli_real_escape_string( $dbc, trim($_POST['username']));
$compmodel = mysqli_real_escape_string( $dbc, trim($_POST['compmodel']));
$serialnumber = mysqli_real_escape_string( $dbc, trim($_POST['serialnumber']));
$sncheck = ($_POST['sncheck']== 'ok') ? $_POST['sncheck'] : '';
$purchdate = mysqli_real_escape_string( $dbc, trim($_POST['purchasedate']));
$os = mysqli_real_escape_string( $dbc, trim($_POST['os']));
$memory = mysqli_real_escape_string( $dbc, trim($_POST['memory']));
$monitor1 = mysqli_real_escape_string( $dbc, trim($_POST['monitor1']));
$monitor2 = mysqli_real_escape_string( $dbc, trim($_POST['monitor2']));
$warranty = mysqli_real_escape_string( $dbc, trim($_POST['warranty']));
$warrantyend = mysqli_real_escape_string( $dbc, trim($_POST['warrantyend']));
$status = mysqli_real_escape_string( $dbc, trim($_POST['status']));
$notes = mysqli_real_escape_string( $dbc, trim($_POST['notes']));
if (dateformat($purchdate) == 1 && $sncheck == "ok"){
$querysn = "SELECT serialnumber FROM inventory" .
" WHERE serialnumber = '" . $serialnumber . "'";
$data = mysqli_query($dbc, $querysn) or die('Error querying the Database for Serial Numbers');
if (mysqli_num_rows($data) == 0){
$queryupdate = "UPDATE inventory SET government = '$gov', pctype = '$pctype', compname = '$compname'," .
" username = '$username', compmodel = '$compmodel', serialnumber = '$serialnumber', purchased = '$purchdate'," .
" operatingsystem = '$os', `memory` = '$memory', monitor1 = '$monitor1'," .
" monitor2 = '$monitor2', warranty = '$warranty', warrantyend = '$warrantyend', `status` = '$status', notes = '$notes'" .
" WHERE serialnumber = '" . $_POST['serialnumber'] . "'";
mysqli_query($dbc, $queryupdate);
echo 'PC was sucessfully saved';
}
else{
echo '<p class="error">The serial number has already been used, please try another</p>';
}
}
else{
echo '<p class="error">The date format does not match</p>';
}
if (dateformat($purchdate) == 1){
$querysn = "SELECT serialnumber FROM inventory" .
" WHERE serialnumber = '" . $serialnumber . "'";
$data = mysqli_query($dbc, $querysn) or die('Error querying the Database for Serial Numbers');
if (mysqli_num_rows($data) == 0){
$queryupdate = "UPDATE inventory SET government = '$gov', pctype = '$pctype', compname = '$compname'," .
" username = '$username', compmodel = '$compmodel', purchased = '$purchdate'," .
" operatingsystem = '$os', `memory` = '$memory', monitor1 = '$monitor1'," .
" monitor2 = '$monitor2', warranty = '$warranty', warrantyend = '$warrantyend', `status` = '$status', notes = '$notes'" .
" WHERE serialnumber = '" . $_POST['serialnumber'] . "'";
mysqli_query($dbc, $queryupdate);
echo 'PC was sucessfully saved';
}
else{
echo '<p class="error">The serial number has already been used, please try another</p>';
}
}
else{
echo '<p class="error">The date format does not match</p>';
}
}
EDITED ANSWER: Based on your edited question, try changing:
to:
If a checkbox is checked, it will be set in
$_POST. If it’s not checked it will not be.ORIGINAL ANSWER:
It’s tough to answer your question without seeing code, so I’m just stabbing in the dark, but here goes. Here’s an example:
This is a complete guess because I’m not sure what you’re doing. But this will take an array of serial numbers and display a form containing them all with checkboxes next to each one. When the form is submitted, the selected serial numbers will be in
$_REQUEST['serial_numbers'](which will be an array). The script will loop through each member of this array and print those serial numbers back. Not very useful, but again, it’s just an example. Please post some code if you’d like more direction and I’ll edit my answer.