我创建了一个使用PHP和MySQL的表单。这里有人可以输入文本和上传图像。我的图像上传部分正在工作。图像存储在数据库中但文本没有存储在数据库中
我的代码如下:
的index.php
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="text" name="username">
<input type="text" name="report">
Select image to upload:
<input type="file" name="image"/>
<input type="submit" name="submit" value="UPLOAD"/>
</form>
upload.php的
<?php
if(isset($_POST["submit"])){
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false){
$image = $_FILES['image']['tmp_name'];
$imgContent = addslashes(file_get_contents($image));
/*
* Insert image data into database
*/
//DB details
$dbHost = 'localhost';
$dbUsername = 'root';
$dbPassword = 'root';
$dbName = 'testreport';
//Create connection and select DB
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
// Check connection
if($db->connect_error){
die("Connection failed: " . $db->connect_error);
}
$username = mysqli_real_escape_string($link, $_REQUEST['username']);
$report = mysqli_real_escape_string($link, $_REQUEST['report']);
$dataTime = date("Y-m-d H:i:s");
//Insert image content into database
$insert = $db->query("INSERT into images (username,report,image, created) VALUES ('$username','$report','$imgContent', '$dataTime')");
if($insert){
echo "File uploaded successfully.";
}else{
echo "File upload failed, please try again.";
}
}else{
echo "Please select an image file to upload.";
}
}
?>
答案 0 :(得分:0)
我认为它应该是$ _POST [''],因为表单方法是'post' 所以它将是:
$username = mysqli_real_escape_string($_POST['username']);
$report = mysqli_real_escape_string($_POST['report']);
答案 1 :(得分:0)
你必须为mysqli_real_escape_string()
定义这样的链接$link = mysqli_connect("localhost","root","root","testreport");
答案 2 :(得分:0)
尝试以下代码
$username = mysqli_real_escape_string($db, $_POST['username']);
$report = mysqli_real_escape_string($db, $_POST['report']);
$dataTime = date("Y-m-d H:i:s");
当您使用mysqli_real_escape_string时,您需要在第一个参数中传递mysqli对象。 mysqli_real_escape_string