php 解析错误:语法错误,意外的 '"',需要标识符 (T_STRING) 或变量 (T_VARIABLE) 或数字 (T_NUM_STRING)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41298675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)
提问by Rocky
Parse error: syntax error, unexpected '"', expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\cc_real\3HLR14CMORCGZ0IY8AE7H4JL409RV9\insert.php on line 15.
解析错误:语法错误,意外的“”,在第 15 行的 C:\xampp\htdocs\cc_real\3HLR14CMORCGZ0IY8AE7H4JL409RV9\insert.php 中需要标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)。
Showing me this error, and Below is my code that I am trying to use for inserting data into a database.
向我展示了这个错误,下面是我试图用来将数据插入数据库的代码。
<?php
echo "Invoked!!!";
$con = mysqli_connect('localhost', 'root', '');
if (!$con)
{
die('could not connect:'.mysql_error());
}
mysqli_select_db('job1');
Error: $sql = "INSERT INTO `cc_job2`(cc_Answer_filename,cc_time,cc_workerID) VALUES
('$_post["Answer_filename"]','$_post["track_data"]','$_post["workerID"]')";
$result = mysqli_query($sql)
if ($result){
echo ("<br> Input data is succeed")
} else{
echo("<br> Input data is failed");
}
mysqli_close($con);
?>
采纳答案by Pravin Vavadiya
Just Add ;
end of the line echo ("<br> Input data is succeed")
and $result = mysqli_query($sql)
.
只需添加;
行尾echo ("<br> Input data is succeed")
和$result = mysqli_query($sql)
。
Try to something like this.
尝试这样的事情。
<?php
echo "Invoked!!!";
$con = mysqli_connect('localhost', 'root', '');
if (!$con)
{
die('could not connect:'.mysql_error());
}
mysqli_select_db('job1');
Error: $sql = "INSERT INTO cc_job2(cc_Answer_filename,cc_time,cc_workerID) VALUES ('".$_post['Answer_filename']."','".$_post['track_data']."','".$_post['workerID']."')";
$result = mysqli_query($sql);
if ($result){
echo ("<br> Input data is succeed");
}else{
echo("<br> Input data is failed");
}
mysqli_close($con);
?>
回答by Tolios
There are several issues with this piece of code.
这段代码有几个问题。
- Missing
;
at the end of several lines - Missing the
$con
parameter for mostmysqli_
functions - Mixing
mysql_
andmysqli_
- Using
$_post
instead of$_POST
(capital letters)
- 缺少
;
几行末尾 $con
大多数mysqli_
函数缺少参数- 混合
mysql_
和mysqli_
- 使用
$_post
代替$_POST
(大写字母)
See the comments around the code for what's been changed.
有关更改的内容,请参阅代码周围的注释。
<?php
echo "Invoked!!!";
$con = mysqli_connect('localhost', 'root', '');
/*
You can also do this, and drop mysqli_select_db() later in the code
$con = mysqli_connect('localhost', 'root', '', 'job1');
*/
if (!$con) {
// Cannot mix mysql with mysqli (changed out mysql_error())
// Also, mysqli has "mysqli_connect_error()" for connecting errors
die('could not connect: '.mysqli_connect_error());
}
// This function require the $con parameter
mysqli_select_db($con, 'job1');
// Quotes being messed up - this is your current error
// Concatenate the POST values instead, like this
// Also using $_post instead of $_POST
$sql = "INSERT INTO `cc_job2` (cc_Answer_filename, cc_time, cc_workerID) VALUES ('".$_POST["Answer_filename"]."', '".$_POST["track_data"]."', '".$_POST["workerID"]."')";
// Missing $con as the first parameter and ; at the end
$result = mysqli_query($con, $sql);
if ($result) {
// Missing ; at the end
echo "<br> Input data is succeed";
} else{
echo "<br> Input data is failed";
// You should add echo mysqli_error($con); here to troubleshoot queries
}
mysqli_close($con);
?>
Note that the query will fail if any of the POST-values contains singlequotes '
, so you should either escape them, or better yet, use prepared statements (see the paragraph below and the link "How can I prevent SQL injection in PHP?" at the bottom.
请注意,如果任何 POST 值包含单引号'
,则查询将失败,因此您应该对它们进行转义,或者更好的是,使用准备好的语句(请参阅下面的段落和链接“如何防止 PHP 中的 SQL 注入?”)底部。
This code is also vulnerable to SQL injection, and you should use prepared statements with placeholders to guard yourself against this.
此代码也容易受到 SQL 注入的影响,您应该使用带有占位符的准备好的语句来防止这种情况发生。
See these links
查看这些链接
回答by Dharati Patel
$sql = "INSERT INTO `cc_job2`(cc_Answer_filename,cc_time,cc_workerID) VALUES ('".$_post['Answer_filename']."','".$_post['track_data']."','".$_post['workerID']."')";
回答by Aniket B
Concate in mysql query:
在 mysql 查询中连接:
<?php
echo "Invoked!!!";
$con = mysqli_connect('localhost', 'root', '');
if (!$con)
{
die('could not connect:'.mysql_error());
}
mysqli_select_db('job1');
Error: $sql = "INSERT INTO cc_job2 (cc_Answer_filename,cc_time,cc_workerID) VALUES
('".$_post["Answer_filename"]."','".$_post["track_data"]."','".$_post["workerID"]."')";
$result = mysqli_query($sql)
if ($result){
echo ("<br> Input data is succeed");
}else{
echo("<br> Input data is failed");
}
mysqli_close($con);
?>