Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have created checkout page. If i select more than one product, then record store only one record of second row. For example i select ABC and XYZ product to store in database; but only store in database XYZ product. Below i given code what i tried.

What I have tried:

Here is my data fetched in database code.
<form id="order_pro" action="" method="post">
	<?php
		foreach ($_SESSION['productinfo'] as $productinfo) {
	?>
		<div class="row">
			<input type="text" name="pro_name" value="<?php echo $productinfo['pro_name'];?>"/>
			<input type="text" name="pro_qua" value="<?php echo $productinfo['pro_qua'];?>"/>
			<input type="text" name="pro_price" value="<?php echo number_format($productinfo['pro_prices'] * $productinfo['pro_qua'],2); ?>"/>
		</div>
		<div class="row">
			<button type="button" class="btn btn-primary next-step button_checkout">Save and continue</button>
		</div>
	<?php 
	}                                   
	?>
</form>


Here is store data code:

<?php
	
	require_once '../admin/includes/db_connect.php';		
		
	$pro_name	=	$_POST["pro_name"];
	$pro_qua	=	$_POST["pro_qua"];
	$pro_price	=	$_POST['pro_price'];

	$name		=	$_POST["name"];
	$mobile		=	$_POST["mobile"];
	$email		=	$_POST["email"];
	$address	=	$_POST["address"];
	$city		=	$_POST["city"];
	$pin		=	$_POST["pin"];
		
	if( empty($name)|| empty($mobile)|| empty($email)|| empty($address) || empty($city) || empty($pin)){
		echo '			
			<div class="alert alert-danger alert-dismissible" role="alert">
			<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
			Warning! Please fill all mandatory fields.
			</div>
		';
	}		
	else{
		$result=mysqli_query($conn, "INSERT INTO wm_orders(pro_name, pro_qua, pro_price, name, mobile, email, address, city, pin, status) VALUES ('$pro_name','$pro_qua','$pro_price','$name','$mobile','$email','$address','$city','$pin','pending')")or die("Could not insert data: " .mysqli_error($conn));
	
		echo 'data store in database';				
	}	
?>
Posted
Updated 31-Aug-18 20:32pm
Comments
Herman<T>.Instance 31-Aug-18 7:28am    
more than 1 row and no iteration over your row collection. That's your next Google search

Back to basics:
1. Whichever button you click in a form, all rows of data will be posted to the receiving script;
2. How can the receiving script distinguish the data elements from different rows if they have the same values for their 'name' attributes in all the rows?
Check this out How do I create arrays in a HTML form?[^]
 
Share this answer
 
v3
PHP
$result=mysqli_query($conn, "INSERT INTO wm_orders(pro_name, pro_qua, pro_price, name, mobile, email, address, city, pin, status) VALUES ('$pro_name','$pro_qua','$pro_price','$name','$mobile','$email','$address','$city','$pin','pending')")

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900