语法错误 mysql php
Syntax error mysql php
我的 post 脚本有问题。它给出了一个语法错误,我从昨天开始就找不到了,可能我只是瞎了眼或其他什么。但是错误在哪里呢?
问题:
您的 SQL 语法有误;查看与您的 MySQL 服务器版本相对应的手册,了解在第 1
行的“1”)'附近使用的正确语法
functions.php
function addPost($pName, $pAuthor, $pContent, $pCat = null) {
$query = mysql_query("INSERT INTO cm_posts VALUES(null,'$pName','$pAuthor','$pContent,'$pCat')") or die(mysql_error());
}
doAddPost.php
<?php
include('./includes/functions.php');
if(isset($_POST['submit'])) {
if(isset($_POST['PostName'])) {
if(isset($_POST['PostContent'])) {
addPost($_POST['PostName'], $_POST['PostAuth'], $_POST['PostContent'], $_POST['postCategory']);
header("Location: posts.php");
} else {
echo "Please edit it Marcell";
}
} else {
echo "please set a post name!";
include('addpost.php');
}
} else {
header("Location: addpost.php");
}
?>
addpost.php
<?php include_once("./includes/functions.php"); ?>
<form action="doAddPost.php" method="post">
<table>
<tr>
<td><label for="PostName">Name</label></td><td><input type="text" name="PostName" /></td>
</tr>
<tr>
<td><label for="PostAuth">Ath</label></td><td><input type="text" name="PostAuth" /></td>
</tr>
<tr>
<td><label for="PostContent">Content</label></td>
<td><textarea name="PostContent"></textarea></td>
</tr>
<label for="postCategory">Category</label>
<select class="form-control" name="postCategory">
<?php
$result = mysql_query("SELECT * FROM cm_categories");
while($cat = mysql_fetch_assoc($result)) {
?>
<option value="<?php echo $cat['ID']; ?>"><?php echo $cat['Title']; ?></option>
<?php
}
?>
</select>
<tr>
<td colspan="2"><input type="submit" name="submit" /></td>
</tr>
</table>
</form>
有什么想法吗?
您的 sql 语法中缺少一个简单的引号。
你的:
function addPost($pName, $pAuthor, $pContent, $pCat = null) {
$query = mysql_query("INSERT INTO cm_posts VALUES(null,'$pName','$pAuthor','$pContent,'$pCat')") or die(mysql_error());
}
正确:
function addPost($pName, $pAuthor, $pContent, $pCat = null) {
$query = mysql_query("INSERT INTO cm_posts VALUES(null,'$pName','$pAuthor','$pContent','$pCat')") or die(mysql_error());
}
请注意您的所有数据都从任何简单引号中转义,否则您将成为潜在 mysql 注入的目标。
我的 post 脚本有问题。它给出了一个语法错误,我从昨天开始就找不到了,可能我只是瞎了眼或其他什么。但是错误在哪里呢?
问题:
您的 SQL 语法有误;查看与您的 MySQL 服务器版本相对应的手册,了解在第 1
行的“1”)'附近使用的正确语法functions.php
function addPost($pName, $pAuthor, $pContent, $pCat = null) {
$query = mysql_query("INSERT INTO cm_posts VALUES(null,'$pName','$pAuthor','$pContent,'$pCat')") or die(mysql_error());
}
doAddPost.php
<?php
include('./includes/functions.php');
if(isset($_POST['submit'])) {
if(isset($_POST['PostName'])) {
if(isset($_POST['PostContent'])) {
addPost($_POST['PostName'], $_POST['PostAuth'], $_POST['PostContent'], $_POST['postCategory']);
header("Location: posts.php");
} else {
echo "Please edit it Marcell";
}
} else {
echo "please set a post name!";
include('addpost.php');
}
} else {
header("Location: addpost.php");
}
?>
addpost.php
<?php include_once("./includes/functions.php"); ?>
<form action="doAddPost.php" method="post">
<table>
<tr>
<td><label for="PostName">Name</label></td><td><input type="text" name="PostName" /></td>
</tr>
<tr>
<td><label for="PostAuth">Ath</label></td><td><input type="text" name="PostAuth" /></td>
</tr>
<tr>
<td><label for="PostContent">Content</label></td>
<td><textarea name="PostContent"></textarea></td>
</tr>
<label for="postCategory">Category</label>
<select class="form-control" name="postCategory">
<?php
$result = mysql_query("SELECT * FROM cm_categories");
while($cat = mysql_fetch_assoc($result)) {
?>
<option value="<?php echo $cat['ID']; ?>"><?php echo $cat['Title']; ?></option>
<?php
}
?>
</select>
<tr>
<td colspan="2"><input type="submit" name="submit" /></td>
</tr>
</table>
</form>
有什么想法吗?
您的 sql 语法中缺少一个简单的引号。
你的:
function addPost($pName, $pAuthor, $pContent, $pCat = null) {
$query = mysql_query("INSERT INTO cm_posts VALUES(null,'$pName','$pAuthor','$pContent,'$pCat')") or die(mysql_error());
}
正确:
function addPost($pName, $pAuthor, $pContent, $pCat = null) {
$query = mysql_query("INSERT INTO cm_posts VALUES(null,'$pName','$pAuthor','$pContent','$pCat')") or die(mysql_error());
}
请注意您的所有数据都从任何简单引号中转义,否则您将成为潜在 mysql 注入的目标。