Jump to content

Recommended Posts

 

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?

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/259630-the-mechanics-of-php/
Share on other sites

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.

$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.

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.