Reputation: 53
I have the following MySQL query which needs to be passed to query()
. I'm having trouble understanding it.
INSERT INTO admin (student_name, student_email, student_city) VALUES ('mark','[email protected]','newark');
The place I got the script from has given the following,
$sql = "INSERT INTO students (student_name, student_email, student_city) VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
The part I'm having trouble understanding is ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')
What is happening there? All those inverted commas and periods have got me confused.
Upvotes: 1
Views: 93
Reputation: 11
when you insert a string into Database my sql query, you MUST plus " or ' character
By your issue, the query clause is:
$sql = "INSERT INTO students (student_name, student_email, student_city) VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
The $_POST["stu_name"]
, $_POST["stu_email"]
, $_POST["stu_city"]
are the variables that you received by form with $_POST
method
Best regards, Tuyen
Upvotes: 0
Reputation: 9024
Here the SQL is being concatenated using the .
in PHP.
So, lets take a look at this this:
// 12 3 45678
// vv v vvvvv
('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
'
is to open the MySQL single quote."
ends the string in PHP. .
to join the current PHP string with $_POST['stu_name']
.
"
. '
.,
to enter the second value '
to open a string in MySQL. Then the process repeats itself. Upvotes: 2
Reputation: 35
Simply put a line after the query like this
echo "INSERT INTO students (student_name, student_email, student_city) VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
It will print the SQL query with values. Note the '
in the values. Here you are passing string values in to table, so you use '
and commas to separate the values. Hope this helps you in understanding quickly.
Note: Do not use it on production server. Use it on your local server.
Upvotes: 0
Reputation: 144
if using POST method
$stu_name = $_POST["stu_name"] //mark
$stu_email = $_POST["stu_email"] //[email protected]
$stu_city = $_POST["stu_city"] //newark
$sql = "INSERT INTO students (student_name, student_email, student_city) VALUES ('$stu_name','$stu_email','$stu_city')";
The above is same as
$sql = "INSERT INTO admin (student_name, student_email, student_city) VALUES ('mark','[email protected]','newark')";
Upvotes: 0
Reputation: 1523
Try this, you had issue of quotes only :
["stu_name"] chnaged this to ['stu_name']
$sql = "INSERT INTO students (student_name, student_email, student_city) VALUES ('".$_POST['stu_name']"','".$_POST['stu_email']."','".$_POST['stu_city']."')";
Upvotes: 0
Reputation: 40481
This is to long for a comment:
('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
The whole query need to be warped in double quotes , but when you want to concatenate a variable ->
('".$_POST["stu_name"] <-- this part is leaving the query as
('Value
('".$_POST["stu_name"]."', <-- this part is leaving the query as
('Value',
Each value inside the comma needs to be concatenate into two single quotes on both their sides, hence the single quotes signs. Each dot (.) is concatenating the variable into the existing string and back into the string.
Upvotes: 1