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 に答える