所以我在php上使用一个小脚本进行网页管理,然后我做了一个项目注册,所以我得到所有的params并将它们发送到脚本来为数据库构建一个INSERT(mysql )。这是我的代码:
//Getting the params
$title = $_POST["title"];
$date = $_POST["date"];
$hour = $_POST["hour"];
$description = $_POST["description"];
$link = $_POST["link"];
$speaker = $_POST["speaker"];
$site = $_POST["site"];
$file = $_POST["file"];
//Link and File are optional, so I'll be using NULL instead if they're empty
$link = !empty($link) ? ("'".$link."'") : ("'". NULL ."'");
$file = !empty($file) ? ("'".$file."'") : ("'". NULL ."'");
//Now I'm ready to build the query
$query = "INSERT INTO ".$type;
$query = $query . "(title,data,hour,description,link,speaker,site,file)";
$query = $query . "VALUES (";
$query = $query . "'" .$title."'";
$query = $query . ",'".$date."'";
$query = $query . ",'".$hour."'";
$query = $query . ",'".$description."'";
$query = $query . ",".$link;
$query = $query . ",'".$speaker."'";
$query = $query . ",'".$site."'";
$query = $query . ",".$file.")";
//Finally, I'll be sending the INSERT as a query using:
$result = mysql_query($query);
if(!$result)
echo "SQL Error"
所以,我总是进入错误声明。 我在同一网页上的其他脚本中有其他INSERTS,并且它们运行良好,这个模仿它们。我已经查过了:
任何提示都将受到赞赏。
[已解决] 字符串未被转义,因此引号打破了查询。因此,如果您仍然发布此类问题并使用已弃用的mysql _ API,您可能还需要 mysql_escape_string 方法(请检查{{3} })。
答案 0 :(得分:0)
使用这些行。
$link = !empty($link) ? $link : NULL;
$file = !empty($file) ? $file :NULL ;
PHP
中的 null NULL
没有引号也不会在$ link和$ file中使用额外的“”。
在运行query
之前尝试打印并在PhpMyadmin
答案 1 :(得分:0)
您遇到串联问题。尝试使用以下代码更改代码行:
//Link and File are optional, so I'll be using NULL instead if they're empty
$link = !empty($link) ? ($link) : (NULL);
$file = !empty($file) ? ($file) : (NULL);
//Now I'm ready to build the query
$query = "INSERT INTO $type (title,data,hour,description,link,speaker,site,file) VALUES ('" .$title."','".$date."'
,'".$hour."','".$description."','".$link."','".$speaker."','".$site."','".$file."')";
此外,NULL永远不会作为字符串传递。否则,它将被视为 而是一个字符串。