昨天我将我的上传脚本更新到了mysqli,在解决了一些错误并且无法从我的数据库中获取结果之后,我发现了另一个问题,我无法找出它为什么会发生...
当我上传新的博客文章时,它确实以正确的方式将其上传到我的数据库,但是从第二篇文章开始,我在帖子中输入了一个最终作为空条目。
在我将我的脚本从mysql更新到mysqli之前,这就像一个魅力。
我的猜测是我对查询做错了什么,但我不知道我在这里缺少什么......
提前致谢!
以下是表单和上传脚本的代码部分
<?php
session_start(); /// initialize session
include("important/passwords.php");
check_logged(); /// function checks if visitor is logged. If user is not logged the user is redirected to login.php page
// Start a session for displaying any form errors
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload Multiple Images Using jquery and PHP</title>
</head>
<body>
<div id="maindiv">
<div id="formdiv">
<h2 align="center">Upload en delete Blogs</h2>
<?php
if (isset($_SESSION['error']))
{
echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>";
unset($_SESSION['error']);
}
?>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label>Datum:</label>
<input type="text" name="date" style="width:250px;"/><br />
<label>Blogs:</label>
<textarea name="blog" style="width:250px;height:150px;"></textarea><br /><br />
<input type="submit" value="Upload" name="submit" id="submit" class="upload" />
</p>
</form>
<p>
<form action="delete_multiple.php" method="post" class="textdelete">
Wil je nieuwsberichten van de site halen?
<input type="submit" name="formSubmit" value="Submit" />
</form>
</p>
<p>
<form action="logout.php" method="post" class="textdelete">
<input type="submit" name="formSubmit" value="Logout" />
</form>
</p>
</div>
</div>
</body>
</html>
&#13;
<?php
// Call our connection file
require("includes/conn.php");
$date=$_POST['date'];
$blog=$_POST['blog'];
$query="Insert into blog (date, blog) values ('$date', '$blog')";
mysqli_query($conn, $query) or die ('error updating database');
echo "Het nieuws is geupdate met '$date', '$blog'. De pagina zal over 5 seconden terug naar blogupload gaan.";
header('Refresh: 5; url=blogupload.php');
?>
&#13;
答案 0 :(得分:0)
如果数据包含特殊字符,则需要转义数据。
$date = mysqli_real_escape_string($conn, $_POST['date']);
$blog = mysqli_real_escape_string($conn, $_POST['blog']);
但更好的方法是使用准备好的陈述,然后你不必担心这个。
$query="Insert into blog (date, blog) values (?, ?)";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "ss", $_POST['date'], $_POST['blog']);
mysqli_stmt_execute($stmt) or die(mysqli_error($conn));