需要仅更新php复选框中的已检查值

时间:2014-07-29 00:22:36

标签: php mysql

我有以下代码来更新sql数据库,具体取决于选中的值。问题是我只想更新已检查的值并保留其他值,而我当前的代码将所有内容更新为零或一。我已经尝试了许多不同的方法来实现这一目标而没有成也欢迎有关如何使关于空inpput的'else'语句仅在单击Submit而不是一直显示之后出现的建议。编写代码非常新,如果答案显而易见并且我错过了,那么道歉,感谢您的帮助!     

$host="localhost";
$username="######";
$password="#######";
$db_name="signuplist";
$tbl_name="signupbydate";
$myusername=$_SESSION['logname'];
?>
    <head>
        <title>Request Date</title>
    </head>
    <body>
    <div style='margin-left:6.0in; margin-top:0.75in'>
    <p style='font-weight:bold'>
        When would you like to play golf?</p>
    <p>Choose the dates you wish to play</p>
    <p>Make sure you click 'Update' when you are done</p>
           <form name="requesttime" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
           <input type="checkbox" name="playdate[]" value="playdate1"> September 3, 2014 <br>
           <input type="checkbox" name="playdate[]" value="playdate2"> September 6, 2014 <br>
           <input type="checkbox" name="playdate[]" value="playdate3"> September 10, 2014 <br>
           <input type="checkbox" name="playdate[]" value="playdate4"> September 13, 2014 <br>
           <input type="checkbox" name="playdate[]" value="playdate5"> September 17, 2014 <br>
           <br>
           <input type="submit" value="Update" name="submit">
<?php

$cxn=mysqli_connect($host,$username,$password,$db_name)
     or die ("Couldn't Connect to Server");
$chkbox = array('playdate1', 'playdate2', 'playdate3', 'playdate4', 'playdate5');

     if(isset($_POST['submit']))
     {   $playdate = $_POST['playdate'];
         $values = array();
           foreach($chkbox as $selection )
           {     if(in_array($selection, $playdate))
                   { $values [$selection] = 1;  }
                 else
                   {$values [$selection] = 0; } 

           }
      $sql1="UPDATE $tbl_name 
             SET playdate1=$values[playdate1], playdate2=$values[playdate2], playdate3=$values[playdate3], playdate4=$values[playdate4], playdate5=$values[playdate5]
             WHERE username='$myusername'";

      mysqli_query($cxn, $sql1) or die('<br/>Error reading database: '.mysqli_error($cxn));
      mysqli_close($cxn);
    }  

    if(empty($_POST['submit'])) { echo "Make a selection";  exit; }

?>

2 个答案:

答案 0 :(得分:1)

  //This handles the page when the request_method is GET, PUT, DELETE etc.
  if($_SERVER['REQUEST_METHOD'] == 'POST') {   
    //move the exit up so you're not adding in extra logic
    if(empty($_POST['playdate'])) { echo "Make a selection";  exit; }

    $playdate = $_POST['playdate'];

    $chkbox = array('playdate1', 'playdate2', 'playdate3', 'playdate4', 'playdate5');

    $setString = array();
     foreach($chkbox as $selection ) {     
      if(in_array($selection, $playdate)) {
        //we only grab values we want and shove them into a happy array
        $setString[] = $selection . '= 1';   
      } else {
        $setString[] = $selection . '= 0';
      } 
     }
    //if count is 0 this will act as a boolean false
    if(count($setString)) {

      //implode the array to create the correct formatting for the sql query
      $sql1=" UPDATE $tbl_name SET " . implode(',' , $setString) . " WHERE username='$myusername'";   

      //no sense in opening a connection and closing it unless we actually have data we wish to write
      $cxn=mysqli_connect($host,$username,$password,$db_name) or die ("Couldn't Connect to Server");

      mysqli_query($cxn, $sql1) or die('<br/>Error reading database: '.mysqli_error($cxn));

      mysqli_close($cxn);
    }
  }

答案 1 :(得分:1)

您可以构建反映用户当前设置的表单,以便用户可以选中和取消选中值:

<form name="requesttime" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

<?php
$query = "select * from $tbl_name where username = ?";
$st = $cnx->prepare($query);
$st->bind_param('s', $myusername);
$st->execute();
$rs = $st->get_result();
$row = $rs->fetch_assoc();
$chkbox = array(
    'playdate1' => 'September 3, 2014', 
    'playdate2' => 'September 6, 2014', 
    'playdate3' => 'September 10, 2014',
    'playdate4' => 'September 13, 2014',
    'playdate5' => 'September 17, 2014'
);
foreach ($chkbox as $selection => $day) {
    $checked = isset($row[$selection]) && $row[$selection];
    if ($checked) {
        echo '<input type="checkbox" name="playdate[]" value="'.$selection.'" checked> '.$day.' <br>';
    } else {
        echo '<input type="checkbox" name="playdate[]" value="'.$selection.'"> '.$day.' <br>';
    }
}
?>

<br>
<input type="submit" value="Update" name="submit">
</form>

<?
if(isset($_POST['submit'])) {
     $playdate = $_POST['playdate'];
     $values = array();
       foreach($chkbox as $selection => $day)
       {     if(in_array($selection, $playdate))
               { $values[$selection] = 1; }
             else
               { $values[$selection] = 0; } 
       }
  $sql1="UPDATE $tbl_name 
         SET playdate1={$values['playdate1']}, 
             playdate2={$values['playdate2']}, 
             playdate3={$values['playdate3']},
             playdate4={$values['playdate4']},
             playdate5={$values['playdate5']}
         WHERE username='$myusername'";

  mysqli_query($cxn, $sql1) or die('<br/>Error reading database: '.mysqli_error($cxn));
  mysqli_close($cxn);
}  

if(empty($_POST['submit'])) { echo "Make a selection";  exit; }