Jump to content

how do iinsert the auto_increment field into another table?


simpsonaj

Recommended Posts

Hi guys,

 

I'm new to PHP and i've been trying to do this for a very long time and i just can't get it to work, even according to all the resources i can find on the net so just wondering if there's anyone out there who can help?!....

 

I have 2 tables ('customer' and 'regcustomer') and 1 form. The user fills in the form with all their details and the script should insert their credentials into the 'customer' table, which has an auto_increment field for the 'customerid'. I then want the script to take the 'username' and 'password' specified in the form, AND the 'customerid' generated in the 'customer' table and insert it into the 'regcustomer' table.

 

The credentials are inserting successfully into the 'customer' table and the 'customerid' is automatically being generated there. I just can't get the generated 'customerid' to insert into the 'regcustomer' table.

 

Here's the code that's generating the error:

 

$query1 = "SELECT last_insert_id(customerid) FROM customer";

$result1 = mysql_query($query1);

//echo $result1;

$row = mysql_fetch_array($result1);

$result2 = $row['customerid'];

$datapoint = mysql_result($result2, 0, customerid);

 

$result3 = "INSERT INTO regcustomer (username, password, $datapoint)

VALUES

('$_POST[username]','$_POST[password]','$_POST[customerid]')";

 

The error message i'm getting is:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in....in line 25

 

Line 25 is the  [$datapoint = mysql_result($result2, 0, customerid);] line. I think it doesn't like the '0' argument here as it's turned to red in my editor!

 

Any help would be greatly appreciated.

Thank you very much!

 

Alex

Well, $result2 isn't a mysql resource result, its a value that you retrieved from your database.

 

After you've inserted everything into the customer table, take it out again:

 

$query = "SELECT * FROM `customer`";

$result = mysql_query($query);

$username = mysql_result($result, 0, username);

$password = mysql_result($result, 0, password);

$customerid = mysql_result($result, 0, customerid);

 

$query = "INSERT INTO regcustomer (username, password, customerid) VALUES ('\''.$username.'\'', '\''.$password.'\'', '\''.$customerid.'\'')";

if(mysql_query($query)) {

  echo "Success!";

  } else {

  echo "Failure";

  }

 

This should do it for you.

Hello thanks very much for this...

 

If anyone's interested, this is how i managed to get this working, very simple, i guess i was trying to make it too complicated!...

 

$id = mysql_insert_id();

mysql_query("INSERT INTO regcustomer (username, password, customerid)

VALUES ('$_POST[username]','$_POST[password]',$id)");

 

Thanks,

Alex

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.