Hello all, i dont know how to fix this i made allot of forms but never with select option, what i want is to post
the select option and to update database. here is the script could somebody explain me how to change this

echo '<tr><td><select>';
echo '<option value="volvo" name=uvolvo>Volvo</option>';
echo '<option value="saab" name=usaab>Saab</option>';
echo '<option value="opel" name=uopel>Opel</option>';
echo '<option value="audi" name=audi>Audi</option>';
echo '</tr></td></select>';

And do i need to change also this

$AddQuery = "INSERT INTO producten (Product, Item, Description, Extra, Valuta, Price, Nummer)

in to this

$AddQuery = "INSERT INTO producten (Product, Item, Description, Extra, Valuta, Price, Nummer, Volvo, Saab, Opel, Audi )

Any help is welcome.

if(isset($_POST['add'])){
$AddQuery = "INSERT INTO producten (Product, Item, Description, Extra, Valuta, Price, Nummer) VALUES ('$_POST[uproduct]','$_POST[uitem]','$_POST[udescription]','$_POST[uextra]','$_POST[uvaluta]','$_POST[uprice]','$_POST[unummer]')";         
mysql_query($AddQuery, $con);
};

$sql = "SELECT * FROM producten";
$myData = mysql_query($sql,$con);

while($record = mysql_fetch_array($myData))
echo '<table border="0" cellpadding="0" cellspacing="0"  id="id-form">';
echo '<form action=add-product.php method=post>';

echo '<tr>';
echo '<th valign="top">Product name:</th>';
echo '<td><input type=text class="inp-form" name=uproduct></td>';
echo '<td></td></tr>';

echo '<tr>';
echo '<th valign="top">Item:</th>';
echo '<td><input type=text class="inp-form" name=uitem></td>';
echo '<td></td></tr>';


echo '<tr>';
echo '<th valign="top">Extra</th>';
echo '<td><input type=text class="inp-form" name=uextra></td>';
echo '<td></td></tr>';

echo '<tr>';
echo '<th valign="top">Valuta</th>';
echo '<td><input type=text class="inp-form" name=uvaluta></td>';
echo '<td></td></tr>';

echo '<tr>';
echo '<th valign="top">Price</th>';
echo '<td><input type=text class="inp-form" name=uprice></td>';
echo '<td></td></tr>';

echo '<tr>';
echo '<th valign="top">Nummer</th>';
echo '<td><input type=text class="inp-form" name=unummer></td>';
echo '<td></td></tr>';

echo '<tr><td><select>';
echo '<option value="volvo" name=uvolvo>Volvo</option>';
echo '<option value="saab" name=usaab>Saab</option>';
echo '<option value="opel" name=uopel>Opel</option>';
echo '<option value="audi" name=audi>Audi</option>';
echo '</tr></td></select>';

echo '<tr>';
echo '<th valign="top">Description</th>';
echo '<td><textarea rows="" cols="" class="form-textarea" name=udescription></textarea></td>';
echo '<td></td></tr>';

echo '<tr>
    <th>Image 1:</th>
    <td><input class="file file_1" style="display: inline; width: 300px;"><div style="width: 78px; height: 29px; background-image: url(http://localhost/controlpannel/images/forms/upload_file.gif); display: inline; position: absolute; overflow: hidden; background-position: 100% 50%; background-repeat: no-repeat no-repeat;"><input type="file" class="file_1" style="position: relative; height: 29px; width: 300px; display: inline; cursor: pointer; opacity: 0; margin-left: -222px;"></div></td>
    <td>
    <div class="bubble-left"></div>
    <div class="bubble-inner">JPEG, GIF 5MB max per image</div>
    <div class="bubble-right"></div>
    </td>
    </tr>';


echo '<td valign="top">';
echo '<td>' . '<input type=submit class="form-submit" name=add value=' . ' </td>';
echo '</td></tr>';

echo '</form>';
echo '</table>';
mysql_close($con);

Thanks in advance !!

Dani AI

Generated

Short version: don’t add one column per car make and don’t put name on each <option>. is right — use a small lookup table for car makes and a single foreign-key column on your product table. Also give the <select> a name so the browser posts the chosen value.

Correct form markup (example):

<select name="car_make_id">
  <option value="1">Volvo</option>
  <option value="2">Saab</option>
  <option value="3">Opel</option>
  <option value="4">Audi</option>
</select>

Server-side: read the posted value, validate it, and save that single id into the product row. Example outline (use PDO or mysqli prepared statements — do not use old mysql_* functions):

$car_id = isset($_POST['car_make_id']) ? (int) $_POST['car_make_id'] : 0;
if ($car_id <= 0) { /* return error */ }

$stmt = $pdo->prepare('INSERT INTO producten (..., car_make_id) VALUES (..., :car_id)');
$stmt->execute([':car_id' => $car_id, /* other params */]);

Practical tips and gotchas:

  • Populate the <option>s by querying the car_makes table and outputting each row’s id as the value and the name as the label. Escape with htmlspecialchars() when printing.
  • Before inserting, confirm the submitted id actually exists in car_makes (avoid bad foreign keys).
  • If you keep using file inputs, set enctype="multipart/form-data" on the <form>.
  • Use prepared statements to prevent SQL injection and migrate off mysql_* (removed in modern PHP).
  • To preserve the user’s choice on form redisplay, compare the posted id when building options and add the selected attribute.

Follow that pattern and you won’t need to change your table structure every time the car list changes.

Member Avatar for Member #120589

This is not the way to do it. If you change the cars in the select,it means you have to change your DB table. That's just not practical.

You should be looking to create a table like this:

Product, Item, Description, Extra, Valuta, Price, Nummer, Car_make_id

With another related table for car_make:

Car_makes

car_make_id
car_make_name

You then just need to query this table to build your html select, e.g.

SELECT * FROM car_makes

while(...){
    $options.= "<option value='{$r['car_make_id']}'">{$r['car_make_name']}</option>";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.