i have a small program that has a drop down list with car names in it depending on which drop down item is picked i want to store its value in a data base. I think i have the general idea of how to do this but my code is not storing the value to my database.
my database has three fields: ‘id'(type int(11)), ‘car_val’ (type int(11)), ‘name’ (type text)
<?php
//////connecting to our sql server
$db_connect=mysql_connect("xxxxx", "xxxxxx", "xxxxxx") or die("not logged in");
//////now selecting our database
$db_select=mysql_select_db("xxxxxx") or die(mysql_error());
////this query is used the first time the page loads
$query = mysql_query("SELECT * FROM car ");
echo '<form action="drop_down_car_test.php?" method="GET">';
echo '<table border = \"1\">';
echo '<tr><td>id</td><td>car</td><td>name</td>';
while($row=mysql_fetch_array($query)){
$currentID=$row['id'];
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo "<select name='carDropDown".$row['id']."' >;
<option value=\"1\">ford</option>;
<option value=\"2\">toyota</option>;
<option value=\"3\">gmc</option>;
</select>";
$carVal=$_GET['carDropDown'.$row['id']];
echo $carVal;
mysql_query("INSERT INTO car (car_val) VALUE ($carVal) WHERE id=$currentID ");
echo "</td><td>";
echo $row['name'];
echo "</td><td>";
}////end while
echo"<table>";
echo '<td bgcolor=#7FFF00><input type="submit" value="Update"></td>';
echo "</form>";
?>
You are using a where clause on an insert this is not correct.
You should either be using an auto increment field for the id in which case you don’t provide it, or you should provide it youself. Either way an insert statement always looks like the following
insert into tablename (column1name, col2name) values (value1, value2)etc.Except for the case where you provide data for all columns in the correct order in which case you can shorten it, but I would always advise using the above syntax.
If you don’t use an auto increment field then it is up to you to provide the id but within the format mentioned in my previous sentecne.