Kristoff1875 Posted December 18, 2012 Share Posted December 18, 2012 (edited) 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 December 18, 2012 by Kristoff1875 Quote Link to comment https://forums.phpfreaks.com/topic/272155-grabbing-single-value-from-other-table-to-insert/ Share on other sites More sharing options...
Love2c0de Posted December 18, 2012 Share Posted December 18, 2012 What is the name of your other table? I think you may have to do a join. Regards, AoTB. Quote Link to comment https://forums.phpfreaks.com/topic/272155-grabbing-single-value-from-other-table-to-insert/#findComment-1400203 Share on other sites More sharing options...
Kristoff1875 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) The other table is called 'Clients'. Cheers Edited December 19, 2012 by Kristoff1875 Quote Link to comment https://forums.phpfreaks.com/topic/272155-grabbing-single-value-from-other-table-to-insert/#findComment-1400205 Share on other sites More sharing options...
berridgeab Posted December 19, 2012 Share Posted December 19, 2012 (edited) 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 December 19, 2012 by berridgeab Quote Link to comment https://forums.phpfreaks.com/topic/272155-grabbing-single-value-from-other-table-to-insert/#findComment-1400207 Share on other sites More sharing options...
Kristoff1875 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) 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 Edited December 19, 2012 by Kristoff1875 Quote Link to comment https://forums.phpfreaks.com/topic/272155-grabbing-single-value-from-other-table-to-insert/#findComment-1400210 Share on other sites More sharing options...
Muddy_Funster Posted December 19, 2012 Share Posted December 19, 2012 you really shouldn't be sending raw $_POST[] data anywhere neer your database Quote Link to comment https://forums.phpfreaks.com/topic/272155-grabbing-single-value-from-other-table-to-insert/#findComment-1400256 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.