如何使用模态
中的保存按钮将数据插入phpmyadmin我可以使用什么代码,以便我可以将数据插入来自bootstrap模式的phpmyadmin。非常感谢那些回答我问题的人。
here is the picture of my phpmyadmin tbl_books
<?php
$server = "localhost";
$user = "root";
$pw = "";
$db = "b1_bm";
$conn = mysql_connect($server,$user,$pw);
if(!$conn) {
die('could not connect: ' . mysql_error());
}
mysql_select_db($db, $conn);
?>
table{
border-collapse: collapse;
width: 100%;
}
th, td{
text-align: center;
padding: 8px;
}
tr:nth-child(even) {
background-color: #primary;
}
th{
background-color: #4CAF50;
color: white;
}
#btnsubmit{
background-color: #4CAF50;
color: white;
border: 0px;
width: 60px;
height: 30px;
border-radius: 10px;
cursor:pointer;
}
#txtsearch{
height: 20px;
width: 190px;
}
#form-search{
margin-right: 0px;
margin-left: 1070px;
}
#btnAddBook{
position: absolute;
}
<!DOCTYPE html>
<html>
<head>
<link href = "css/style.css" rel = "stylesheet"/>
<script type = "text/javascript" src = "js/jquery.min.js"></script>
<script type = "text/javascript" src = "js/bootstrap.min.js"></script>
<link href = "css/bootstrap.min.css" rel = "stylesheet"/>
</head>
<body>
<form id="form-search" action = " main.php" method = "post">
<input id="txtsearch" type = "text" name= "search" placeholder ="Enter keyword here..."/>
<input id="btnsubmit" type = "submit" name= "submit" value= "Go"/>
</form>
<hr>
<br>
<div class = "table">
<table>
<tr>
<th>ISBN</th>
<th>Book Title</th>
<th>Book Author</th>
<th>Genre</th>
<th>Year Published</th>
<th>Description</th>
</tr>
<?php
include('connection.php');
if(isset($_POST['submit'])) {
$sql = "Select * from tbl_books where isbn like '$search'";
$result = mysql_query($sql, $conn);
while($row = mysql_fetch_array($result)) {
echo "<tr>
<td> ". $row['isbn'] ."</td>
<td> ". $row['title'] ."</td>
<td> ". $row['author'] ."</td>
<td> ". $row['genre'] ."</td>
<td> ". $row['year_published'] ."</td>
<td> ". $row['description'] ."</td>
</tr>";
}
} else {
$sql = "Select * from tbl_books";
$result = mysql_query($sql, $conn);
while($row = mysql_fetch_array($result)) {
echo "<tr>
<td> ". $row['isbn'] ."</td>
<td> ". $row['title'] ."</td>
<td> ". $row['author'] ."</td>
<td> ". $row['genre'] ."</td>
<td> ". $row['year_published'] ."</td>
<td> ". $row['description'] ."</td>
</tr>";
}
}
mysql_close($conn);
?>
</table>
</div>
<div class = "container">
<button id = "btnAddBook" type = "button" class = "btn btn-success" data-toggle = "modal" data-target = "#myModal">Add Book</button>
<div class = "modal fade" id = "myModal" role="dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<button type = "button" class = "close" data-dismiss = "modal">×</button>
<h4 class = "modal-title">Add Book</h4>
</div>
<div class = "modal-body">
<form role = "form">
<div class = "form-group">
<label for = "exampleInputIsbn">ISBN</label>
<input name = "isbn" type = "text" class = "form-control" placeholder = "Enter ISBN">
</div>
<div class = "form-group">
<label for = "exampleInputTitle">TITLE</label>
<input name = "title" type = "text" class = "form-control" placeholder = "Enter the title of the book">
</div>
<div class = "form-group">
<label for = "exampleInputAuthor">AUTHOR</label>
<input name = "author" type = "text" class = "form-control" placeholder = "Enter the author of the book">
</div>
<div class = "form-group">
<label for = "exampleInputGenre">GENRE</label>
<input name = "genre" type = "text" class = "form-control" placeholder = "Enter the genre">
</div>
<div class = "form-group">
<label for = "exampleInputYearPublished">YEAR PUBLISHED</label>
<input name = "year" type = "text" class = "form-control" placeholder = "Year published">
</div>
<div class = "form-group" method = "post">
<label for = "exampleInputDescription">DESCRIPTION</label>
<input name = "description" type = "text" class = "form-control" placeholder = "Description">
</div>
</form>
</div>
<div class = "modal-footer">
<button name ="save" type = "button" class = "btn btn-success" data-dismiss = "modal">Save</button>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
答案 0 :(得分:1)
这是一个非常简单的帖子形式,这只是为了让您了解如何使用帖子表单将值插入到mySQL数据库中。
<!DOCTYPE html>
<html>
<head>
<link href = "css/style.css" rel = "stylesheet"/>
<script type = "text/javascript" src = "js/jquery.min.js"></script>
<script type = "text/javascript" src = "js/bootstrap.min.js"></script>
<link href = "css/bootstrap.min.css" rel = "stylesheet"/>
</head>
<body>
<form id="form-search" action = " main.php" method = "post">
<input id="txtsearch" type = "text" name= "search" placeholder ="Enter keyword here..."/>
<input id="btnsubmit" type = "submit" name= "submit" value= "Go"/>
</form>
<hr>
<br>
<div class = "table">
<table>
<tr>
<th>ISBN</th>
<th>Book Title</th>
<th>Book Author</th>
<th>Genre</th>
<th>Year Published</th>
<th>Description</th>
</tr>
<?php
if(isset($_POST['submit'])) {
include('connection.php');
$sql = "Select * from tbl_books where isbn like '$search'";
$result = mysql_query($sql, $conn);
while($row = mysql_fetch_array($result)) {
echo "<tr>
<td> ". $row['isbn'] ."</td>
<td> ". $row['title'] ."</td>
<td> ". $row['author'] ."</td>
<td> ". $row['genre'] ."</td>
<td> ". $row['year_published'] ."</td>
<td> ". $row['description'] ."</td>
</tr>";
}
} else {
$sql = "Select * from tbl_books";
$result = mysql_query($sql, $conn);
while($row = mysql_fetch_array($result)) {
echo "<tr>
<td> ". $row['isbn'] ."</td>
<td> ". $row['title'] ."</td>
<td> ". $row['author'] ."</td>
<td> ". $row['genre'] ."</td>
<td> ". $row['year_published'] ."</td>
<td> ". $row['description'] ."</td>
</tr>";
}
}
?>
</table>
</div>
<div class = "container">
<form method="post">
<div class = "form-group">
<label for = "exampleInputIsbn">ISBN</label>
<input name = "isbn" type = "text" class = "form-control" placeholder = "Enter ISBN">
</div>
<div class = "form-group">
<label for = "exampleInputTitle">TITLE</label>
<input name = "title" type = "text" class = "form-control" placeholder = "Enter the title of the book">
</div>
<div class = "form-group">
<label for = "exampleInputAuthor">AUTHOR</label>
<input name = "author" type = "text" class = "form-control" placeholder = "Enter the author of the book">
</div>
<div class = "form-group">
<label for = "exampleInputGenre">GENRE</label>
<input name = "genre" type = "text" class = "form-control" placeholder = "Enter the genre">
</div>
<div class = "form-group">
<label for = "exampleInputYearPublished">YEAR PUBLISHED</label>
<input name = "year_published" type = "text" class = "form-control" placeholder = "Year published">
</div>
<div class = "form-group" method = "post">
<label for = "exampleInputDescription">DESCRIPTION</label>
<input name = "description" type = "text" class = "form-control" placeholder = "Description">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<?php
if (isset($_POST['isbn']) && !empty($_POST['isbn'])) {
$isbn = $_POST['isbn'];
$title = $_POST['title'];
$author = $_POST['author'];
$genre = $_POST['genre'];
$year_published = $_POST['year_published'];
$description = $_POST['description'];
$insert = ("INSERT INTO tbl_books(isbn, title, author, genre, year_published, description)
VALUES ($isbn, $title, $author, $genre, $year_published, $description)");
}
$result = mysql_query($insert, $conn);
?>
</body>