簡體   English   中英

帶有下拉菜單的表格可以從2個連接的mysql表中收集數據並將其保存到1個表中

[英]Form with dropdown select that collects data from 2 joined mysql tables and save them into 1 table

我使用帶有下拉選擇的表單,該表單從2個左連接的mysql表(suppliers和Suppliers_expenses)收集數據,並將它們保存到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);
?> 

您忘記在查詢中選擇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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM