Lloyd Owen Lloyd Owen - 3 months ago 13
PHP Question

PHP post ID of dropdown statement not value

I have a PHP PDO statement that queries a table in my database to return a list of users.

The form is used to log maintenance events, so the username is used to log the person that has done the maintenance.

However when the form is submitted - its submitting the value of the dropdown box instead of the ID of the row it's returning. The table its submitting it into has a column "engineer" the is referencing the engineers table e.g. John Doe id = 1.

I think I need it to post the ID and not the name.. Here's my code:

<form action="" method="post">
<label>Engineer Name:</label>
<select id="teamlist" name="teamlist">
$pdo = new PDO('mysql:host=localhost;dbname=frontier_maintlog', 'user', 'pass');

$stmt = $pdo->prepare('Select name from engineers');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['name'].'</option>';


<input type="submit" value=" Submit " name="submit"/><br />

and here's the bit that does the inserting into database:

$sql = "INSERT INTO log (engineer, description, fa_description)
VALUES ('".$_POST["engineer"]."','".$_POST["description"]."','".$_POST["fa_description"]."')";

The "log" table looks a bit like this:logtableimage

Thank you! Sorry if it doesn't make perfect sense.

Here's the error I receive
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'engineer' at row 1


Error is that you are inserting string to int datatype.

You should also select id value from engineers table, to use for options:

$stmt = $pdo->prepare('Select id,name from engineers');

// and assign value to options 
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) 
    echo "<option value='$row[id]'>$row[name]</option>";