有02
表称为item
和customer
。如何将表id传递给PHP中的另一个表
item(item_id, item_name) customer(cus_id, iid, cus_name)
我只是试图从item
存储item_id
到iid
在customer
。 但它总是显示null
值。 我的数据库是item_sales
。
这是我的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>
将是非常危险的代码。您允许将POST变量直接插入到数据库中,而且绝对不会进行验证。这使您的应用程序打开到SQL注入。 – Chris
尽管如此,不知道问题出在哪里,但我不确定为第二个SQL设置了%item_id%的值。这可能是你的问题。 – Chris
我不知道要更正此代码。但当我从下拉列表中选择一个选项时,我只想将'item'表中的'item_id'存储到'customer'表中的'iid'。 'iid'应该链接到'item'表中的'item_id'。你能改正这个代码吗? –