使用PHP选择框选择具有特定数值的SQL字段

时间:2014-08-01 20:09:36

标签: php mysql sql

在此先感谢您的帮助,我仍然是编写代码的新手。我正在尝试使用选择列表来选择特定日期,并从该日期打印已注册人员的名称列表。注册时数据库的值为1,如果未注册则为零。在尝试处理输入日期和在数据库中使用适当值选择名称时出现了困难。感谢您的任何反馈,并提前为任何明显的错误道歉!

<form name="whoplaying" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <select name="playdate">
       <option value="playdate1"> September 3, 2014 </option>
       <option value="playdate2"> September 6, 2014 </option>
       <option value="playdate3"> September 10, 2014 </option>
       <option value="playdate4"> September 13, 2014 </option>
       <option value="playdate5"> September 17, 2014 </option>
    </select>
       <input type="submit" value="Select Playdate">

<?php

$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");

     if(isset($_POST['submit']))
     {   
         $playdate = $_POST['playdate'];

         $stmt = mysqli_stmt_init($cxn);
           if (mysqli_stmt_prepare($stmt, 'SELECT username FROM $tbl_name WHERE $playdate=?')) {

           /* bind parameters for markers */
           mysqli_stmt_bind_param($stmt, "s", $playdate);

           /* execute query */
           mysqli_stmt_execute($stmt);

           /* bind result variables */
           $result=mysqli_stmt_bind_result($stmt, $playdate);

           /* fetch value */
              while   ($row=mysqli_stmt_fetch($result));
               {                       
                 echo "<table border='1'>
             <table class='center'>
         <tr>
             <th><div style=width:100px>Username</div></th>
                 </tr>";

             extract ($row);
             echo"<tr>\n
             <td>$username</td>
             </tr>\n";
              }
           echo "</table>"; 

           mysqli_stmt_close($stmt);  }    

     mysqli_close($cxn);

    }
?>

2 个答案:

答案 0 :(得分:1)

您的表单定义错误。它应该是

<select name="playdate">
   <option value="Sep 3, 2014">Sep 3, 2014</option>
   <option value="Sep 6, 2014">Sep 6, 2014</option>
   etc...
</select>

$playdate = $_POST['playdate']; // $playdate becomes "Sep 3, 2014"

另外,您很容易受到SQL injection attacks的攻击,因此请停止使用此代码,直到您已经学会了如何避免这种情况。

答案 1 :(得分:0)

现在这似乎工作正常,感谢您不断尝试的灵感!

<form name="whoplaying" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <select name="playdate">
       <option value="playdate1" > September 3, 2014 </option>
       <option value="playdate2" > September 6, 2014 </option>
       <option value="playdate3" > September 10, 2014 </option>
       <option value="playdate4" > September 13, 2014 </option>
       <option value="playdate5" > September 17, 2014 </option>
    </select>           
       <input type="submit" value="Update" name="submit">

<?php

$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");


 if(isset($_POST['submit']))
 {   
     $playdate = $_POST['playdate'];
     $value='1';
         $query = "select username from $tbl_name where $playdate = ?";
         $stmt = $cxn->prepare($query);
         $stmt->bind_param('s', $value);
         $stmt->execute();
         $result = $stmt->get_result();                                      
          echo "<table border='1'>
      <table class='center'>
    <tr>
        <th><div style=width:100px>Firstname</div></th>
        <th><div style=width:100px>Lastname</div></th>
    </tr>";
while ($row=mysqli_fetch_assoc($result))
{
extract ($row);
echo"<tr>\n
     <td>$username</td>
     <td>$username</td>
     </tr>\n";
}
echo "</table>";              

 mysqli_close($cxn);

}
?>