gpt4 book ai didi

PHP从数组中插入数据到数据库中并在html表中输出数据库数据

转载 作者:行者123 更新时间:2023-11-29 18:18:43 25 4
gpt4 key购买 nike

我在从数组向 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>";
}
}

另外,如果您希望零售值(value)小于库存值(value),则检查零售值(value)错误的条件是错误的。请仔细检查

关于PHP从数组中插入数据到数据库中并在html表中输出数据库数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46779866/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com