如何使用php将数据插入2个mysql表 [英] how to insert data to 2 mysql tables using php
本文介绍了如何使用php将数据插入2个mysql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我编写的代码,我一次只能插入一个表。请帮助我如何编写可以一次插入2个表的代码,我是php我的sql的新手,所以请帮助我详细感谢朋友,因为我从这个论坛一直得到非常好的回复..对不起,如果这个问题是愚蠢的,因为我开始只在一个星期的项目上工作
<?php
$ con = mysql_connect( localhost, root, );
if (!$ con)
{
die(' 无法连接:' .mysql_error());
}
mysql_select_db( newcrm,$ con) ;
$ sql = INSERT INTO common(refno,date,agentname,zcno,callerid)
VALUES
('$ _POST [refid]','$ _ POST [date]','$ _ POST [agentname]','$ _ POST [zcno]','$ _ POST [cid]') 跨度>;
$ sql1 = INSERT INTO查询(refno,customername,category,comments)
VALUES
('$ _POST [refid]','$ _ POST [cname]','$ _ POST [catg]','$ _ POST [commts]');
if (!mysql_query($ sql,$ con)&&!mysql_query($ sql1,$ con))
{
die(' 错误:' .mysql_error());
}
mysql_close($ con)
?>
解决方案
con = mysql_connect( localhost,< span class =code-string> root, 跨度>);
if (!
con)
{
die(' 无法连接:' .mysql_error());
}
mysql_select_db( newcrm,
CON);
below is my code written and i can insert only to one table at a time. please help me how to write code which can insert to 2 tables at a time and i am new to php my sql so please help me in details thanks friends as i get very good responses from this forum all the time.. and sorry if this question is silly as i started working on project a week age only
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("newcrm", $con);
$sql="INSERT INTO common (refno, date, agentname, zcno, callerid)
VALUES
('$_POST[refid]','$_POST[date]','$_POST[agentname]','$_POST[zcno]','$_POST[cid]')";
$sql1="INSERT INTO enquiry (refno, customername, category, comments)
VALUES
('$_POST[refid]','$_POST[cname]','$_POST[catg]','$_POST[commts]')";
if (!mysql_query($sql,$con) && !mysql_query($sql1,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con)
?>
解决方案
con = mysql_connect("localhost","root",""); if (!
con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("newcrm",
con);
这篇关于如何使用php将数据插入2个mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文