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

Edited by Kristoff1875
Link to comment
Share on other sites

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";

Edited by berridgeab
Link to comment
Share on other sites

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:

Edited by Kristoff1875
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.