最近,我尝试在数据库中插入一些包含文本和图像的记录。文本插入时没有问题,图像无法进入。我尝试插入的图像并不重。通常只有几kb。我的.php文件有没有出错?
<?php
// php code to Insert data into mysql database from input text
if(isset($_POST['insert'])){
$hostname = "localhost";
$username = "root";
$password = "";
$databaseName = "database1";
// get values form input text and number
$fname = $_POST['fname'];
$contact1 = $_POST['contact1'];
$contact2 = $_POST['contact2'];
$filename = $_FILES['uploadfile']['name'];
$filetmpname = $_FILES['uploadfile']['tmp_name'];
$filename2 = $_FILES['uploadfile2']['name'];
$filetmpname2 = $_FILES['uploadfile2']['tmp_name'];
$folder = 'imagesuploadedf/';
$folder = 'imagesuploadedf2/';
move_uploaded_file($filetmpname, $folder.$filename);
move_uploaded_file($filetmpname2, $folder2.$filename2);
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
// mysql query to insert data
$query = "INSERT INTO `tabela`(`name`, `imagename`, `imagename2`, `contact1`, `contact2`) VALUES ('$fname', '$filename', '$filename2', '$contact1', '$contact2')";
$result = mysqli_query($connect,$query);
// check if mysql query successful
if($result)
{
echo 'Data Inserted';
}
else{
echo 'Data Not Inserted';
}
mysqli_free_result($result);
mysqli_close($connect);
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP INSERT DATA </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="fname" placeholder="First Name"><br><br>
<input type="file" name="uploadfile" ><br><br>
<input type="file" name="uploadfile2" ><br><br>
<input type="text" name="contact1" placeholder="Contact1"><br><br>
<input type="text" name="contact2" placeholder="Contact2"><br><br>
<input type="submit" name="insert" value="Add Data To Database">
</form>
</body>
</html>
2条答案
按热度按时间oo7oh9g91#
请添加
enctype="multipart/form-data"
在表单标签中就像
<form action="index.php" method="post" enctype="multipart/form-data">
kcrjzv8t2#
请试一试
是文件夹,即
imagesuploadedf
以及imagesuploadedf2
创建?在添加到数据库之前,还要小心并清理输入数据。