Injecting two queries into two different tables in mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Injecting two queries into two different tables in mysql

Post by Supplement »

I'm sure it's possible but I can't seem to get it to fire correctly. Here's my code:

Code: Select all

<?php

header("Location:http://www.web.com/inte.php"); 



$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("thedb", $con);

$sql="INSERT INTO affus (Cards, NoCards, User, Pass, hmnumber, BusinessType, Ctry, Company, addr1, City, State, zip, wknumber, wkfax, Fname, lname, Email, Email2)
VALUES
('$_POST[Cards]','$_POST[NoCards]','$_POST[User]','$_POST[Pass]','$_POST[hmnumber]','$_POST[BusinessType]','$_POST[Ctry]','$_POST[Company]','$_POST[addr1]','$_POST[City]','$_POST[State]','$_POST[zip]','$_POST[wknumber]','$_POST[wkfax]','$_POST[Fname]','$_POST[lname]','$_POST[Email]','$_POST[Email2]')";


$sql_1="INSERT INTO usup (username, password)
VALUES('$_POST[User]','$_POST[Pass]')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)


?>




In need of a bit of help. Thanks in advance gents.

=]
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Injecting two queries into two different tables in mysql

Post by Celauran »

I don't see you ever executing mysql_query($sql_1)
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Injecting two queries into two different tables in mysql

Post by Jade »

You need to connect to the database on the other website, and you need to have the database on http://web.com configured to allow access from whatever site you're putting this piece of code on. Then you'd do:

Code: Select all

<?php
$con = mysql_connect("http://web.com","username","password");
if (!$con)
  die('Could not connect: ' . mysql_error());

mysql_select_db("thedb", $con);

$sql="INSERT INTO affus (Cards, NoCards, User, Pass, hmnumber, BusinessType, Ctry, Company, addr1, City, State, zip, wknumber, wkfax, Fname, lname, Email, Email2)
VALUES
('$_POST[Cards]','$_POST[NoCards]','$_POST[User]','$_POST[Pass]','$_POST[hmnumber]','$_POST[BusinessType]','$_POST[Ctry]','$_POST[Company]','$_POST[addr1]','$_POST[City]','$_POST[State]','$_POST[zip]','$_POST[wknumber]','$_POST[wkfax]','$_POST[Fname]','$_POST[lname]','$_POST[Email]','$_POST[Email2]')";


$sql_1="INSERT INTO usup (username, password)
VALUES('$_POST[User]','$_POST[Pass]')";


if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)


?>

User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Re: Injecting two queries into two different tables in mysql

Post by Supplement »

same website for both queries 'thedb'.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Injecting two queries into two different tables in mysql

Post by Celauran »

What's with the header redirect at the top of the page?
User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Re: Injecting two queries into two different tables in mysql

Post by Supplement »

Celauran wrote:What's with the header redirect at the top of the page?

It just redirects to another form after the previous form data is injected.
User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Re: Injecting two queries into two different tables in mysql

Post by Supplement »

Celauran wrote:I don't see you ever executing mysql_query($sql_1)

right, it's only injecting the first query into 'thedb' into the table 'affus' but the second query is not inserting into the database 'thedb' and table 'usup'.
User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Re: Injecting two queries into two different tables in mysql

Post by Supplement »

is this not possible?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Injecting two queries into two different tables in mysql

Post by Celauran »

Aside from the fact that you're completely ignoring one of the two queries, you're redirecting away from the page before either of them gets a chance to run.
User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Re: Injecting two queries into two different tables in mysql

Post by Supplement »

the first query runs fine.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Injecting two queries into two different tables in mysql

Post by Celauran »

Strange. It shouldn't.

At any rate, $sql_1 isn't running because you aren't calling mysql_query($sql_1).
User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Re: Injecting two queries into two different tables in mysql

Post by Supplement »

ok, i got it working. thank you..

question


How come is won't insert if i add it here?

if (!mysql_query($sql,$sql_1,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";

mysql_close($con)
User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Re: Injecting two queries into two different tables in mysql

Post by Supplement »

i ended up adding it here:

$sql_1="INSERT INTO usup (username, password)
VALUES('$_POST[User]','$_POST[Pass]')"; mysql_query($sql_1);


and it worked.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Injecting two queries into two different tables in mysql

Post by Celauran »

Supplement wrote:question


How come is won't insert if i add it here?

Code: Select all

if (!mysql_query($sql,[b]$sql_1[/b],$con))
Because functions expect certain arguments in a certain order.
mysql_query()
User avatar
Supplement
Forum Commoner
Posts: 45
Joined: Thu Aug 18, 2011 8:52 pm
Location: Oceanside, CA

Re: Injecting two queries into two different tables in mysql

Post by Supplement »

Additionally, I'd Imagine that I need to mysql_escape_string()???

I forget where it goes in the syntax.

Again, sorry for such a noob question.

:banghead:
Post Reply