使用PHP PDO不更新所有复选框更新复选框的数据库值 [英] Update database values for checkboxes using PHP PDO not updating all checkboxes
本文介绍了使用PHP PDO不更新所有复选框更新复选框的数据库值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有3行,每行有一个复选框,当我取消选中所有框,然后单击保存我收到错误在我的else语句。如果我一次取消选中框并点击保存,当我到达最后一个复选框,然后点击保存我也收到错误的else语句。
I have 3 rows and each row has a checkbox, when I uncheck all boxes and click save I receive the error in my else statement. If I uncheck on box at a time and click save, when I get to the last checkbox and click save I also receive the error in the else statement.
这里是我的
这是我的PHP代码:
//Update Social Preferences with new value for display on homepage
$settingsArray = array('myfacebook', 'mytwitter', 'mygoogle');
if(isset($_POST['btn-save']))
{
if(isset( $_POST['mysocial']))
{
$values = array();
foreach($_POST['mysocial'] as $selection )
{ if(in_array($selection, $settingsArray))
{ $values[ $selection ] = 1; }
else
{ $values[ $selection ] = 0; }
} // end of foreach.
$user_id = $_SESSION['user']['id'];
try // save user selection to the database
{
$stmt = $db->prepare("UPDATE social_preferences SET my_facebook = :myfacebook, my_twitter = :mytwitter, my_google = :mygoogle WHERE user_id = :userID");
$stmt->bindParam(":userID", $userid, PDO::PARAM_INT);
$stmt->bindParam(':myfacebook', $values['myfacebook']);
$stmt->bindParam(':mytwitter', $values['mytwitter']);
$stmt->bindParam(':mygoogle', $values['mygoogle']);
$stmt->execute();
header("Location: admin-social-test.php");
die("Redirecting to admin-social-test.php");
} catch(PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
}
else
{ $noCheckbox = 'No checkbox selection made...'; }
} // End of, if statement from the button check
这是我的HTML代码:
Here is my HTML Code:
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
推荐答案
所以我改变了这一点:
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
到此:
<input type="hidden" name="mysocial[]" value="myfacebook1" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="myfacebook" <?php echo ($result['my_facebook']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mytwitter1" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mytwitter" <?php echo ($result['my_twitter']==1 ? 'checked' : '');?> />
<input type="hidden" name="mysocial[]" value="mygoogle1" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
<input type="checkbox" name="mysocial[]" value="mygoogle" <?php echo ($result['my_google']==1 ? 'checked' : '');?> />
这似乎已经奏效了。
这篇关于使用PHP PDO不更新所有复选框更新复选框的数据库值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文