gpt4 book ai didi

php - 如何使用 PHP 的强大功能将多个输入插入到数据库中?

转载 作者:行者123 更新时间:2023-12-04 18:01:35 25 4
gpt4 key购买 nike

我有一个重复的 html 表单输入,现在这里的问题是我无法将它们全部插入到数据库中。我正在使用 foreach 但无法插入其他字段。

这是我的代码:

<table><!---one name and date but has many products--->
<tr>
<td>Name</td>
<td><input type="text" name="name_txt[]" /></td>
</tr>
<tr>
<td>Date</td>
<td><input type="date" name="date_txt[]" /></td>
</tr>


<tr>
<td>Product</td>
<td><input type="text" name="product_name_txt[]" /></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="description_txt[]" /></td>
</tr>
<tr>
<td>Date Purchased</td>
<td><input type="date" name="date_txt[]" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity_txt[]" /></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price_txt[]" /></td>
</tr>

<tr>
<td><br><br></td>
</tr>

<tr>
<td>Product</td>
<td><input type="text" name="product_name_txt[]" /></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="description_txt[]" /></td>
</tr>
<tr>
<td>Date Purchased</td>
<td><input type="date" name="date_txt[]" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity_txt[]" /></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price_txt[]" /></td>
</tr>

<tr>
<td><br><br></td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="add" />
<input type="reset" name="submit" value="clear" /></td>
</tr>

</table>

这是我添加插入数据库的 php 代码:

<?php   

if (isset($_POST['product_name_txt']))
{
include 'db.php';

foreach($_POST['product_name_txt'] as $row => $value){
$product=$_POST['product_name_txt'];
$description=$_POST['description_txt'];
$date=$_POST['date_txt'];
$quantity=$_POST['quantity_txt'];
$price=$_POST['price_txt'];

$sql = "INSERT INTO products (product_name,description,date,quantity,price)
VALUES ('$value','','','','')";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}

?>

最佳答案

你的 db.php 文件应该包含这样的代码

$conn = mysqli_connect("your_localhost", "your_username", "your_password", "your_database_name");

现在让我们来看看您的 HTML 和 PHP 代码。在下面检查我的代码。

<form method="POST" action="">
<table>
<tr>
<td>Product</td>
<td><input type="text" name="product_name_txt[]" /></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="description_txt[]" /></td>
</tr>
<tr>
<td>Date Purchased</td>
<td><input type="date" name="date_txt[]" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity_txt[]" /></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price_txt[]" /></td>
</tr>

<tr>
<td><br><br></td>
</tr>

<tr>
<td>Product</td>
<td><input type="text" name="product_name_txt[]" /></td>
</tr>
<tr>
<td>Description</td>
<td><input type="text" name="description_txt[]" /></td>
</tr>
<tr>
<td>Date Purchased</td>
<td><input type="date" name="date_txt[]" /></td>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="quantity_txt[]" /></td>
</tr>
<tr>
<td>Price</td>
<td><input type="text" name="price_txt[]" /></td>
</tr>

<tr>
<td><br><br></td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submit" value="add" />
<input type="reset" name="submit" value="clear" /></td>
</tr>

</table>
</form>

<?php

if(isset($_POST['submit']) && $_POST['submit'] == 'add'){

if (isset($_POST['product_name_txt']))
{
include 'db.php';

foreach($_POST['product_name_txt'] as $row => $value){

$product=$_POST['product_name_txt'][$row];
$description=$_POST['description_txt'][$row];
$date=$_POST['date_txt'][$row];
$quantity=$_POST['quantity_txt'][$row];
$price=$_POST['price_txt'][$row];


$sql = "INSERT INTO `products` (`product_name`, `description`, `date`, `quantity`, `price`) VALUES ('".$product."','".$description."','".$date."','".$quantity."','".$price."');";

if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
}
?>

Check my Output that Query got executed successfully

关于php - 如何使用 PHP 的强大功能将多个输入插入到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469482/

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