I have a duplicate html form input now the problem here is I can't insert them all into the database. I was using foreach but the other fields cannot be inserted.
Here's my code:
<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> </td>
<td><input type="submit" name="submit" value="add" />
<input type="reset" name="submit" value="clear" /></td>
</tr>
</table>
Here's my php code for adding inserting into the database:
<?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);
}
}
}
?>
It's easy, use foreach for take all data from post array.
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 ('$value','','','','')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments