下面是我正在处理的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);
?>
答案 0 :(得分:0)
试试这个:
$mysqli = new mysqli('****', '****', '****', '****');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $mysqli->prepare("INSERT INTO products (prodname, category, description, price, image) VALUES ('$prodname', '$cat', '$des', '$price', '$image')");
$stmt->bind_param('ss', $prodname, $cat, $des, $price, $image);
/* execute prepared statement */
$stmt->execute();
printf("%d Row inserted.\n", $stmt->affected_rows);
$stmt->close();
我也是新手,发现了这个例子。它略有不同,而且很有效。