下面是我正在处理的一个 php 脚本,但我认为它缺少一些东西。它不会向数据库写入任何内容。我想将图像路径而不是图像上传到数据库。我是新手,所以任何帮助将不胜感激。
<?php
$target = "uploads/";
$target = $target . basename($_FILES['uploaded']['name']);
$ok=1;
//Size condition
if ($uploaded_size > 350000) {
echo "Your file is too large.<br>";
$ok=0;
}
//Limit file type condition
if ($uploaded_type =="text/php") {
echo "No PHP files<br>";
$ok=0;
}
//Check that $ok was not set to 0 by an error
if ($ok==0) {
echo "Sorry your file was not uploaded";
}
//If everything is ok try to upload it
else {
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
}
if(isset($_POST['submit'])) {
// Create connection
$con=mysqli_connect("localhost","****","****","*****");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$prodname = $_POST['prodname'];
$cat = $_POST['category'];
$des = $_POST['description'];
$price = $_POST['price'];
$image = "uploads/" . basename($_FILES['uploaded']['name']);
$query="INSERT INTO products (prodname, category, description, price, image) VALUES ('$prodname', '$cat', '$des', '$price', '$image')";
$statement = $con->prepare($query);
$statement->bind_param('ss', $prodname, $cat, $des, $price, $image);
$statement->execute();
$statement->store_result();
}
$result = mysql_query($query);
if($result) {
echo("<br>Product upload is successful");
}
else {
echo("<br>Product upload failed");
}
mysql_close($con);
?>