I use a form with dropdown select that collects data from 2 left joined mysql tables (suppliers & suppliers_expenses) and save them into suppliers_expenses table. The dropdown shows the names of all suppliers and want to save the "Supplier_id" of this particular supplier. The problem is that the "Supplier_id" value is not inserted into table (only 0) but all the other values works OK. I'm stacked.
Bellow see the form i use.
<form action="insert_suppliersExpenses.php" method="post">
<?php
$con=mysqli_connect("server","dbuser","123","db");
mysqli_set_charset($con, 'utf8');
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET CHARACTER SET 'utf8'");
mysqli_query($con, "SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT Name
FROM suppliers
LEFT JOIN suppliers_expenses
ON suppliers.Supplier_id=suppliers_expenses.Supplier_id
GROUP BY Name;"; //Write a query
$data = mysqli_query($con, $query); //Execute the query
?>
Name: <select name="Supplier_id">
<?php
while($fetch_options = mysqli_fetch_assoc($data)) { //Loop all the options retrieved from the query
?>
<option id ="<?php echo $fetch_options['Supplier_id']; ?>" value="<?php echo $fetch_options['Supplier_id']; ?>"><?php echo $fetch_options['Name']; ?></option>
<!--Echo out options-->
<?php
}
?>
</select>
<br>
Subject: <input type="text" name="Subject"><br>
Date: <input id="datepicker" type="text" name="datepicker" /><br>
Ammount: <input type="text" name="Ammount"><br>
<input type="submit" value="submit">
<input type="reset" value="reset">
</form>
And the php page to insert the values.
--------------------------------------
<?php
$con=mysqli_connect("server","dbuser","123","db");
mysqli_set_charset($con, 'utf8');
mysqli_query($con, "SET NAMES 'utf8'");
mysqli_query($con, "SET CHARACTER SET 'utf8'");
mysqli_query($con, "SET COLLATION_CONNECTION = 'utf8_unicode_ci'");
$datepicker=date("Y-m-d",strtotime($date));
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO suppliers_expenses (Supplier_id, datepicker, Subject, Ammount)
VALUES ('$_POST[Supplier_id]','" . $_POST['datepicker'] . "','$_POST[Subject]','$_POST[Ammount]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "SUCCESFULL INSERTION";
mysqli_close($con);
?>