我在从数组中插入MySQL数据库表中的数据时遇到问题。我有一个表单和一个数组,用于存储提交的值及其内容,我显示在 html 表中。在每个表行中,都有一个来自一个提交的值和一个使用 jQuery 删除该行的按钮。问题是我还需要从 html 表中选择一行并插入到数据库表中的功能。我想要每行末端的按钮,单击该按钮时会将行内容插入我的数据库表中,类似于我现在从 html 表中删除一行。删除可以通过简单的jQuery完成,但是有了这个我不知道如何继续,提前感谢您的回答。
这就是我所走多远的程度:
<div class="row">
<div class="col-7"> <!-- array table col -->
<div class="arraytable" style="margin-left: 15px; margin-top:15px;">
<table id="arraytable" class="table table-hover">
<tbody>
<thead>
<tr>
<th></th>
<th>Code:</th>
<th>Title:</th>
<th>Inventory nr.:</th>
<th>Inventory value.:</th>
<th>Retail value.:</th>
<th></th>
<th></th>
</tr>
</thead>
<?php
session_start();
$code = $title = $number = $value = $retailValue = "";
$code_err = $number_err = $value_err = $title_err = $retailValue_err = "";
if( isset($_POST["add"]) ){
if( empty(trim($_POST["code"])) ){
$code_err = "Enter code.";
}
else{
$code = trim($_POST["code"]);
}
if ( empty(trim($_POST["title"])) ) {
$title_err = "Enter title.";
}
else {
$title = trim($_POST["title"]);
}
if ( empty(trim($_POST["number"])) || !is_numeric($_POST["number"]) ) {
$number_err = "Inventory nr. must be entered, must be numeric";
}
else{
$number = trim($_POST["number"]);
}
if ( empty(trim($_POST["value"])) || !is_numeric($_POST["value"]) ) {
$value_err = "Inventory value must be entered, must be numeric";
}
else {
$value = trim($_POST["value"]);
}
if ( empty(trim($_POST["retailvalue"])) || !is_numeric($_POST["retailvalue"]) || $_POST["retailvalue"] < $_POST["value"] ) {
$retailValue_err = " Retail value must be entered, must be numeric. Must be smaller than inventory value.";
}
else {
$retailValue = trim($_POST["retailvalue"]);
}
if(empty($code_err) && empty($number_err) && empty($value_err) && empty($title_err) && empty($retailValue_err)) {
$_SESSION['info'][] = array($code, $title, $number, $value, $retailValue);
if(isset($_SESSION['info'])) {
for($i = 0; $i < count($_SESSION['info']); $i++) {
echo "<tr> <td></td>";
foreach($_SESSION['info'][$i] as $key){
echo " <td>$key</td>";
}
echo "<td><a class="remove" href=""> <i class="fa fa-trash" aria-hidden="true"></i> </a></td> ";
echo "</tr>";
}
}
}
}
?>
</tbody>
</table>
</div>
</div> <!-- end arraytable col -->
<div class="col-3" style="margin-left:15px; margin-top:15px;"> <!-- form colum -->
<form name="form" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method = "POST">
<div class="form-group <?php echo (!empty($code_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Code:" id="code" name="code" class="form-control" value="<?php echo $code; ?>"/>
<span class="help-block"><?php echo $code_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($title_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Title:" id="title" name="title" class="form-control" value="<?php echo $title; ?>" />
<span class="help-block"><?php echo $title_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($number_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Inventory nr:" id="number" name="number" class="form-control" value="<?php echo $number; ?>" />
<span class="help-block"><?php echo $number_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($value_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Inventory value:" id="value" name="value" class="form-control" value="<?php echo $value; ?>" />
<span class="help-block"><?php echo $value_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($retailValue_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Retail value:" id="retailvalue" name="retailvalue" class="form-control" value="<?php echo $retailValue; ?>" />
<span class="help-block"><?php echo $retailValue_err; ?></span>
</div>
<div class="form-group text-center">
<input type="submit" class="btn btn-primary" name="add" id="add" value="Add" >
</div>
</form>
</div> <!-- end form colum -->
</div> <!-- end array table, form row -->
<script>
$('#arraytable').on('click','tr a.remove',function(e){
e.preventDefault();
$(this).closest('tr').remove();
});
</script>
你可能想要这样的东西
if(empty($code_err) && empty($number_err) && empty($value_err) && empty($title_err) && empty($retailValue_err)) {
// insert all inputs to an a new array
$newInfo = array($_POST['code'], $_POST['title'], $_POST['number'], $_POST['value'], $_POST['retailvalue']);
// push the new array to session variable 'info'
array_push($_SESSION['info'], $newInfo);
// as you already have, loop thru each session info
for($i = 0; $i < count($_SESSION['info']); $i++) {
echo "<tr><td></td>";
// echo each value to table cells
foreach($_SESSION['info'][$i] as $value){
echo "<td>".$value."</td>";
}
echo "<td><a class="remove" href=""> <i class="fa fa-trash"></i>Delete</a></td> ";
echo "</tr>";
}
}
另外,如果您希望零售价值小于库存价值,则检查零售价值错误的条件是错误的。请仔细检查
我决定使用Ajax将数据发布到数据库。你可以看到我使用名为postToDatabase的javascript方法来发布数据,并且数据已作为参数传递给该方法。
查看下面的代码。
<?php
// your database connection and database selection using PDO ( Safe and better to use PDO)
//BEGINNING OF DATABASE CONNECTION
$servername = "localhost";
$username = "root";
$password = "password";
$db_name = "db";
$db_engine = 'mysql';
try {
$conn = new PDO("$db_engine:host=$servername;dbname=$db_name", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{ echo "Connection failed: " . $e->getMessage(); }
// END OF DATABSE CONNECTION
//DATA POSTED VIA AJAX
$code = trim($_POST["code"]);
$title = trim($_POST["title"]);
$number = trim($_POST["number"]);
$value = trim($_POST["value"]);
$retailValue = trim($_POST["retailvalue"]);
// BEGINNING OF INSERT QUERY
$sql = $conn ->prepare("INSERT INTO books (code, title, number, value, retailvalue) VALUES (?, ?, ?, ?, ?)"); // ? WILL BE REPLACED BY THEIR RESPECTIVE VALUES IN THE EXEC() METHOD.
$sql->execute(array($code, $title, $number, $value, $retailValue));
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet" type="text/css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div id="success_msg" class="alert alert-success fade in" style="display:none;">
<a href="#" class="close" data-dismiss="alert">×</a>
<strong>Success!</strong> Data Saved.
</div>
<div class="col-7"> <!-- array table col -->
<div class="arraytable" style="margin-left: 15px; margin-top:15px;">
<form name="form">
<table id="arraytable" class="table table-hover">
<tbody>
<thead>
<tr>
<th></th>
<th>Code:</th>
<th>Title:</th>
<th>Inventory nr.:</th>
<th>Inventory value.:</th>
<th>Retail value.:</th>
<th></th>
<th></th>
</tr>
</thead>
<?php
session_start();
$code = $title = $number = $value = $retailValue = "";
$code_err = $number_err = $value_err = $title_err = $retailValue_err = "";
if( isset($_POST["add"]) ){
if( empty(trim($_POST["code"])) ){
$code_err = "Enter code.";
}
else{
$code = trim($_POST["code"]);
}
if ( empty(trim($_POST["title"])) ) {
$title_err = "Enter title.";
}
else {
$title = trim($_POST["title"]);
}
if ( empty(trim($_POST["number"])) || !is_numeric($_POST["number"]) ) {
$number_err = "Inventory nr. must be entered, must be numeric";
}
else{
$number = trim($_POST["number"]);
}
if ( empty(trim($_POST["value"])) || !is_numeric($_POST["value"]) ) {
$value_err = "Inventory value must be entered, must be numeric";
}
else {
$value = trim($_POST["value"]);
}
if ( empty(trim($_POST["retailvalue"])) || !is_numeric($_POST["retailvalue"]) || $_POST["retailvalue"] < $_POST["value"] ) {
$retailValue_err = " Retail value must be entered, must be numeric. Must be smaller than inventory value.";
}
else {
$retailValue = trim($_POST["retailvalue"]);
}
if(empty($code_err) && empty($number_err) && empty($value_err) && empty($title_err) && empty($retailValue_err)) {
$_SESSION['info'][] = array($code, $title, $number, $value, $retailValue);
if(isset($_SESSION['info'])) {
for($i = 0; $i < count($_SESSION['info']); $i++) {
echo "<tr> <td></td>";
foreach($_SESSION['info'][$i] as $key){
echo " <td>$key</td>";
$vals .= "'$key',";
}
echo "<td><a class="remove" href=""> <i class="fa fa-trash" aria-hidden="true"></i> </a></td> "; ?>
<td>
<a onclick="postToDatabase(<?php echo substr($vals,0,-1); $vals=NULL; ?>);" class="add" href="#"> <i class="fa fa-plus" aria-hidden="true"></i>
</a>
</td>
<?php echo "</tr>";
}
}
}
}
?>
</tbody>
</table>
</form>
</div>
</div> <!-- end arraytable col -->
<div class="col-3" style="margin-left:15px; margin-top:15px;"> <!-- form colum -->
<form name="form" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method = "POST">
<div class="form-group <?php echo (!empty($code_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Code:" id="code" name="code" class="form-control" value="<?php echo $code; ?>"/>
<span class="help-block"><?php echo $code_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($title_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Title:" id="title" name="title" class="form-control" value="<?php echo $title; ?>" />
<span class="help-block"><?php echo $title_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($number_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Inventory nr:" id="number" name="number" class="form-control" value="<?php echo $number; ?>" />
<span class="help-block"><?php echo $number_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($value_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Inventory value:" id="value" name="value" class="form-control" value="<?php echo $value; ?>" />
<span class="help-block"><?php echo $value_err; ?></span>
</div>
<div class="form-group <?php echo (!empty($retailValue_err)) ? 'has-error' : ''; ?>">
<input type="text" placeholder="Retail value:" id="retailvalue" name="retailvalue" class="form-control" value="<?php echo $retailValue; ?>" />
<span class="help-block"><?php echo $retailValue_err; ?></span>
</div>
<div class="form-group text-center">
<input type="submit" class="btn btn-primary" name="add" id="add" value="Add" >
</div>
</form>
</div> <!-- end form colum -->
</div> <!-- end array table, form row -->
</div>
<script>
$('#arraytable').on('click','tr a.remove',function(e){
e.preventDefault();
$(this).closest('tr').remove();
});
function postToDatabase(code, title,number,value,retailvalue){
$.ajax({
type: "POST",
url: 'index.php',
data: "code=" + code + "&title=" + title + "&number=" + number + "&value=" + value + "&retailvalue=" + retailvalue,
success: function (msg) {
$('#success_msg').show();
}
});
}
</script>
</body>
</html>