Hi I have the following code:
$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "
<option value='{$row['name']}'>{$row['name']}</option>";
}
$dropdown .= "
</select>";
echo $dropdown;
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
And this one (databaseupdate.php):
<?php
$username = "root";
$password = "usbw";
$hostname = "localhost";
$db_name = "examples";
$tbl_name = "cars";
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$nameupdate = $_POST['nameupdate'];
$yearupdate = $_POST['yearupdate'];
$test = $_POST['test'];
$query = "UPDATE cars SET name = '$nameupdate', year = '$yearupdate' WHERE id='$test'";
mysql_query($query) or die (mysql_error());
if($query){
echo "Successful";
echo "<BR>";
echo "<a href='databaseconnect.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
So, the list is populated by all the car names from the database, which is good but from this point I don't know how to get the id that comes with the car's name.
I have tried it with a query that says "wher id='$test'" (as you can see in my code) but that returns empty.
Example of what I want:
If I select "Mercedes" from the dropdownlist and want to update it with the textboxes I want my code to know that Mercedes has an ID of 1.
I don't know how I can do that and I hope someone can help me out here.
Thanks.
do like this: (just add dropdown inside your form)
<?php
$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "
<option value='{$row['id']}'>{$row['name']}</option>";
}
$dropdown .= "
</select>";
?>
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
<?php echo $dropdown; ?>
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
First of all include your dropdown inside the form.
Second if you want ID for your drop down selection, fetch id from db for the car name in $result
, then do like this:
$dropdown = "<select name='test'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "
<option value='{$row['ID']}'>{$row['name']}</option>";
}
$dropdown .= "
</select>";
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
echo $dropdown;
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<input type="submit" value="Update">
</form>
Done.
<form name="input" action="databaseupdate.php" method="post">
Select the car you want to edit and fill in the form, followed by clicking the update button.
<br>
Carname: <input type="text" name="nameupdate">
Year build: <input type="text" name="yearupdate">
<select name='test'>
<?php while($row = mysql_fetch_assoc($result)) {
echo "
<option value='{$row['name']}'>{$row['name']}</option>";
}?>
</select>
<input type="submit" value="Update">
</form>
in your databaseupdate.php
$values = $_POST['test'];
$values contains selected values
The main problem is that you didn't included your select into form tag. Also to get values from POST method you should use $_POST in PHP or $_REQUEST
to check what $_POST contains you can use function
var_dump($_POST);