I have never done this before, and this is the first time I have encountered this situation
Here is my code that I created:
$query = mysql_query("INSERT INTO customerinfo (CustomerID, FirstName, LastName, PhoneNumber, PhoneNumber, PhoneNumber, Email, Symptoms) VALUES ('', '{$first}', '{$last}', {$codearea}, {$threedigit}, {$fourdigit}, '{$email}')", $connection);
As you can see, I have table that has 6 columns, which are:
Now, when a client fills out the form, it should insert it into the database. However, if you look at my insert statement, I listed PhoneNumber column 3 times because the form has 3 fields(code area, prefix, and line number) on the HTML page, and I want all three fields insert into the PhoneNumber column. How can I do this?
Thank you!
Concatenate the values into a single string before executing the query.
$phone = $codearea . $threedigit . $fourdigit;
$query = mysql_query("INSERT INTO customerinfo (CustomerID, FirstName, LastName, PhoneNumber, Email, Symptoms) VALUES ('', '{$first}', '{$last}', '{$phone}', '{$email}')", $connection);
PHP mysql_
is deprecated, use http://php.net/pdo instead.
Properly escape your column names and use PDO prepared statements to set bindings.
INSERT INTO `customerinfo` (`CustomerInfo`, `FirstName`, `LastName`) VALUES (:customer_info, :first_name, :last_name);
Once you have this, you can execute the query like this.
$sth = $db->prepare([the query from the above]);
$sth->execute(array('customer_info' => implode('; ', array([data that you want to concatenate])), 'first_name' => 'name', 'last_name' => 'name'));
There is no way to store three values in one field.
You must
You just need to concatenate those with a .
in PHP and insert as a single field. You could do something similar in MySQL but it is a bit more messy.
If you're prompting for separate areacode/prefix/line data in the form, you should keep them separate in the database as well - three separate fields instead of a single "phone number" fields.
However, if that's not possible, then concatenate the values in PHP:
INSERT INTO ... (..., PhoneNumber, ...) VALUES (..., '{$codearea}-{$threedigit}-{$fourdigit}', ...)
You will want to use PHP to merge the 3 HTML form fields together before you insert:
$vPhoneNumber
= $part1 . $part2 . $part3
You can then insert $vPhonenumber
in your SQL.
Use a variable to store all related phone values, like this:
$phoneNumber = $_POST['codearea'] + " " + $_POST['threedigit'] + " " + $_POST['fourdigit'];
Then, change your query to:
INSERT INTO customerinfo
(CustomerInfo, FirstName, LastName, PhoneNumber, Email, Symptoms)
VALUES
('', '{$first}', '{$last}', '{$phoneNumber}', '{$email}')", $connection);
This aproach you're using will difficult your work if you have to present the data from the database to html, in 3 fields. Probably you'll need "explode" the phone string in 3. Would be better if you were using only one html field or 3 collumns in your database table.
$query = mysql_query(
"INSERT INTO customerinfo (
CustomerInfo,
FirstName,
LastName,
PhoneNumber,
Email,
Symptoms
) VALUES (
'',
'".$first."',
'".$last."',
'".$codearea.$threedigit.$fourdigit."',
'".$email."',
'".$symptoms."'
)", $connection);