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

问题描述 投票:-2回答:1

我正在写一个新闻网站,你可以在登录时添加新闻。这些新闻将被发送到MySQL数据库,我希望它们能够在网站上显示。

问题是虽然我的预处理语句正在执行,并且所有变量都填充了正确的值,但数据不会写入SQL表。

使用相同的查询,而不是作为预准备的语句,但输入的值可以正常工作。

<!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>

回声只是检查点,以便我看到执行的内容和不执行的内容。

<?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";

        }
    }
}

?>
php mysql
1个回答
1
投票

我认为您正在插入空数据,因此它不会插入表中。尝试之前定义变量:

<?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";

        }
    }
}

?>
© www.soinside.com 2019 - 2024. All rights reserved.