I have multiple checkbox in a form, such as:
- Car
- Bike
- Plane
- Boat
- Walk
User can choose one, two, three, or all of it. At the first time, I tried to choosed just one or two option, when I processed the script there's error statement -> "Undefined index for car", "Undefined index for boat" for option I didn't checked. So I was googled and found the answer with this script: (For the insert script)
$car = (isset($_POST['car']))?$_POST['car']:NULL;
$bike = (isset($_POST['bike']))?$_POST['bike']:NULL;
$plane = (isset($_POST['plane']))?$_POST['plane']:NULL;
$boat = (isset($_POST['boat']))?$_POST['boat']:NULL;
$walk = (isset($_POST['walk']))?$_POST['walk']:NULL;
and it was worked perfectly, but then when I want to update my form with this script: (Update/Edit Form)
if(isset($_POST['save'])) {
include('connection.php');
$car = (isset($_POST['car']))?$_POST['car']:NULL;
$bike = (isset($_POST['bike']))?$_POST['bike']:NULL;
$plane = (isset($_POST['plane']))?$_POST['plane']:NULL;
$boat = (isset($_POST['boat']))?$_POST['boat']:NULL;
$walk = (isset($_POST['walk']))?$_POST['walk']:NULL;
$update = mysql_query("UPDATE information SET(
car = (isset($_POST['car']))?$_POST['car']:NULL;
bike = (isset($_POST['bike']))?$_POST['bike']:NULL;
plane = (isset($_POST['plane']))?$_POST['plane']:NULL;
boat = (isset($_POST['boat']))?$_POST['boat']:NULL;
walk = (isset($_POST['walk']))?$_POST['walk']:NULL;
)")
and there's an error, with the quotation marked, so I've changed it into this script: (Fixing the quotation marked)
if(isset('$_POST[save]')) {
include('connection.php');
$car = (isset('$_POST[car]'))?'$_POST[car]':NULL;
$bike = (isset('$_POST[bike]'))?'$_POST[bike]':NULL;
$plane = (isset('$_POST[plane]'))?'$_POST[plane]':NULL;
$boat = (isset('$_POST[boat]'))?'$_POST[boat]':NULL;
$walk = (isset('$_POST[walk]'))?'$_POST[walk]':NULL;
$update = mysql_query("UPDATE information SET(
car = (isset('$_POST[car]'))?'$_POST[car]':NULL;
bike = (isset('$_POST[bike]'))?'$_POST[bike]':NULL;
plane = (isset('$_POST[plane]'))?'$_POST[plane]':NULL;
boat = (isset('$_POST[boat]'))?'$_POST[boat]':NULL;
walk = (isset('$_POST[walk]'))?'$_POST[walk]':NULL;
)")
if($update){
echo 'Update Success! ';
echo '<a href="index.php">Results</a>';
}else{
echo 'Failed To Update! ';
echo '<a href="index.php">Home</a>';
}
}else{
echo '<script>window.history.back()</script>';
Still got error's "Undefined index" but then I was found a solution from stackoverflow also, like this:
function checkbox_value($name) {
return (isset($_POST[$name]) ? 1 : 0);
}
$sql = 'UPDATE table SET '.
'checkbox1 = '. checkbox_value('checkbox1') .','.
'checkbox2 = '. checkbox_value('checkbox2') .','.
'checkbox3 = '. checkbox_value('checkbox3') .','.
'checkbox4 = '. checkbox_value('checkbox4') .','.
'checkbox5 = '. checkbox_value('checkbox5') .','. "LIMIT 1";
I've made changes into this/like this:
function checkbox_value($update) {
return (isset($_POST[$update]) ? 1 : 0);
}
$update = 'UPDATE information SET '.
'car = '. checkbox_value('car') .','.
'bike = '. checkbox_value('bike') .','.
'plane = '. checkbox_value('plane') .','.
'boat = '. checkbox_value('boat') .','.
'walk = '. checkbox_value('walk') .','. "LIMIT 1";
The "Undefined index" was gone, but Failed to update the data. Little bit confusing here...anyone can give me a suggestion please. Thank you very much for the trouble.
Aucun commentaire:
Enregistrer un commentaire