gpt4 book ai didi

php - 带有下拉选择的表单,从 2 个连接的 mysql 表收集数据并将它们保存到 1 个表中

转载 作者:行者123 更新时间:2023-11-30 00:45:18 25 4
gpt4 key购买 nike

我使用带有下拉选择的表单,该表单从 2 个左连接的 mysql 表(供应商和供应商费用)收集数据并将它们保存到供应商费用表中。下拉列表显示所有供应商的名称,并希望保存该特定供应商的“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);
?>

最佳答案

您忘记在查询中选择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

关于php - 带有下拉选择的表单,从 2 个连接的 mysql 表收集数据并将它们保存到 1 个表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21426239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com