Reputation: 11
I am having a problem adding this array in a mysql database someone could help me? i think the problem is to identify the id of the array to input into the database.
<html>
<title> Registro do Pedido </title>
<body>
<h2> REGISTRE O PEDIDO</h2>
<form action="teste1.php" method="post">
<p align = "left">
Product 1:<input type="text" name="product1" ></br></br>
Quantity:<input type="int" name="quantidade1" ></br></br>
price:<input type="text" name="price1" ></br></br>
Product 2:<input type="text" name="product2" ></br></br>
Quantity:<input type="int" name="quantidade2" ></br></br>
price:<input type="text" name="price2" ></br></br>
<input type="submit" value="Registrar Pedido">
</body>
i need to add a for a client more than one procut and describe each quantity and price so i can calculate the total price how can i do that?
<?php
$conn = new mysqli("localhost", "root", "", "teste");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$addproduct[1]=array(
'produto'=> '$_POST[product1]',
'quant'=>'$_POST[quantidade1]',
'price'=>'$_POST[price1]'
);
$addproduct[2]=array(
'produto'=> '$_POST[product2]',
'quant'=>'$_POST[quantidade2]',
'price'=>'$_POST[price2]'
);
$sql = "INSERT INTO produtos (produto, quantidade, preco)
VALUES ('$addproduto[][produto]', '$addproduto[][quant]', '$addproduto[][price]')";
?>
Upvotes: 0
Views: 44
Reputation: 2778
This answer scales well if you want to offer more than 2 product entries, for example 3, 4 or 5 products to be added at once
HTML
<?php $num_products = 2; ?>
<h2> REGISTRE O PEDIDO</h2>
<form action="teste1.php" method="post">
<p align = "left">
<?php for ($i = 1; $i <= $num_products; $i++) { ?>
Product <?php echo $i; ?>:<input type="text" name="product[]" ></br></br>
Quantity:<input type="int" name="quantidade[]" ></br></br>
Price:<input type="text" name="price[]" ></br></br>
<?php } ?>
<input type="submit" value="Registrar Pedido">
</p>
</form>
PHP
<?php
$conn = new mysqli("localhost", "root", "", "teste");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$produto = isset($_POST['product']) ? $_POST['product'] : array();
$quant = isset($_POST['quantidade']) ? $_POST['quantidade'] : array();
$price = isset($_POST['price']) ? $_POST['price'] : array();
if (is_array($produto) && count($produto) > 0) {
// Create a unique order number
$sql = 'INSERT INTO orders SET orderid = NULL, dateadded = NOW()';
mysqli_query($conn, $sql);
$orderid = mysqli_insert_id($conn);
for ($i = 0, $j = count($produto); $i < $j; $i++) {
if (isset($quant[$i]) && isset($price[$i])) {
$sql = 'INSERT INTO produtos (orderid, itemid, produto, quantidade, preco) VALUES (' . $orderid . ', NULL, "' . mysql_real_escape_string($produto[$i]) . '", "' . ((int) $quant[$i]) . '", "' . ((float) $price[$i]) . '")';
mysqli_query($conn, $sql);
}
}
}
?>
Later on if you know the Order ID you can get the total value of all products for that order:
SELECT SUM(preco) FROM produtos WHERE orderid = 123
Upvotes: 1
Reputation: 2402
First of all, to get the total price for each item in the array, i would another element, like so:
$addproduct[1]=array(
'produto'=> '$_POST[product1]',
'quant'=>'$_POST[quantidade1]',
'price'=>'$_POST[price1]',
'total'=> '$_POST[quantidade1] * $_POST[price1]'
);
The other thing that I think you're trying to do is insert this into a database, and you have more than one item, so you need a multi-insert statement, like so:
$sql = "INSERT INTO produtos (produto, quantidade, preco)
VALUES
('$addproduto[1][produto]', '$addproduto[1][quant]','$addproduto[1][price]'),
('$addproduto[2][produto]', '$addproduto[2][quant]', '$addproduto[2][price]')";
I'm not a PHP developer, so you need to double check the syntax on the array elements that I added and whether or not that would work and calculate the total value for each item.
Upvotes: 0