【发布时间】:2015-10-09 11:40:58
【问题描述】:
我的 MySQL 查询应该插入来自表单的数据输入。没有给出错误。我认为这段代码绝对没有错。我之前已将此代码用于其他表,并且可以正常工作。请忽略 isset 进行更新。那是为了别的东西。 writerID、illustratorID 和 publisherName 是来自不同表的外键。会是这个问题吗?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "graphicnoveldb";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// get results from database
if(isset($_POST['update'])){
$UpdateQuery = "Update graphicnovel Set
graphicNovelTitle='$_POST[graphicnoveltitle]',
genre='$_POST[genre]',
synopsis='$_POST[synopsis]',
review='$_POST[review]',
rating='$_POST[rating]',
writerID='$_POST[writerID]',
illustratorID='$_POST[illustratorID]',
coverPage='$_POST[coverpage]'
Where graphicNovelTitle='$_POST[hidden]'";
mysqli_query($conn,$UpdateQuery);
};
if(isset($_POST['add'])){
$AddQuery = "Insert into graphicnovel (graphicNovelTitle, genre, synopsis, review, rating, writerID, illustratorID, publisherName, datePublished, coverPage)
Values('$_POST[ugraphicnoveltitle]',
'$_POST[ugenre]',
'$_POST[usynopsis]',
'$_POST[ureview]',
'$_POST[urating]',
'$_POST[uwriterID]',
'$_POST[uillustratorID]',
'$_POST[upublishername]',
'$_POST[udatepublished]',
'$_POST[ucoverpage]')";
mysqli_query($conn,$AddQuery);
};
$sql="SELECT * FROM graphicnovel";
$result = mysqli_query($conn,$sql);
echo "<table>";
echo "<form action = ModifyGraphicNovel.php method =post>";
echo"<tr><th>New Graphic Novel Title</th>";
echo '<td><input type=text name= ugraphicnoveltitle></td></tr>';
echo"<tr><th>New Genre</th>";
echo '<td><input type=text name= ugenre></td></tr>';
echo"<tr><th>New Synopsis</th>";
echo '<td><textarea name= usynopsis rows = 5 cols = 70 border =5 value=></textarea></tr>';
echo"<tr><th>New Review</th>";
echo '<td><textarea name= ureview rows = 5 cols = 70 border =5 value=></textarea></tr>';
echo"<tr><th>New Rating</th>";
echo '<td><input type=text name= urating></td></tr>';
echo"<tr><th>New Writer ID</th>";
echo '<td><input type=text name= uwriterID></td></tr>';
echo"<tr><th>New Illustrator ID</th>";
echo '<td><input type=text name= uillustratorID></td></tr>';
echo"<tr><th>New Publisher Name</th>";
echo '<td><input type=text name= upublishername></td></tr>';
echo"<tr><th>New Date Published</th>";
echo '<td><input type=date name= udatepublished></td></tr>';
echo"<tr><th>New Coverpage</th>";
echo '<td><input type=text name= ucoverpage></td></tr>';
echo '<tr><td>' . "<input type = submit name=add value=add" . ' </td></tr>';
echo "</form>";
echo "</table>";
// close table>
$conn->close();
?>
【问题讨论】:
-
"I see absolutely nothing wrong this code"- 你的意思是除了明显的 SQL 注入漏洞和完全缺乏错误检查? -
@David
if ($conn->connect_error) { die-- 嗯,不是完整,但无论如何...... -
@AlanMachado:确实,OP 确实会检查数据库交互中最不可能失败的错误。但是在执行任何用户想要执行的任何代码时,从不检查错误。
-
@David 是的,我给你。然后,php.ini 有可能使错误不屈不挠。
-
你知道我可以用于 $AddQuery 的检查吗?以及如何?
标签: php html mysql database forms