phppup Posted March 24, 2012 Share Posted March 24, 2012 I'm trying to understand the mechanics of PHP. Suppose I have a form that collects these three values $firstname = $_POST['firstname']; $middleinitial= $_POST['middleinitial']; $lastname= $_POST['lastname']; and I want to insert EVERYTHING into ONE FIELD in my table called FULL_NAME. What is the methodI should use to COMBINE the values shown above? Do they need to be listed individually in the statement below? What if I want a final entry to contain fiirstname (space) middleinitial (period) (space) lastname all in one field? $sql = "INSERT INTO myTable ( FULL_NAME ) VALUES ( $firstname,$middleinitial,$lastname )"; $result=mysql_query($sql); Would the $SQL line look like this? Or am I totally off track? Quote Link to comment https://forums.phpfreaks.com/topic/259630-the-mechanics-of-php/ Share on other sites More sharing options...
dragon_sa Posted March 24, 2012 Share Posted March 24, 2012 $name = "$firstname $middleinitial. $lastname"; and insert $name in your database Quote Link to comment https://forums.phpfreaks.com/topic/259630-the-mechanics-of-php/#findComment-1330744 Share on other sites More sharing options...
phppup Posted March 24, 2012 Author Share Posted March 24, 2012 And what about the SQL statement? How should that be modified? Quote Link to comment https://forums.phpfreaks.com/topic/259630-the-mechanics-of-php/#findComment-1330749 Share on other sites More sharing options...
AyKay47 Posted March 24, 2012 Share Posted March 24, 2012 And what about the SQL statement? How should that be modified? It doesn't need to be, you will generate the full name using PHP, something like what dragon_sa suggested. the code will then be: $firstname = $_POST['firstname']; $middleinitial= $_POST['middleinitial']; $lastname= $_POST['lastname']; $full_name = "$firstname $middleinitial $lastname"; $sql = "INSERT INTO myTable ( FULL_NAME ) VALUES ( $full_name )"; $result = mysql_query($sql); But typically, for greater database flexibility, you would separate each part of the name into 3 separate fields. So you would have `first_name`, `middle_initial` and `last_name` fields. Then when grabbing the data vie select statement, use CONCAT_WS() to alias all three columns into a full_name result. Quote Link to comment https://forums.phpfreaks.com/topic/259630-the-mechanics-of-php/#findComment-1330752 Share on other sites More sharing options...
Mahngiel Posted March 24, 2012 Share Posted March 24, 2012 $sql = "INSERT INTO myTable ( FULL_NAME ) VALUES ( $firstname,$middleinitial,$lastname )"; It's not PHP you need to understand the mechanics of, it's SQL. Perhaps this will help you understand the problem INSERT INTO `tablename` (row1, row2, row3) VALUES (content_for_row1, content_for_row2, content_for_row3) The problem you are having with your statement above is you are trying to insert three values into one row, and that is not the syntax for SQL. Like the posters above noted, you will have to join your variables into ONE value in order to insert that into ONE row. Quote Link to comment https://forums.phpfreaks.com/topic/259630-the-mechanics-of-php/#findComment-1330757 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.