问题处理PHP复选框,代码与INSERT一起使用,但在使用UPDATE时出现语法错误

时间:2014-07-11 18:55:12

标签: php mysql

提前感谢您的耐心和帮助。编写代码的新手,编写了以下代码:

$host="localhost";
$username="#####";
$password="#######";
$db_name="signuplist";
$tbl_name="signupbydate";
$myusername=$_SESSION['logname'];

$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;  }
    } // end of foreach.

    // MySQL statement. Don't forget to strip to avoid sql injection 

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

         // MySQL statement to execute the UPDATE statement above.           
          mysqli_query($cxn, $sql1) or die('<br/>Error reading database: '.mysqli_error($cxn));
          mysqli_close($cxn);
}  // End of, if statement from the button check
?>

<html>
    <head>
        <title>Checkbox</title>
    <head>
         <title>HTML Checkbox</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">
         </form>
    </body>
</html>

我最初使用INSERT而不是UPDATE并且没有收到任何错误消息,但现在使用UPDATE行我收到此错误消息,我已经尝试了大约一天来解决:

解析错误:语法错误,第55行的C:\ xampp \ htdocs \ requesttime.php中的意外“请求时间”(T_STRING)

数据库中的'username'字段为VARCHAR(25),'playdate'字段为ENUM(1),值为'0'或'1',默认值为'0',如果有的话轴承

我知道有更有效的方法可以做到这一点,也可以处理输入,但我很享受学习过程,所以下次会到那里 谢谢!

1 个答案:

答案 0 :(得分:0)

更改

WHERE username=$myusername;

WHERE username=$myusername";