innowqhy
innowqhy

Reputation: 53

Change this SQL query to php

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

Answers (6)

Tuyen Nguyuen
Tuyen Nguyuen

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

Ikhlak S.
Ikhlak S.

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

Upvotes: 2

KKumar
KKumar

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

J. Shiv
J. Shiv

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

Nehal
Nehal

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

sagi
sagi

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

Related Questions