Page 1 of 1

[SOLVED] How do I execute multiple queries in a single page?

Posted: Sat Aug 07, 2010 9:05 pm
by Bbob
Hi

Is it possible to execute multiple queries in a single page?


What I want to do this is, once a user is registered, his username, password and type will be inserted in the login table as well as the customerinfo table.


So far this is what I can think of but it doesnt work. Any help would be much appreciated.

Code: Select all

<?php

	$businessname=$_POST['businessname']; 
	$businessaddress=$_POST['businessaddress']; 
	$contactperson=$_POST['contactperson']; 
	$contactnumber=$_POST['contactnumber']; 
	$customer_username=$_POST['customer_username']; 
	$customer_password=$_POST['customer_password']; 
	$registerdate = date ("Y-m-d");
	
	$type = "user";


$submit = $_POST['submit2'];


if ($submit)
{	

		
		//Start connect to DB
		$db_name="hp"; 
		$tbl_name="customerinfo"; 

		mysql_connect("localhost", "root", "")or die("cannot connect"); 
		mysql_select_db("$db_name")or die("cannot select DB");
		//End connect to DB
		
		
		
		//Insert to DB
		$sql="INSERT INTO customerinfo VALUES('','$businessname','$businessaddress','$contactperson','$contactnumber','$customer_username','$customer_password','$type','$registerdate')";
		
		$sql2 = "INSERT INTO login VALUES(' ' ,'$customer_username','$customer_password','$type')";
				
		
		mysql_query($sql, $sql2);
		
		die ("Registration Success!");
		
		
		
	}
	else
		echo "Please fill in the fields!";
?>
The code above gave me this error
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\hp\insertcustomerinfo.php on line 37

Re: How do I execute multiple queries in a single page?

Posted: Sat Aug 07, 2010 9:36 pm
by requinix
mysql_query

Okay. So you can't pass multiple queries to mysql_query. What's your next guess?

Re: How do I execute multiple queries in a single page?

Posted: Sat Aug 07, 2010 9:55 pm
by Bbob
Subject: How do I execute multiple queries in a single page?
tasairis wrote:mysql_query

Okay. So you can't pass multiple queries to mysql_query. What's your next guess?

Hi

I really have no idea what to do next, Ive even tried but it doesnt work either.

mysql_query = INSERT into login...

mysql_query = INSERT into customerinfo...


BTW if you guys have a better algorithm feel free to post it

Im new at PHP so my coding may not have the 'finesse' of an experience programmer.

Re: How do I execute multiple queries in a single page?

Posted: Sat Aug 07, 2010 10:26 pm
by requinix
Ugh.

Fine. Whatever. I guess those five glaring mistakes should have tipped me off.

Code: Select all

mysql_query($sql);
mysql_query($sql2);

Re: How do I execute multiple queries in a single page?

Posted: Sat Aug 07, 2010 10:52 pm
by Bbob
Hi

It still doesnt work, only the first query was executed.

Heres the contents of my table,

Table Name: login
Columns:
userid int10
username varchar45
password varchar45
type varchar5


Table Name: customerinfo
Columns:
customerID int10
businessname varchar45
businessaddress varchar64
contactperson varchar45
contactnumber double
username varchar45
password varchar45
type varchar5
registerdate date


BTW What are the 5 mistakes in my code?

Re: How do I execute multiple queries in a single page?

Posted: Sat Aug 07, 2010 11:00 pm
by requinix
Bbob wrote:It still doesnt work, only the first query was executed.
Then

Code: Select all

mysql_query($sql2) or die(mysql_error());
Spoiler: " " is not a valid number.

1. $submit = $_POST['submit2'];
You assume that submit2 exists. It might not. If it doesn't then PHP will give you a warning.

2. mysql_connect("localhost", "root", "")
a) You connect to your database as root.
b) root doesn't have a password.

3. "$db_name"
While not an error per se, it shows that you don't know really know what strings and/or variables are. Lose the quotes.

4. SQL injection. You're vulnerable to it. That means I can screw around with your database.

5. Unless this is an AJAX page, die ("Registration Success!") is a mistake.


Ah, I feel better.

Re: How do I execute multiple queries in a single page?

Posted: Sun Aug 08, 2010 12:37 am
by Bbob
Hi

Im not really going to put this codes in the net (Hence user = "root" & password = "")-- i dont even know how to put this online

Ill probably be the only one using this. This is only for me -- for practice for the next term.


==========================

As for the code the error showed something wrong about my foreign keys --- Ill check on that and post here if I run into any problems I dont understand.



Thanks for the tips!