简体   繁体   中英

Inserted data are not going to my database

I am trying to upload a image in my website with some data. The image that I am trying to upload is being uploaded in my image folder but the data is not going in by database .And it's showing an error in mysqli_stmt_bind_param . I couldn't figure out an error. can someone please help me ?

if (isset($_POST['submit1'])) {
    $cakename = $_POST['cakeName'];

    $Beschreibung = $_POST['beschreibung'];
    $Preis = $_POST['preis'];

    $file = $_FILES['image'];

    $fileName = $file["name"];
    $filetype = $file["type"];
    $fileTempName = $file["tmp_name"];
    $fileError = $file["error"];
    $fileSize = $file["size"];

    $fileExt = explode(".", $fileName);
    $fileActualExt = strtolower(end($fileExt));

    $allowed = array("jpg", "jpeg", "png");

    if (in_array($fileActualExt, $allowed)) {
        if ($fileError === 0) {
            if ($fileSize < 2000000) {
                $imageFullName = $cakename.".". uniqid("", true).".". $fileActualExt;
                $fileDestination = "../Bake_my_cake1/image/".$imageFullName;

                include_once 'lib/db_connector.php';
                $db = dbconnect();

                if (empty($Beschreibung) || empty($Preis)) {
                    header("Location: ../Bake_my_cake1/Profile_baker1.php?upload=empty");
                    exit();
                } else {
                    $sql = "SELECT * FROM Cake;";
                    $stmt = mysqli_stmt_init($db);
                    if (!mysqli_stmt_prepare($stmt, $sql)) {
                        echo "SQL statement failed 1";
                    } else {
                        mysqli_stmt_execute($stmt);
                        $result = mysqli_stmt_get_result($stmt);
                        $rowCount = mysqli_num_rows($result);
                        $setImageOrder = $rowCount + 1;

                        $sql = "INSERT INTO Cake (CakeName, Beschreibung, Preis, orderGallery) VALUES(?, ?, ?, ?);";

                        if (!mysqli_stmt_prepare($stmt, $sql)) {
                            echo "SQL statement failed 2";
                        } else {
                            mysqli_stmt_bind_param($stmt, "ssss", $imageFullName, $Beschreibung, $Preis, $setImageOrder);
                            mysqli_stmt_execute($stmt);
                            echo "error 1";
                            move_uploaded_file($fileTempName, $fileDestination);

                            header("Location ../Bake_my_cake1/Profile_baker1.php?upload=sucess");
                        }
                    }
                }
                dbclose($db);
            } else {
                echo"File Size is too Big";
                exit();
            }
        } else {
            echo"You have an error";
            exit();
        }
    } else {
        echo"You need to upload a proper file type";
        exit();
    }
}

?>

else{
    mysqli_stmt_bind_param($stmt,"ssss", $imageFullName, $Beschreibung, $Preis, $setImageOrder);
    mysqli_stmt_execute($stmt);
    echo "error 1";
    move_uploaded_file($fileTempName,$fileDestination);

    header("Location ../Bake_my_cake1/Profile_baker1.php?upload=sucess");

it is showing error 1 .

according to your variable name i'm assuming that your function dbconnect() returns database object. And you're passing that $db in mysqli_stmt_init instead of DB connection object.

ex.

If you are returning database connection object and storing it in $con then your code will be look like $stmt = mysqli_stmt_init($con) .

Using this code you will not see "SQL statement failed 1" message and your code will be executed as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM