简体   繁体   English

如何将表ID传递给PHP中的另一个表

[英]How to pass table id to another table in PHP

There are 02 tables called item and customer . 02表,分别称为itemcustomer

item(item_id, item_name) customer(cus_id, iid, cus_name)

I just tried to store item_id from item to the iid in the customer . 我只是试图将item_iditemiid存储在customer but it always showing null values. 但它始终显示null值。 My database is item_sales . 我的数据库是item_sales

Here is my PHP code 这是我的PHP代码

<html>
<title></title>
<head></head>
<body>

<?php 
$hostname = "localhost";
$database = "item_sales";
$username = "root";
$password = "";

$con = mysql_pconnect($hostname, $username, $password);

error_reporting(0);

?>

<form action="index.php" method="post" enctype="multipart/form-data">

<p>Customer Name : <input type="text" name="cus_name" /><br/><br/>  </p>
<p>Select an Item: 
<select name="iid">
        <?php
            $sql = mysql_query("SELECT * FROM item");
            mysql_select_db($database,$con);
            while($sqlv = mysql_fetch_array($sql)) 
            { ?>
                <option id="<?php echo $sqlv['item_id']; ?>"><?php echo $sqlv['item_name']; ?></option>
            <?php } ?>
    </select>
    </p>

 <?php

 if(isset($_POST['submit']))
 {
        $sql2 = "SELECT * FROM item WHERE iid='%item_id%'";
        mysql_select_db($database,$con);
        $mydata = mysql_query($sql2);

        $cus_name = $_POST['cus_name'];


        $sql3 = "INSERT INTO customer (cus_id, iid, cus_name) VALUES ('', '$_POST[iid]', '$cus_name')";
        mysql_query($sql3);

 }
 ?>



 <input type="submit" name="submit" value="Add Sale" />
</form>


</body>
</html>

The reason it is not working is that you are attempting to save the iid select into the iid field, and I'm guessing the iid field in customer is a numeric type field, like INT - using the POST variable like this, you are going to be saving the text of the SELECT rather than the val. 它不起作用的原因是,您尝试将iid选择保存到iid字段中,而我猜客户中的iid字段是一个数字类型字段,例如INT-使用这样的POST变量,您将保存SELECT而不是val的文本。

What you need to do to fix this particular problem is set a "value" on each of the select options. 要解决此特定问题,需要在每个选择选项上设置一个“值”。 You've set an ID but thats no real help here. 您已经设置了一个ID,但这在这里并没有真正的帮助。

<select name="iid">
    <?php
    $sql = mysql_query("SELECT * FROM item");
    mysql_select_db($database,$con);
    while($sqlv = mysql_fetch_array($sql)) 
    { ?>
        <option value="<?php echo $sqlv['item_id']; ?>"><?php echo $sqlv['item_name']; ?></option>
    <?php } ?>
</select>

This is besides the point your code is very dangerous. 除此之外,您的代码非常危险。 I would recommend you do not use the original mysql functions as, 1) they don't offer any real protection from malicious users, and 2) they will be removed from PHP support very soon. 我建议您不要使用原始的mysql函数,因为1)它们没有对恶意用户提供任何真正的保护,2)它们将很快从PHP支持中删除。

See this SO article on how to replace the mysql functionality from your PHP code : How can I prevent SQL injection in PHP? 请参阅这篇SO文章,了解如何从PHP代码中替换mysql功能: 如何防止PHP中的SQL注入?

That article also might help you understand the dangers your code offers. 该文章还可以帮助您了解代码所带来的危险。

The correct code is following : 正确的代码如下:

<html>
<title></title>
<head></head>
<body>

<?php 
$hostname = "localhost";
$database = "item_sales";
$username = "root";
$password = "";

$con = mysql_pconnect($hostname, $username, $password);

error_reporting(0);

?>

<form action="index.php" method="post" enctype="multipart/form-data">

<p>Customer Name : <input type="text" name="cus_name" /><br/><br/>  </p>
<p>Select an Item: 
<select name="iid">
        <?php
            $sql = mysql_query("SELECT * FROM item");
            mysql_select_db($database,$con);
            while($sqlv = mysql_fetch_array($sql)) 
            { ?>
                <option value="<?php echo $sqlv['item_id']; ?>"><?php echo $sqlv['item_name']; ?></option>
            <?php } ?>
    </select>
    </p>

  <?php

  if(isset($_POST['submit']))
  {
        $sql2 = "SELECT * FROM item";
        mysql_select_db($database,$con);
        $mydata = mysql_query($sql2);

        $cus_name = $_POST['cus_name'];
        $iid = $_GET['item_id'];


        $sql3 = "INSERT INTO customer (cus_id, iid, cus_name) VALUES ('', '$_POST[iid]', '$cus_name')";
        mysql_query($sql3);

    }
    ?>



    <input type="submit" name="submit" value="Add Sale" />
 </form>


 </body>
 </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM