Click here to Skip to main content
15,072,134 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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
<?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)
?>
Posted
Updated 5-Mar-18 21:24pm

Hello,

Try the following.
PHP
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
 
mysql_select_db("newcrm", $con);
 
$sql = sprintf("INSERT INTO common (refno, date, agentname, zcno, callerid) VALUES ('%s', '%s', '%s', '%s', '%s')", 
               mysql_real_escape_string($_POST[refid]), $_POST[date], mysql_real_escape_string($_POST[agentname]), 
               mysql_real_escape_string($_POST[zcno]), mysql_real_escape_string($_POST[cid]));
$ret = mysql_query($sql, $con);
if (!ret) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    die($message);
}

$sql1 = sprintf("INSERT INTO enquiry (refno, customername, category, comments) VALUES('%s', '%s', '%s', '%s')", 
                  mysql_real_escape_string($_POST[refid]), mysql_real_escape_string($_POST[cname]), 
                  mysql_real_escape_string($_POST[catg]), mysql_real_escape_string($_POST[commts]));

$ret = mysql_query($sql1, $con);
if (!ret) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    die($message);
}
mysql_close($con)
?>

Regards,
   
You have to use mysql_query. For example:
PHP
$sql="INSERT INTO common (refno, date, agentname, zcno, callerid)
VALUES
('$_POST[refid]','$_POST[date]','$_POST[agentname]','$_POST[zcno]','$_POST[cid]')";
mysql_query($sql); 
$sql1="INSERT INTO enquiry (refno, customername, category, comments)
VALUES
('$_POST[refid]','$_POST[cname]','$_POST[catg]','$_POST[commts]')";
mysql_qery($sql1); 

or
PHP
$sql1=mysql_query("INSERT INTO enquiry (refno, customername, category, comments)
VALUES
('$_POST[refid]','$_POST[cname]','$_POST[catg]','$_POST[commts]')");
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900