adriscoll Posted May 24, 2011 Share Posted May 24, 2011 Hello. I have a simple enough code that takes information from one table and drops it into another. This is great, but I have 2a new complexities that I have been unable to code correctly. A. 'lastname', 'firstname' on table1 need to be combined into 'name' How & Where do I combine these strings and then pass them? <?php include('dbconfig.php'); // Make a MySQL Connection mysql_connect("localhost", "$user", "$password") or die(mysql_error()); mysql_select_db("$database") or die(mysql_error()); $result = mysql_query( "INSERT INTO table2 (lastname, firstname, email) SELECT lastname, firstname, email FROM table1 WHERE email='[email protected]' ") or die(mysql_error()); ?> Quote Link to comment https://forums.phpfreaks.com/topic/237303-insert-into-select-with-concatenated-variables/ Share on other sites More sharing options...
Nodral Posted May 24, 2011 Share Posted May 24, 2011 Hi Do it as 2 seperate queries. $sql="SELECT lastname, firstname, email FROM table1 WHERE email='[email protected]' "; $sql=mysql_query($sql) or die(); while($row=mysql_fetch_array($sql){ $name=$row['lastname'] . $row['firstname']; } $sql="INSERT INTO table2 " . $name; mysql_query($sql) or die(); Quote Link to comment https://forums.phpfreaks.com/topic/237303-insert-into-select-with-concatenated-variables/#findComment-1219532 Share on other sites More sharing options...
adriscoll Posted June 17, 2011 Author Share Posted June 17, 2011 Nodral, I appreciate the response but that query didn't populate anything. I think it is on the right track, but am unable to execute it. Quote Link to comment https://forums.phpfreaks.com/topic/237303-insert-into-select-with-concatenated-variables/#findComment-1230902 Share on other sites More sharing options...
PFMaBiSmAd Posted June 17, 2011 Share Posted June 17, 2011 Should work (you provided no information about how the names should be combined) - "INSERT INTO table2 (name, email) SELECT CONCAT(lastname, firstname), email FROM table1 WHERE email='[email protected]'" Quote Link to comment https://forums.phpfreaks.com/topic/237303-insert-into-select-with-concatenated-variables/#findComment-1230904 Share on other sites More sharing options...
adriscoll Posted June 17, 2011 Author Share Posted June 17, 2011 PFMaBiSmAd, Thank you. That worked wonderfully. Quote Link to comment https://forums.phpfreaks.com/topic/237303-insert-into-select-with-concatenated-variables/#findComment-1230906 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.