我使用带有下拉选择的表单,该表单从2个左连接的mysql表(供应商和供应商_经费)中收集数据并将其保存到suppliers_expenses表中。下拉列表显示所有供应商的名称,并希望保存此特定供应商的“Supplier_id”。问题是“Supplier_id”值没有插入到表中(仅为0),但所有其他值都可以正常工作。我已经堆积了。
贝娄看到我使用的表格。
<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);
?>
答案 0 :(得分:0)
您忘记在查询中选择Supplier_id
试试这个
$query = "SELECT suppliers.Supplier_id,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