0

i am dynamically adding the values in the dropdown . After selecting the value in the dropdown and entering text in the textbox, both textbox value and dropdown value has to be entered in the database on button click. But my code is not working properly ie only textbox value is inserted in the database but not dropdown selected vlaue. Please correct my code

  <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>';

                  }
               }
           }
      }
}

    ?>
1
  • 1
    Welcome. Your <option>s need a value: <option value=\"\">. Right now they all have an empty value, returning nothing. Commented Oct 2, 2018 at 7:19

1 Answer 1

1

The line:

echo "<option value=\"\">" . $row['topic'] . "</option>";

change it to:

echo "<option value='".$row['topic']."'>" . $row['topic'] . "</option>";

You are passing empty values for the topic parameter. Setting up a correct value will ensure that you are inserting the correct user input in the database. For future you can debug this easy by outputting your POST content before executing the SQLinsert.

That will fix your issue, but there are several other things you might consider changing, one of them is the deprecated mysql_ extension, switch to mysqli or PDO.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.