php - How to pass chosen value from drop down list to the database table

776

I have a drop-down list and I want to pass the chosen values such as 'ID', 'input1' to the database. The user chooses one row from the drop-down list and when he clicks submit all values should go to the database table. How to achieve this with POST method?

 <form action="demo2.php" method="post" />
                                <p>
    <select name="command">
    <?php 
    $sql = mysql_query("SELECT ID,input1, input2, input3, input4, input5 FROM demo");
    while ($row = mysql_fetch_array($sql)){
    echo "<option value=\"owner1\">" . $row['ID'] . '.' . $row['input1'] . ' - ' . $row['input2'] . "</option>";}
    ?>

Part of my demo2.php. This is just an example of what I want to achieve.

$value9 = $_POST[$row['ID']];
$sql2 ="INSERT INTO table ID VALUES ('$value9')";
782

Answer

Solution:

First remove / in the opeing of the form,

<form action="demo2.php" method="post">
  <p>
    <select name="command">
      <?php $sql = mysql_query("SELECT ID,input1, input2, input3, input4, input5 FROM demo"); ?>
      <?php while ($row = mysql_fetch_array($sql)): ?>
        <?php echo "<option value=\"owner1\">" . $row['ID'] . '.' . $row['input1'] . ' - ' . $row['input2'] . "</option>"; ?>
      <?php endwhile; ?>
    </select>
  </p>
  <button type="submit" name="button">Submit</button>
</form>

and the add submit button

143

Answer

Solution:

In order to get and pass the value from the options you need to call the name of theselect tag.

$value9 = $_POST['command'];
$sql2 ="INSERT INTO table ID VALUES ('$value9')";

something like that. This might be helpful.

People are also looking for solutions to the problem: php - Can pkg install without dependencies in FreeBSD 10.3?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.