开发者

Is there a template to insert querys?

开发者 https://www.devze.com 2023-03-30 20:56 出处:网络
I want to insert a few certain values from a php file, to my My开发者_运维技巧SQL database, using a query.

I want to insert a few certain values from a php file, to my My开发者_运维技巧SQL database, using a query. I use the following code snippet:

mysql_query("INSERT INTO `text` VALUES ('', '$user_id', '$text', '"$categories"')");

but I get an error saying the following:

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\Project\Func\idea.func.php on line 10

Does anyone know what I'm doing wrong?

I have stated both variables, earlier, by making them into real escape strings. My MySQL table structure is as follows:

idea_id (auto)
user_id 
text
categories
timestamp


You need to use string concatenation:

mysql_query("INSERT INTO `text` VALUES ('', '$user_id', '$text', '" . $categories . "')");

or get rid of the double quotes surrounding $categories:

mysql_query("INSERT INTO `text` VALUES ('', '$user_id', '$text', '$categories')");


I would prefer

mysql_query(sprintf(   
  "INSERT INTO `text` VALUES ('', '%s', '%s', '%s')",
  mysql_real_escape_string($user_id),
  mysql_real_escape_string($text),
  mysql_real_escape_string($categories)) 
);


Change '"$categories"')") to '" . $categories . "')".

You need to put fullstops between the variables to tell PHP that you want to concatenate (join) all of it together as a string.


Point is very important in this case it serves to separate the two variables in this case. If there is no point return error

0

精彩评论

暂无评论...
验证码 换一张
取 消