Hi Everyone

I have a webform, from which i want records should be submitted into two tables under same database name.

My code is

<?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)
?>

This is submitting 3 leads 1 is in table dupphones and 2 leads in table forms.

I want this to submit 1 lead in each table only.


Please help

Thanks

Prakash ghai

Recommended Answers

All 6 Replies

You don't need below code.It is generating problem.
Remove it and then run.

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }

Thank you so much

It is working correct now !!!

And i have one more small problem, i am sure you will help me out with that as well

As i have these 2 tables i want Number field should be check from both tables on every new submission and if it is already exists and give error like record already exist. and should not be submitted.....and if record is new then that should be submitted in both the tables

i have used the below code for that but it not working fine----please check or suggest a different one

elseif(isset($_POST['Number'])){

	// check for number existence in database
	$query = "SELECT Number FROM forms WHERE Number LIKE '".$_POST['Number']."'";
	$resId = mysql_query($query);	
	$numOfno = mysql_num_rows($resId);
	if(($numOfno > 0) && isset($_POST['Number'])){
	echo "Phone No. already exists in our database!!!<br/>";
	}

I know here i have just mentioned only one table, but i am confused in this that what to do and how to mention the second table info......
Please help

Thanks

Hi Vibha

Please help

thanks

$db=mysql_select_db("qserves1_uksurvey", $con);
$sql =  "SELECT * FROM forms WHERE Number='".$_POST['Number']."'";
$res = mysql_query($sql, $con) or die (mysql_error());
if(mysql_num_rows($res)>0)
{
	echo 'forms already exist with number:'.$_POST['Number'];
	exit;
}
else
{
	$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());

}

Same with second table.
Try it and let me know still there is no fix.

Hi Vibha

It Is not working

Here is the complete page code

<?php
$con = mysql_connect("localhost","qserves1_ukadmin","lexington2009");
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","qserves1_ukadmin","lexington2009");
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());

$db=mysql_select_db("qserves1_uksurvey", $con);
$sql =  "SELECT * FROM forms WHERE Number='".$_POST['Number']."'";
$res = mysql_query($sql, $con) or die (mysql_error());
if(mysql_num_rows($res)>0)
{
	echo 'forms already exist with number:'.$_POST['Number'];
	exit;
}
else
{
	$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());

}

$db2=mysql_select_db("qserves1_uksurvey", $con);
$sql2 =  "SELECT * FROM dupphones WHERE Number='".$_POST['Number']."'";
$res = mysql_query($sql2, $con2) or die (mysql_error());
if(mysql_num_rows($res)>0)
{
	echo 'forms already exist with number:'.$_POST['Number'];
	exit;
}
else
{
	$sql2="INSERT INTO dupphones (date, Number)
	Values
(NOW(),'$_POST[Number]')";
	$sql_result = mysql_query($sql2, $con2) or die (mysql_error());

}

if ($_POST['Receivingsky']=="") {
Print("Please fill in Receivingsky!<br>");
}
elseif ($_POST['Title']=="")
{
Print("Please fill in Title!!<br>");
}
elseif ($_POST['Firstname']=="")
{
Print("Please fill in Firstname!!!<br>");
}
elseif ($_POST['Lastname']=="")
{
Print("Please fill in Lastname!!!<br>");
}
elseif ($_POST['Number']=="")
{
Print("Please fill in Number!!!<br>");
}
elseif ($_POST['WarrantyCoverForSky']=="")
{
Print("Please fill in WarrantyCoverForSky!!!<br>");
}
elseif ($_POST['Tvmake']=="")
{
Print("Please fill in Tvmake!!!<br>");
}
elseif ($_POST['Warrantycover']=="")
{
Print("Please fill in Warrantycover!!!<br>");
}
elseif ($_POST['Payingmonthly']=="")
{
Print("Please fill in Payingmonthly!!!<br>");
}
elseif ($_POST['Agentname']=="")
{
Print("Please fill in Agentname!!!<br>");
}
else

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)
?>

It is showing forms already exist with number: (the number we are submiting) ,,On every submission

and if the number is duplicate then even it is submitting and showing the above message after the submission

Because your code is completely wrong.
Make sure whenever you write any code which will be executed after form submission then put in if condition.
e.g. in below code 'submitBtn' is your form submit button name.
You have placed insert code twice.correct it.

Here it is:

<?php
$con = mysql_connect("localhost","qserves1_ukadmin","lexington2009");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

if(isset($_POST['submitBtn']))
{
//============ db 1 ===========
$db=mysql_select_db("qserves1_uksurvey", $con);
$sql =  "SELECT * FROM forms WHERE Number='".$_POST['Number']."'";
$res = mysql_query($sql, $con) or die (mysql_error());
if(mysql_num_rows($res)>0)
{
	echo 'forms already exist with number:'.$_POST['Number'];
	exit;
}
else
{
	$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());

}
//============ db 2 ===========
$db2=mysql_select_db("qserves1_uksurvey", $con);
$sql2 =  "SELECT * FROM dupphones WHERE Number='".$_POST['Number']."'";
$res = mysql_query($sql2, $con2) or die (mysql_error());
if(mysql_num_rows($res)>0)
{
	echo 'forms already exist with number:'.$_POST['Number'];
	exit;
}
else
{
	$sql2="INSERT INTO dupphones (date, Number)
	Values
(NOW(),'$_POST[Number]')";
	$sql_result = mysql_query($sql2, $con2) or die (mysql_error());

}
}

if ($_POST['Receivingsky']=="") {
Print("Please fill in Receivingsky!<br>");
}
elseif ($_POST['Title']=="")
{
Print("Please fill in Title!!<br>");
}
elseif ($_POST['Firstname']=="")
{
Print("Please fill in Firstname!!!<br>");
}
elseif ($_POST['Lastname']=="")
{
Print("Please fill in Lastname!!!<br>");
}
elseif ($_POST['Number']=="")
{
Print("Please fill in Number!!!<br>");
}
elseif ($_POST['WarrantyCoverForSky']=="")
{
Print("Please fill in WarrantyCoverForSky!!!<br>");
}
elseif ($_POST['Tvmake']=="")
{
Print("Please fill in Tvmake!!!<br>");
}
elseif ($_POST['Warrantycover']=="")
{
Print("Please fill in Warrantycover!!!<br>");
}
elseif ($_POST['Payingmonthly']=="")
{
Print("Please fill in Payingmonthly!!!<br>");
}
elseif ($_POST['Agentname']=="")
{
Print("Please fill in Agentname!!!<br>");
}
else

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)
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.