How can I insert a drop-down value into a DB? I have a form that selects a list of people. And with them i have their unique id showing. So populating isn't he problem, but inserting is... any ideas?
Code:
<?php
$q=$_GET["q"];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("ajax_demo", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_f开发者_C百科etch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<option value="1">John Doe</option>
You get the 1
via post, I don't see where's the problem with inserting that into the database.
Update after adding code to the question:
Please show how you generate the options, this code seems ok. One thing though: you should escape $q
before passing it to the query like this: mysql_real_escape_string($q)
. It returns a string, so you can just concatenate it to the query.
Read about SQL injection attacks.
精彩评论