简体   繁体   English

SQL 查询在准备好的语句中使用时不起作用,但在 PhpMyAdmin 中起作用

[英]SQL query doesn't work when used in prepared statement but does in PhpMyAdmin

I am writing a News-Website where you can add news if you're logged in. The news are to be sent to a MySQL Database and from there I want them to be displayed on the website.我正在编写一个新闻网站,如果您已登录,您可以在其中添加新闻。新闻将发送到 MySQL 数据库,然后我希望它们显示在网站上。

The problem is that although my prepared statement is beeing executed and all the variables are filled with the right values, the data doesn't get written to the SQL table.问题是,尽管我的准备好的语句正在执行并且所有变量都填充了正确的值,但数据并没有写入 SQL 表。

Using the same query, not as a prepared statement but with the values typed out works correctly.使用相同的查询,而不是作为准备好的语句,但输入的值可以正常工作。

<!DOCTYPE html>
<html>
<head>
<title>News hinzufügen</title>

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<link rel="stylesheet" type="text/css" href="stylesheet.css">

<script src="https://cloud.tinymce.com/5/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>

</head>
<body>
    <form action="add_news.php" method="POST" enctype="multipart/form-data">
        <input type="text" name="title" placeholder="Titel" class="titlestyle" required="required">
        <select name="kategorie" class="kategoriestyle">
            <option value="1">Kategorie 1</option>
            <option value="2">Kategorie 2</option>
            <option value="3">Kategorie 3</option>
        </select>
        Gültig von <input type="date" name="vondate"> bis <input type="date" name="bisdate" required="required">
        <textarea name="news" class="textareastyle" required="required">    
        </textarea>
        <input type="file" name="imageUpload" id="imageUpload">
        <input type="text" name="bildbeschreibung" placeholder="Beschreiben Sie Ihr Bild"> <br>
        <input type="text" name="link" placeholder="Link">
        <input type="text" name="linkbeschreibung" placeholder="Beschreiben Sie Ihren Link" > <br>
        <input type="submit" name="submit" value="Fertigstellen" class="submitstyle">
    </form>
</body>
</html>

The echos are just checkpoints so that I see what was executed and what wasn't.回声只是检查点,以便我查看已执行的内容和未执行的内容。

<?php session_start();  ?>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php
$dbserver = 'localhost';
$dbusername = 'root';
$dbpassword = '';
$dbname = 'm151db';
$titel = "";
$text = "";
$kategorie;
$vondatum = "";
$bidatum = "";
$bild = "";
$bildbeschr = "";
$link = "";
$linkbeschr = "";


$autor = $_SESSION["username"];
$error = "";

if (isset($_POST['submit'])) {

        $conn = mysqli_connect($dbserver, $dbusername, $dbpassword, $dbname);

        if ($conn->connect_error) {
            die('Connection Error: Es gab ein Problem mit dem Verbindungsaufbau. ('.$conn->connect_errno.')'.$conn->connect_error);
        } 


        $stmt = $conn->prepare("INSERT INTO news (name, beschreibung, kategorie_id, von, bis, bild, bildbeschreibung, link, linkbeschreibung, author) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

        $stmt->bind_param("ssssssssss", $titel, $text, $kategorie, $vondatum, $bisdatum, $bild, $bildbeschr, $link, $linkbeschr, $autor);

        if(isset($_POST['title'])) {
            $titel = trim($_POST['title']);
            echo "$titel";

        }

        if(isset($_POST['news'])) {
            $text = trim($_POST['news']);
            echo "$text";
        }

        if(isset($_POST['kategorie'])) {
            $katerorie = $_POST['kategorie'];
            echo "$kategorie";
        }

        if(isset($_POST['vondate'])) {
            $vondatum = $_POST['vondate'];
            echo "$vondatum";
        }

        if(isset($_POST['bisdate'])) {
            $bisdatum = $_POST['bisdate'];
            echo "$bisdatum";
        }

        if(isset($_POST['bildbeschreibung'])) {
            $bildbeschr = trim($_POST['bildbeschreibung']);
            echo "$bildbeschr";
        }

        if(isset($_POST['link'])) {
            $link = trim($_POST['link']);
            echo "$link";
        }

        if(isset($_POST['linkbeschreibung'])) {
            $linkbeschr = trim($_POST['linkbeschreibung']);
            echo "$linkbeschr";
        }






    if(isset($_FILES['imageUpload'])) {

        echo "0";

        $target_dir = "uploads/";

        $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);

        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));



            $check = getimagesize($_FILES["imageUpload"]["tmp_name"]);

            if($check == true) {

                $uploadOk = 1;
                echo "1";
            }

            else {  ?>
                <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Es können nur Bilddateien hochgeladen werden.
                </div>
        <?php       $uploadOk = 0;
            }


        if(file_exists($target_file)) { ?>
            <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Dieses Bild wurde bereits hochgeladen.
                </div>
        <?php   $uploadOk = 0;
        }

        if($_FILES["imageUpload"]["size"] > 500000) {  ?>
            <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Das Bild ist zu gross. Wähle ein Bild unter 500kb aus.
                </div>
        <?php   $uploadOk = 0;
        }

        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") { ?>
            <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Wähle ein PNG-/JPG-/JPEG-Bild aus.
                </div>
        <?php   $uploadOk = 0;
        }

        if($uploadOk == 0) {  ?>
            <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Es ist ein Fehler beim hochladen ihres Bildes aufgetreten. Versuchen sie es erneut.
                </div>
        <?php 
        }
        else {
            if(move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
                //echo "The file has been uploaded.";
            }
            else {   ?>
                <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Es ist ein Fehler beim hochladen ihres Bildes aufgetreten. Versuchen sie es erneut.
                </div>
            <?php }





            $bild = $target_file;
            echo "$bild";
        }


    $stmt->execute();

    if ($stmt == true) {

            echo "stmt executed";
        }

        else {
            echo "stmt not executed";

        }
    }
}

