1

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.

3
  • 1
    the whole sql string is in double quotes and the values for name, email and city are stored as strings, so they are stored in single quotes and concatenated with the variable. Commented Mar 30, 2016 at 6:07
  • 1
    single quotes before double quotes mean they are saved as string and instead of passing the variable directly inside double quotes, sql string is concatenated with $_POST["stu_name"] Commented Mar 30, 2016 at 6:09
  • Anyway, don't do this. Instead see prepared statements Commented Mar 30, 2016 at 6:59

6 Answers 6

2

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"]."')";
  1. After the bracket, the single quote ' is to open the MySQL single quote.
  2. And then the double quote " ends the string in PHP.
  3. Then, you use PHP . to join the current PHP string with $_POST['stu_name']
  4. And then join it to another PHP string using .
  5. Open a PHP string using double quotes ".
  6. And finally once it's open you need to close the MySQL string you opened using '.
  7. Comma, to enter the second value
  8. A single quote' to open a string in MySQL. Then the process repeats itself.
Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Thanks @sagi. How exactly does '" (single comma followed by a double) change to ' ?
do you understand why a letter followed by a double quotes "a" will turn into a? The same as "'" will turn into ' @innowqhy
0

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']."')";

Comments

0

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')";

1 Comment

He know its the same, he wanted to understand why
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.