我有一个网页表单,我希望从记录应提交到两个表下相同的数据库名称。
我的代码是
<?php
$con = mysql_connect("localhost","************","***********");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db=mysql_select_db("qserves1_uksurvey", $con);
$sql="INSERT INTO forms (date, Receivingsky, Title, Firstname, Lastname, House, Street, Town, County, Postcode, Number, WarrantyCoverForSky, Tvmake, Warrantycover, Payingmonthly, Agentnotes, Agentname)
VALUES
(NOW(),'$_POST[Receivingsky]','$_POST[Title]','$_POST[Firstname]','$_POST[Lastname]','$_POST[House]','$_POST[Street]','$_POST[Town]','$_POST[County]','$_POST[Postcode]','$_POST[Number]','$_POST[WarrantyCoverForSky]','$_POST[Tvmake]','$_POST[Warrantycover]','$_POST[Payingmonthly]','$_POST[Agentnotes]','$_POST[Agentname]')";
$sql_result = mysql_query($sql, $con) or die (mysql_error());
$con2 = mysql_connect("localhost","*******8","*********8");
if (!$con2)
{
die('Could not connect: ' . mysql_error());
}
$db2=mysql_select_db("qserves1_uksurvey", $con2);
$sql2="INSERT INTO dupphones (date, Number)
Values
(NOW(),'$_POST[Number]')";
$sql_result = mysql_query($sql2, $con2) or die (mysql_error());
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo '<html>
<head>
<title>Lead Submitted successfully!!!</title>
</head>
<body>
<center>
<strong><a href="http://q2serves.com/uksurvey.html">Lead Submitted ---- Click Here To Enter New Lead</a></strong>
</center>
</body>
</html>!';
mysql_close($con)
?>这是提交3条线索,1条在表格中,2条在表格形式中。
我希望这提交在每个表中只有1铅。
请帮帮忙
谢谢
发布于 2011-04-09 18:07:33
您可以创建两个到同一数据库的连接。您还可以多次执行mysql_query,有时使用$sql2,然后使用$sql。为了让你的代码看起来更清晰一点,下面是它的样子:
<?php
$con = mysql_connect("localhost","************","***********");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$db=mysql_select_db("qserves1_uksurvey", $con);
$sql="INSERT INTO forms (date, Receivingsky, Title, Firstname, Lastname, House, Street, Town, County, Postcode, Number, WarrantyCoverForSky, Tvmake, Warrantycover, Payingmonthly, Agentnotes, Agentname)
VALUES
(NOW(),'$_POST[Receivingsky]','$_POST[Title]','$_POST[Firstname]',
'$_POST[Lastname]','$_POST[House]','$_POST[Street]','$_POST[Town]','$_POST[County]',
'$_POST[Postcode]','$_POST[Number]','$_POST[WarrantyCoverForSky]','$_POST[Tvmake]',
'$_POST[Warrantycover]','$_POST[Payingmonthly]','$_POST[Agentnotes]',
'$_POST[Agentname]')";
$sql_result = mysql_query($sql, $con) or die (mysql_error());
$sql2="INSERT INTO dupphones (date, Number)
Values
(NOW(),'$_POST[Number]')";
$sql_result = mysql_query($sql2, $con2) or die (mysql_error());
echo '<html>
<head>请注意,您的查询仍然容易受到sql注入的攻击。使用转义或准备好的语句来消除sql注入。
发布于 2011-04-09 18:07:50
只有几句忠告。在提交之前清理您的$_POST数据。并考虑使用主键。另外,为什么要建立新的连接来运行第二个查询?
至少使用mysql_real_escape()
您会得到两个查询,因为您这样做了
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}它再次运行查询。你已经在上面运行过一次了
$sql_result = mysql_query($sql, $con) or die (mysql_error());https://stackoverflow.com/questions/5604312
复制相似问题