我试图制作一个表单,它会将图书添加到图书馆书籍的数据库中,但对于我的生活,我无法使其工作。这里是我的代码:无法使用PHP提交表单正确提交
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h2 class="page-header">Add Books</h2>
<br>
<?php
if (isset($_POST['submit'])){
$host="*****"; // Host name
$username="*****"; // Mysql username
$password="******"; // Mysql password
$db_name="******"; // Database name
$conn = mysqli_connect($host, $username, $password, $db_name);
$name=$_POST['Name'];
$author=$_POST['Author'];
$published=$_POST['Published'];
$isbn=$_POST['ISBN'];
$category=$_POST['Category'];
$sql="INSERT INTO Library (Name, Author, Published, ISBN, Category)
VALUES('$name', '$author', '$published', '$isbn', '$category')";
$query = mysqli_query($conn,$sql);
if($query){
echo "Successfully Added!";
}else{
echo "Error"."<br />".$sql.mysqli_error($con);
}
}
?>
<br>
<form name="addBookForm" action="" method="post">
<fieldset class="form-group">
<label for="frmName">Book Name</label>
<input type="text" class="form-control" id="frmName" placeholder="Name" name="Name">
</fieldset>
<fieldset class="form-group">
<label for="frmAuthor">Author</label>
<input type="text" class="form-control" id="frmAuthor" placeholder="Author" name="Author">
</fieldset>
<fieldset class="form-group">
<label for="frmPublished">Published</label>
<input type="text" class="form-control" id="frmPublished" placeholder="Published" name="Published">
</fieldset>
<fieldset class="form-group">
<label for="frmISBN">ISBN</label>
<input type="text" class="form-control" id="frmISBN" placeholder="ISBN" name="ISBN">
</fieldset>
<fieldset class="form-group">
<label for="frmCategory">Category</label>
<select class="form-control" id="frmCategory" name="Category">
<option value="Politics/Philosophy">Politics/Philosophy</option>
<option value="History/Labour History">History/Labour History</option>
</select>
</fieldset>
<button type="submit" class="btn btn-secondary">Add Book</button>
</form>
</div>
</div>
</div>
我一直在撞墙我的头一整天,有人能够告诉我我做错了什么?当我按提交按钮时,它只是回发到页面,没有任何成功或错误消息,当我检查数据库时,未插入数据。这就像它只是刷新页面。
告诉我们发生了什么问题。 –
你很容易受到[sql注入攻击](http://bobby-tables.com)的攻击,并且你的表单中没有'submit'元素,所以你的$ _POST ['submit']'检查总是会返回假,无论如何跳过所有的DB代码。 –
试试看 - > if($ _ SERVER ['REQUEST_METHOD'] =='POST){< - 而不是 - > if(isset($ _ POST ['submit'])){ – IceJOKER