Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中单击按钮时在数据库中插入动态下拉值_Php - Fatal编程技术网

在php中单击按钮时在数据库中插入动态下拉值

在php中单击按钮时在数据库中插入动态下拉值,php,Php,我在下拉列表中动态添加这些值。在下拉列表中选择值并在文本框中输入文本后,必须在单击按钮时在数据库中输入文本框值和下拉列表值。但我的代码工作不正常,即只在数据库中插入文本框值,而不是下拉选择的值。请更正我的密码 <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> <label style="margin-left:260px;width:170px"

我在下拉列表中动态添加这些值。在下拉列表中选择值并在文本框中输入文本后,必须在单击按钮时在数据库中输入文本框值和下拉列表值。但我的代码工作不正常,即只在数据库中插入文本框值,而不是下拉选择的值。请更正我的密码

  <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
    <label style="margin-left:260px;width:170px"><h3 >Topic:</h3><br></label><br>

    <select name="topic" style="margin-left: 282px;margin-top: -17px;">
     <option value=""> -----Select----- </option>
 <?php 
 $records = mysql_query("SELECT topic FROM topic ");
 while ($row = mysql_fetch_array($records)){
 echo "<option value=\"\">" . $row['topic'] . "</option>";
}
?>
</select>

        <label style="margin-left:260px;width:170px"><h3 >Enter Crime Key Point:</h3><br></label><br>
        <textarea name="details" id="details" rows="14" cols="60" style="margin-left:112px"><?php if(isset($_POST['details'])){echo htmlspecialchars($_POST['details']);}?>
        </textarea><br><br>
    <br><br><input type='submit' name='submit' value='Post' style='margin-left:305px;'>


</form>

  if(isset($_POST["submit"]) ){

//if ( isset( $_POST['topic'] )){
    //if(!empty($_POST['CarList'])){
    $story = $_POST['details']; 
                     $topic = $_POST['topic'];

            echo $topic;
        $sql = "SELECT `registered` FROM `user` WHERE `name`='{$_SESSION['name']}'";
    $result = mysql_query($sql);
    if (! $result)
     {
       throw new My_Db_Exception('Database error: ' . mysql_error());
     }
      else
      {
        $row = mysql_fetch_assoc($result); 
        $register=$row['registered'];
         if ($register==1)
          {

            $sql = "INSERT INTO `stories`(`stories`,`name`,`topic`,`date/time`) VALUES ('$story','{$_SESSION['name']}','$topic',now())";
            $result = mysql_query($sql);
            echo $result;
            if (! $result)
             {
               //echo "Story is not inserted. Please avoid special characters.";
               echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
             }
              else
              {
               die("<script>location.href = 'usershome.php'</script>");
               echo "Story inserted successfully"; 
              }

          }
           else
           {
             $sql = "select count(*) from stories where `date/time`=DATE(NOW()) AND `name` = '{$_SESSION['name']}'";
             $result = mysql_query($sql);
             if (! $result)
              {
                throw new My_Db_Exception('Database error: ' . mysql_error());
              }
               else
               {
                 $row = mysql_fetch_assoc($result); 
                 $count=$row['count(*)'];
                 if ($count<3)
                 {
                  $sql = " INSERT INTO `stories`(`stories`,`name`,`date/time`) VALUES ('$story','{$_SESSION['name']}',now())";
                  $result = mysql_query($sql);
                  if (! $result)
                   {
                     echo "Story is not inserted";
                   }
                   else
                   {
                     die("<script>location.href = 'usershome.php'</script>");
                     echo "Story inserted successfully"; 
                   }
                 }
                  else
                  {
                    echo '<img src="images/animated-star-image-0009.gif" width="25px"/><a href="account.php" style="font-size:13px;color: white">Please Register to enter more than 3 stories per day</a>';

                  }
               }
           }
      }
}

    ?>
该行:

echo "<option value=\"\">" . $row['topic'] . "</option>";
将其更改为:

echo "<option value='".$row['topic']."'>" . $row['topic'] . "</option>";
您正在为topic参数传递空值。设置正确的值将确保在数据库中插入正确的用户输入。 将来,您可以通过在执行SQLinsert之前输出POST内容来轻松调试

这将修复你的问题,但是还有其他一些事情你可能会考虑改变,其中之一是不受欢迎的MySQL扩展,切换到MyQuLi或PDO。您的s需要一个值:。现在它们都有一个空值,不返回任何内容。