我很久以前创建了这段代码我在每天使用后开始注意到它没有输入某些类型的数据。例如,当我从http://en.wikipedia.org/wiki/Pangram复制所有panagrams时,它将无效,它将拉出定义的错误“提交数据时出错”。我没有从PHP本身得到“真正的”错误。为什么这不能正常工作?谢谢您的帮助。
视线变量..
$db = mysqli_connect('localhost','xx','xxx','xxxx') or die('error with connection');
提交系统
session_start();
if(!isset($_SESSION['user_id'])){
header('Location: login.php');
exit();
}
include('../includes/db_connect.php');
if(isset($_POST['submit'])){
$newTitle = $_POST['newTitle'];
$newPost = $_POST['newPost'];
$newPostPreview = $_POST['newPostPreview'];
$newCategory = $_POST['newCategory'];
if(!empty($newPost))
if(!empty($newTitle))
if(!empty($newPostPreview)){
$sql="INSERT INTO posts (title, body, post_preview, category_id, posted)
VALUES('$newTitle', '$newPost', '$newPostPreview', '$newCategory', (now()))";
$query = $db->query($sql);
if($query){
echo "Post entered to database";
}else{
echo "Error Submitting the data";
}
}else{
echo "One Or more Required forms was not filled!";
}
}
textareas和东西
<br><textarea name="newPostPreview" placeholder="Post Preview(Will Be Displayed on the Index Page)" cols="100" rows="10"/></textarea>
<br><textarea name="newPost" placeholder="Post Text" cols="100" rows="10"/></textarea><br>
<input type="submit" name="submit" value="submit"/>
请注意,它只使用纯文本和英文内容工作正常,但有时它无法正常工作(只是普通的英文帖子,其中没有格式化,不提交..)
答案 0 :(得分:1)
当你试图插入具有特殊字符的字符串时,通常会出现这种类型的问题。所以,我想建议在访问post值时使用php函数mysql_real_escape_string()之类的: -
$ newTitle = mysql_real_escape_string($ _ POST ['newTitle']);
$newPost = mysql_real_escape_string($_POST['newPost']);
$newPostPreview = mysql_real_escape_string($_POST['newPostPreview']);
$newCategory = mysql_real_escape_string($_POST['newCategory']);
希望这会起作用
由于