0

下面是我正在处理的一个 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);
?>
4

1 回答 1

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();

我也是一个新手,发现了这个例子。它略有不同并且有效。

于 2013-11-15T03:15:56.257 回答