?>

I think you are inserting empty data, so it is not inserting in the table.我认为您正在插入空数据,因此它没有插入表中。 Try defining your variables before:尝试之前定义您的变量:

<?php session_start();  ?>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<?php
$dbserver = 'localhost';
$dbusername = 'root';
$dbpassword = '';
$dbname = 'm151db';
$titel = "";
$text = "";
$kategorie;
$vondatum = "";
$bidatum = "";
$bild = "";
$bildbeschr = "";
$link = "";
$linkbeschr = "";


$autor = $_SESSION["username"];
$error = "";

if (isset($_POST['submit'])) {
        # First define your variables

        if(isset($_POST['title'])) {
            $titel = trim($_POST['title']);
            echo "$titel";

        }

        if(isset($_POST['news'])) {
            $text = trim($_POST['news']);
            echo "$text";
        }

        if(isset($_POST['kategorie'])) {
            $katerorie = $_POST['kategorie'];
            echo "$kategorie";
        }

        if(isset($_POST['vondate'])) {
            $vondatum = $_POST['vondate'];
            echo "$vondatum";
        }

        if(isset($_POST['bisdate'])) {
            $bisdatum = $_POST['bisdate'];
            echo "$bisdatum";
        }

        if(isset($_POST['bildbeschreibung'])) {
            $bildbeschr = trim($_POST['bildbeschreibung']);
            echo "$bildbeschr";
        }

        if(isset($_POST['link'])) {
            $link = trim($_POST['link']);
            echo "$link";
        }

        if(isset($_POST['linkbeschreibung'])) {
            $linkbeschr = trim($_POST['linkbeschreibung']);
            echo "$linkbeschr";
        }

        # then prepare the statement

        $conn = mysqli_connect($dbserver, $dbusername, $dbpassword, $dbname);

        if ($conn->connect_error) {
            die('Connection Error: Es gab ein Problem mit dem Verbindungsaufbau. ('.$conn->connect_errno.')'.$conn->connect_error);
        } 


        $stmt = $conn->prepare("INSERT INTO news (name, beschreibung, kategorie_id, von, bis, bild, bildbeschreibung, link, linkbeschreibung, author) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

        $stmt->bind_param("ssssssssss", $titel, $text, $kategorie, $vondatum, $bisdatum, $bild, $bildbeschr, $link, $linkbeschr, $autor);








    if(isset($_FILES['imageUpload'])) {

        echo "0";

        $target_dir = "uploads/";

        $target_file = $target_dir . basename($_FILES["imageUpload"]["name"]);

        $uploadOk = 1;
        $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));



            $check = getimagesize($_FILES["imageUpload"]["tmp_name"]);

            if($check == true) {

                $uploadOk = 1;
                echo "1";
            }

            else {  ?>
                <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Es können nur Bilddateien hochgeladen werden.
                </div>
        <?php       $uploadOk = 0;
            }


        if(file_exists($target_file)) { ?>
            <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Dieses Bild wurde bereits hochgeladen.
                </div>
        <?php   $uploadOk = 0;
        }

        if($_FILES["imageUpload"]["size"] > 500000) {  ?>
            <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Das Bild ist zu gross. Wähle ein Bild unter 500kb aus.
                </div>
        <?php   $uploadOk = 0;
        }

        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") { ?>
            <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Wähle ein PNG-/JPG-/JPEG-Bild aus.
                </div>
        <?php   $uploadOk = 0;
        }

        if($uploadOk == 0) {  ?>
            <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Es ist ein Fehler beim hochladen ihres Bildes aufgetreten. Versuchen sie es erneut.
                </div>
        <?php 
        }
        else {
            if(move_uploaded_file($_FILES["imageUpload"]["tmp_name"], $target_file)) {
                //echo "The file has been uploaded.";
            }
            else {   ?>
                <div class="alertbox">
                <span class="closebtn"onclick="this.parentElement.style.display='none';">&times;</span>
                Es ist ein Fehler beim hochladen ihres Bildes aufgetreten. Versuchen sie es erneut.
                </div>
            <?php }





            $bild = $target_file;
            echo "$bild";
        }


    $stmt->execute();

    if ($stmt == true) {

            echo "stmt executed";
        }

        else {
            echo "stmt not executed";

        }
    }
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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