I have a table that prints out all of the users from my table. Next to each user is a select box and a update button. The problem I am having is that the script only reads from the last select box. How can I make the select box dynamic so the script knows which row the select box is from?
code for the select box:
<select name="level" >
<option value="basic" SELECTED>Basic</option>
<option value="premium">Premium</option>
<option value="platinum">Platinum</option>
</select>
<button type="submit" name="update" value="<?php echo $row['id'] ?>">Update</button>
Code in another script that comes into effect when the submit button is pressed:
if ( isset( $_POST['update'] ) ) {
//Get the ID of the account that will be updated
$userID = $_POST['update'];
//Get the level that the admin wishes to change to
$level= $_POST['level'];
//Connect to the DB
mysql_connect("localhost", "xxxx", "xxxx")or die("Cannot connect to database");
mysql_select_db("xxxx")or die("Database cannot be selected or it doesnt exist");
//Updates the users access level
mysql_query("UPDATE users SET level='$level' WHERE id='$userID' ");
Just looking at the html, you are overwriting your select box values as they all have the same name.
The easiest solution would be to convert the name of your select box in an array:
No you can access them server-side as:
You also need to switch to PDO / mysqli and prepared statements with bound variables to avoid your sql injection problem.