dskpywvki951583595 2014-01-29 08:54
浏览 32
已采纳

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

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);
?> 
  • 写回答

1条回答 默认 最新

  • doumei9589 2014-01-29 09:14
    关注

    you forget to select Supplier_id in query

    try this

    $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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条