SQL PHP Insert issue

There are several problems with the code as you have it. First of all, in this line, you want the value of a form-field called name_object.

 $name_object =$_POST['name_object'];

but there is no such form name. So either change that code to use the correct field name, or change the form to use this name. Spot the difference:

<select name="object">

In here:

echo "<option value=\"cust_name1\">" . $row['cust_name'] . "</option>";

and here:

echo "<option value=\"name_object1\">" . $row['name_object'] . "</option>";

you’re setting the value for every drop-down to be the same value, both of them text strings, which are almost certainly not in your other tables. Just do a “view source” on the form when it’s displayed to see what I mean.

Change the above to use the unique-id for each table, which will pass them through to your form processing code. Once in there, all you need to do is

 $sql = "INSERT INTO files (id_cust, register_date, id_object) 
         VALUES ($cust_name, $register_date, $name_object)";

after you’ve done some validation of the various values and fixed the field name issue.

Of course, once it’s working, you’ll want to change it to use prepared statements rather than just concatenating strings into the query.

Browse More Popular Posts

Leave a Comment