gpt4 book ai didi

php - 选择框多次插入值

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

<?php
require '../common/connect-db.php';
require_once 'admin-template.php';
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php
// add button
if(isset($_POST['add_dayoff'])) {
$AddQuery = "INSERT INTO dayoff (fname,lname,datef)
VALUES ('$_POST[select1]')";
mysqli_query($db,$AddQuery);
};
?>
<?php
$query="SELECT DISTINCT fname, lname FROM employees";
$result = $db->query($query);
?>
<table><caption>Dayoff Table</caption>
<tr>
<th>First Name</th>
<th>Employee Dayoff</th>
</tr>
<tr>
<td><select name="select1" id="select1" >
<?php
echo '<option value="">Please select...</option>';
while ($row = mysqli_fetch_array($result)) {
echo "<option value='" . $row['fname'] . $row['lname'] . "'>" . $row['fname'] ." ". $row['lname'] . "</option>";
}
?>

<td><input type="date" name="date" required /></td>
</select></td>
<td><input type="submit" name="add_dayoff" /></td>

</tr>
</table>

主键是employees表中的email,我知道这不是一种安全的方法,我应该使用准备语句,但这只是一个小项目。 'dayoff' 有一个自动递增的 int 'id' 作为主键。

所以发生的情况是所有记录都被插入多次。并且一些记录fnamelname被连接并存储在dayoff

fname

最佳答案

您有语法错误

INSERT INTO dayoff (fname,lname,datef) VALUES ($_POST[select1]).$_POST[select1] will contains fnamelname.

此外,您没有获得日期即:$_post['date']。尝试一下,让我知道结果

  <?php
require_once 'admin-template.php';
?>
<form id="form1" name="form1" method="post" action="test.php">
<?php
// add button
if(isset($_POST['add_dayoff'])) {
$fname=$_POST['select1'];// get the value which stores firstname-lastname
$n=explode('-',$fname);//explode the value with -
$datef=$_POST['date'];
$AddQuery = "INSERT INTO dayoff (fname,lname,datef) VALUES ('$n[0]','$n[1]',$datef)";// query should be like this
mysqli_query($db,$AddQuery);
};
?>
<?php
$query="SELECT DISTINCT fname, lname FROM employees";
$result = $db->query($query);
?>
<table><caption>Dayoff Table</caption>
<tr>
<th>First Name</th>
<th>Employee Dayoff</th>
</tr>
<tr>
<td><select name="select1" id="select1" >
<?php
echo '<option value="">Please select...</option>';
while ($row = mysqli_fetch_array($result)) {
echo "<option value='". $row['fname']."-".$row['lname'] . "'>" . $row['fname'] ." ". $row['lname'] . "</option>";
}
?>
<td><input type="date" name="date" required /></td>
</select></td>
<td><input type="submit" name="add_dayoff" /></td>
</tr>
</table>

关于php - 选择框多次插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34120017/

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