Jump to content

Grabbing Single Value From Other Table To Insert


Kristoff1875

Recommended Posts

Hi guys, i'm currently using:

 

mysql_query("INSERT INTO Jobs (Username, JobNumber, Description, Status) VALUES ('$_POST[ClientUsername]', '$_POST[JobNumber]', '$_POST[Description]', '$_POST[status]')")
or die(mysql_error());

 

Which is inserting all the right values in to my database, only thing is I also want "ClientID" from Clients table to be fetched and put in like this:

 

mysql_query("INSERT INTO Jobs (Username, ClientID JobNumber, Description, Status) VALUES ('$_POST[ClientUsername]', $ClientID, '$_POST[JobNumber]', '$_POST[Description]', '$_POST[status]')")
or die(mysql_error());

 

As I say, this is on a different table. I've tried lots of different ways, like INSERT SELECT etc, but can't seem to have any joy.

 

Any help much appreciated.

 

Thanks

You need an Insert Select syntax.

 

You only need the clientId from the other table, and Select will return strings so long as you have properly escaped them.

 

You will need to do some of the POST keys.

$query = "
INSERT INTO
Jobs (Username, ClientID, JobNumber, Description, Status)
SELECT
'{$_POST['someVar']}',
clientId,
'{$_POST['someVar']}',
'{$_POST['someVar']}',
'{$_POST['someVar']}'
FROM Clients
WHERE clientId = 2";

Should this be right?

 

INSERT INTO
Jobs (ClientID, Username, JobNumber, Description, Status)
SELECT
ClientID,
'{$_POST['ClientUsername']}',
'{$_POST['JobNumber']}',
'{$_POST['Description']}'
'{$_POST['Status']}'
FROM Clients
WHERE Username = '{$_POST['ClientUsername']}'"

 

Because I want it to find the ClientID based on the username that is posted. At the moment i'm getting:

 

Column count doesn't match value count at row 1

 

Ah hold on, missing comma!

 

With the comma it works. Thanks so much :thumb-up:

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.