Reputation: 67
I am not able to insert id and name in myTable MySQL table by using following PHP syntax. id is integer field and name is varchar field.
$query="INSERT INTO myTable (id, name) VALUES (".$_SESSION["id"].", ".$_SESSION["name"].");";
Is there something wrong with above syntax? As per me its right because if insert hardcoded values, those are inserted fine.
Upvotes: 1
Views: 61
Reputation: 11065
String should be enclosed in quotes
$query="INSERT INTO myTable (id, name) VALUES (".$_SESSION["id"].", '".$_SESSION["name"]."');";
Upvotes: 2
Reputation: 46900
Put the string value inside quotes:
$query="INSERT INTO myTable (id, name) VALUES (".$_SESSION["id"].", '".$_SESSION["name"]."');";
Upvotes: 2
Reputation: 44474
Yes, you need to use single quotes for name
$query="INSERT INTO myTable (id, name) VALUES (" . $_SESSION["id"] . ", '" . $_SESSION["name"]."');";
Also, please try not to contstruct the queries by hand using string concatenation/substitution. It can be dangerous if your $_SESSION
(somehow) contains content that can manipulate queries completely.
Read about SQL Injection, and what PHP offers.
Upvotes: 3
Reputation: 44851
name
is a reserved word. Put backticks around it. Also, you need quotes around your name variable (and the id, if it is not an integer).
Your query should look like this:
$query="INSERT INTO myTable (id, `name`) VALUES (".$_SESSION["id"].", '".$_SESSION["name"]."')";
Upvotes: 1
Reputation: 2705
use this
$query="INSERT INTO myTable (id, name) VALUES ({$_SESSION["id"]},'{$_SESSION["name"]}');";
Upvotes: 0