php - SQL 查询在准备好的语句中使用时不起作用,但在 PhpMyAdmin 中起作用
问题描述
我正在编写一个新闻网站,如果您已登录,您可以在其中添加新闻。新闻将被发送到 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';">×</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';">×</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';">×</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';">×</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';">×</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';">×</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 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';">×</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';">×</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';">×</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';">×</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';">×</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';">×</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";
}
}
}
?>
推荐阅读
- mongodb - $in 不使用 mongodb 比较 golang 中的所有值
- php - 使用 Laravel Guards 登录后,如何限制用户不访问登录页面?
- android - 如何在另一个应用程序中从 android webview 应用程序打开一个 url
- node.js - 在 Node 中使用 JWT 进行授权
- sql - impala SQL 中的组数
- amazon-ec2 - ECS 随机终止实例并重新启动:Error: No Container Instances were found in your cluster
- amazon-web-services - 我可以创建仅对 VPN 公开的 AWS API Gateway 和 S3 存储桶吗
- eclipse - 如何在 Eclipse 中将视图恢复为默认值?我已经尝试了所有选项,但仍然面临这个问题
- microsoft-teams - 在选项卡更改/页面重新加载时保持 DevTools 打开
- swiftui - SwiftUI willFinishLaunchingWithOptions 有 launchOptions = nil