没有显示错误,代码也无效。数据库也没有任何变化。当我打开我的页面insert.php并填写详细信息时,只需重置表单。
// insert.php
<html>
<head>
<title>Insert Record</title>
</head>
<body>
<form action="insert.php" method="post">
Name: <input type="text" name="name"><br><p />
Roll No.: <input type="text" name="rollno"><br><p />
Duration: <input type="text" name="duration"><br><p />
Course: <input type="text" name="course"><br><p />
Date of Birth: <input type="text" name="dob"><br><p />
Photo: <input type="file" name="image"><br><p />
<input type="submit" name="submit" value="Submit">
</form>
<?php
error_reporting(0);
//connection
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "vision";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$file = $_FILES["image"]["tmp_name"];
if (!isset($file))
{echo "Please Select an Image";}
else
{
$name = $_POST["name"];
$rollno = $_POST["rollno"];
$duration = $_POST["duration"];
$dob = $_POST["dob"];
$course = $_POST["course"];
$image=addslashes(file_get_contents($_FILES["image"]["tmp_name"]));
//$image_name= addslashes($_FILES["image"]["name"]);
$image_size= getimagesize($_FILES["image"]["tmp_name"]);
//inserting data
if($image_size==FALSE)
{echo "That's not an Image";}
else
{
$sql = mysqli_query($conn,"INSERT INTO record(rollno,name,duration,dob,course,image)VALUES('$rollno','$name','$duration','$dob','$course','$image')");
if($conn->query($sql) == TRUE)
{
echo "<br>Input data is succeed";
}
else
{
echo "<br>Input data is fail";
}
}
}
?>
</body>
</html>
答案 0 :(得分:2)
这是因为当没有图像存在时将不会显示任何图像,并且永远不会有图像。如果要发布图片,则需要添加enctype
代码multipart/form-data
。
更改
<form action="insert.php" method="post">
要
<form action="test.php" enctype="multipart/form-data" method="post">
如果您向表单添加图片,则会显示$sql = mysqli_query($conn,"INS....
行。
答案 1 :(得分:1)
使用: -
<form action="test.php" enctype="multipart/form-data" method="post">