Jump to content

insert data button


thelee

Recommended Posts

i have created a shopping cart.but i dont know how i can add all the choosen data into a database.i just follow the step from youtube.i want to know how i can add one button so all the product will be recorded into database because it already have 1 button to update the shopping cart but it do not have button to record all the choosen product.

 

here is all the file that i have created.

 

index2.php

<?
session_start();
error_reporting(E_ALL);
require("connection.php");
if(isset($_GET['page'])){

	$pages=array("products", "cart");
	if(in_array($_GET['page'], $pages)) {
	
		$_page=$_GET['page'];
	
	}else{
	
		$_page="products";
	
	}
	
}else{

$_page="products";

}
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>


<body>
</body>
</html>

<link rel="stylesheet" href="style2.css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>

<script type="text/javascript">
$(function() {

});
</script> 
</head>

<body>
<div id="container">

<div id="main">
<?php require($_page.".php");     ?>

</div>

<div id="sidebar">
<h1>Cart</h1>

<?php 
if(isset($_SESSION['cart'])){

$sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){

?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php }

?>

<hr />
<a href="index2.php?page=cart">Go to Cart</a>


<?php





}else{

echo "<p>Your Cart is empty</p>";

}
?>
</div>

</div>
</body>


products.php

<?php

	if(isset($_GET['action']) && $_GET['action']=="add"){
	
		$id=intval($_GET['id']);
		
		if(isset($_SESSION['cart'][$id])){
		
			$_SESSION['cart'][$id]['quantity']++;
		
		}else{
		
			$sql_s="SELECT * FROM products
				WHERE id_product={$id}";
			$query_s=mysql_query($sql_s);
			if(mysql_num_rows($query_s)!=0){
				$row_s=mysql_fetch_array($query_s);
				
					
					
					$_SESSION['cart'][$row_s['id_product']]=array(
							"quantity" => 1,
							"price" => $row_s['price']
							
							);
				
				
				}else{
				
					$message="This product id it's invalid!";
	

		}
		}
		}
		
		


?>


<h1>Product List</h1>
<?php 

	if(isset($message)){
	echo "<h2>$message</h2>";
	}
?>
<table>
<tr>
    <th>Name</th>
	<th>Description</th>
	<th>Price</th>
	<th>Action</th>
</tr>
<?php
$sql="SELECT * FROM products ORDER BY name ASC";

$query=mysql_query($sql);

while ($row=mysql_fetch_array($query)) {

?>
<tr>
	<td><?php echo $row['name'] ?></td>
	<td><?php echo $row['description'] ?></td>
	<td>RM <?php echo $row['price'] ?></td>
	<td><a href="index2.php?page=products&action=add&id=<?php echo $row['id_product'] ?>">Add To Cart</a></td>
</tr>
<?php
}
?>

</table>

cart.php

<?php
 if(isset($_POST['submit'])){
 
 foreach($_POST['quantity'] as $key => $val) {
 	if($val==0) {
	unset($_SESSION['cart'][$key]);
	}else{
	$_SESSION['cart'][$key]['quantity']=$val;
	
	}
	}
 
 
 }

?>



<a href="index2.php?page=products">Go back to product page</a>
<h1>View Cart</h1>

<form method="post" action"index2.php?page=cart">

<table>
<tr>
	<th>Name</th>
	<th>Quantity</th>
	<th>Price</th>
	<th>Item Price</th>



</tr>
<?php

	$sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
	$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
	$totalprice+=$subtotal;

?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td>
<td>RM <?php echo $row['price'] ?></td>
<td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td>



</tr>



<?php 
}

?>
<tr>
<td>Total Price: <?php echo $totalprice  ?></td>


</tr>

</table>
<button type="submit" name="submit"> Update Cart</button>


</form>
<br />
<p> To remove an item,set the quantity to 0</p>
Link to comment
Share on other sites

if your goal is to convert the contents of the cart into an actual order during the checkout process, it's actually easier and will result in less code if you store the cart contents in the database in the first place. you would have an entry for the current visitor in an `orders` table (for the information about the order and an order_details table, with the items in the cart/order) with a 'pending' status. to convert the order into an actual order, just change the status to 'ordered'.

 

if you are not up to the changes to the existing code that would be required to do the above (you will still need, at a minimum, the two database tables i mentioned and the code needed to get and store the data into those tables), you would need to write code that basically does -

 

1) get the visitor's information (name, address, email, phone) and store this in a users table.

 

2) insert a row in the orders table with an id (auto increment field), the visitor's user id, date/time, status (pending, ordered, ...), and ship to information (which could be the same or different than the visitor's address in the users table.)

 

3) insert the contents of the $_SESSION['cart'] into the order_details table, one row per item, with an id (auto increment field), order_id (the id from the orders table), item_id, quantity, status, and any other information you may need to record, such as the price when the order was placed... the status field in this table will allow you to track the status of individual items in the order (allows for making partial shipments, items being returned, ...)

Link to comment
Share on other sites

thanks mac_gyver... how i can use two button with a different function in the cart.php in a same form.because the 1st button function is to update cart.can i add second button that have function to insert all the choosen data into database table? Both button in a same form.

Link to comment
Share on other sites

where exactly are you stuck at when you tried to do this?

 

you already have code that has a button and php code to do something when that button is used to submit the form.

 

couldn't you add a second button with a different name attribute, then define and write the php code that would detect if the button with a different name was used to submit the form and loop over the contents of the cart and insert the data into a database table?

Link to comment
Share on other sites

if i add a second button with a different name attribute, do i need any change at the 

foreach($_POST['quantity'] as $key => $val) {
 	if($val==0) {
	unset($_SESSION['cart'][$key]);
	}else{
	$_SESSION['cart'][$key]['quantity']=$val;
	
	}

? sorry,im not familiar with this situation,i dont have much experience.i still need lot of learning

Link to comment
Share on other sites

the purpose of the code you just posted is to update the quantiles in the session based cart. you wouldn't change that code at all.

 

you would add new code to go along with the new button.

 

however, are you really asking how to change the whole session based cart code so that the 'add to cart' and 'update cart' operate on data stored in a database table? if so, you would need to rewrite all the code to do so, which would be beyond the scope of a help forum. you would need to change all the code that is using $_SESSION['cart'] into database queries that insert, delete, and update the records in a database table.

Link to comment
Share on other sites

i want to add "confirm" button. so all the data from the shopping cart will be record in the database. i just want to record name,price,quantity,and total price.i dont want to record the user information because this system is for restaurant,used by staff to record order from customer. 

Link to comment
Share on other sites

it sounds like you don't have a clue about developing a plan, thinking it through, and coding it up.  You've coded up so much so far and now you are looking for help?  What happened to your plan?  What happened to your coding skills?  Or did you not write this yourself?  You have received some good tips here that you have used to make some progress, what is wrong now?  Why are you not doing some work to perhaps re-arrange your process, re-think your goals and making the major changes that you were told you might have to do?

 

Do you really think that all we have to do on this forum is design your solution and write it for you?  :)

Link to comment
Share on other sites

your current code is displaying the content of the cart. wouldn't you use similar code to store the content of your cart into a database table?

 

the reason we are not posting code that does what you want is because it seems you are just asking for someone to do this for you, rather than you making an attempt at doing it. have you even defined and created the database table you are going to store this data into?

Link to comment
Share on other sites

i have modified the cart.php to insert data into table,but it dont have and error and dont insert the data into table.can help me ? here is the cart.php

<?php
error_reporting(E_ALL);
require("connection.php");
 if(isset($_POST['submit'])){
 
 foreach($_POST['quantity'] as $key => $val) {
 	if($val==0) {
	unset($_SESSION['cart'][$key]);
	}else{
	$_SESSION['cart'][$key]['quantity']=$val;
	
 if(isset($_POST['submit2'])){ mysql_query("INSERT INTO order 
(name, quantity, price) VALUES('$name', '$count', '$price' ) ") 
or die(mysql_error());  

echo "data inserted";
}
	}
	}
 

 }

?>



<a href="index2.php?page=products">Go back to product page</a>
<h1>View Cart</h1>

<form method="post" action"index2.php?page=cart">

<table>
<tr>
	<th>Name</th>
	<th>Quantity</th>
	<th>Price</th>
	<th>Item Price</th>



</tr>
<?php

	$sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
	$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
	$totalprice+=$subtotal;

?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td>
<td>RM <?php echo $row['price'] ?></td>
<td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td>



</tr>



<?php 
}

?>
<tr>
<td>Total Price: <?php echo $totalprice  ?></td>


</tr>

</table>
<button type="submit" name="submit"> Update Cart</button>
<button type="submit" name="submit2"> Update Cart</button>


</form>
<br />
<p> To remove an item,set the quantity to 0</p>
Link to comment
Share on other sites

the php code you have added is inside the  if(isset($_POST['submit'])){ ... }. when you use your second submit button, the first submit button is false and all the code inside that if(){} statement is skipped over.

 

the php code you added for the 'insert into database' needs to be separate from the 'update cart' code.

Link to comment
Share on other sites

what you are doing is not complicated, but it does require that you understand what your existing code is doing so that the new code doesn't get put in the wrong logical place.

 

your php code would be organized as follows -

// your existing update cart code
if(isset($_POST['submit'])){

     ...

}


// your new insert to database code
if(isset($_POST['submit2'])){

     ...

}
Link to comment
Share on other sites

thanks mac.i have change the cart.php like this,its working fine i think,but i get this error when i clicked the submit2 button. what is my mistake now ?

<?php
error_reporting(E_ALL);
 ini_set('display_errors', '1');
require("connection.php");
if(isset($_POST['submit2'])){ mysql_query("INSERT INTO order 
(name, quantity, price) VALUES('$name', '$quantity', '$price' ) ") 
or die(mysql_error());  

echo "data inserted"; }
 if(isset($_POST['submit'])){
 
 foreach($_POST['quantity'] as $key => $val) {
 	if($val==0) {
	unset($_SESSION['cart'][$key]);
	}else{
	$_SESSION['cart'][$key]['quantity']=$val;
	


	}
	}
 

 }

?>



<a href="index2.php?page=products">Go back to product page</a>
<h1>View Cart</h1>

<form method="post" action"index2.php?page=cart">

<table>
<tr>
	<th>Name</th>
	<th>Quantity</th>
	<th>Price</th>
	<th>Item Price</th>



</tr>
<?php

	$sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
	$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
	$totalprice+=$subtotal;

?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td>
<td>RM <?php echo $row['price'] ?></td>
<td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td>



</tr>



<?php 
}

?>
<tr>
<td>Total Price: <?php echo $totalprice  ?></td>


</tr>

</table>
<button type="submit" name="submit"> Update Cart</button>
<button type="submit" name="submit2"> Update Cart</button>


</form>
<br />
<p> To remove an item,set the quantity to 0</p>

here is the error:

Notice: Undefined variable: name inC:\xampp\htdocs\emakengku\cart.php on line 6

Notice: Undefined variable: quantity inC:\xampp\htdocs\emakengku\cart.php on line 6

Notice: Undefined variable: price in C:\xampp\htdocs\emakengku\cart.phpon line 6
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order (name, quantity, price) VALUES('', '', '' )' at line 1

Link to comment
Share on other sites

You have not defined the 3 value items in your query.  That gives you the php error as well as the sql error for trying to use unknown vars.

 

BTW - if you did not have those two lines at the top turning on php error checking, you wouldn't know this.

Edited by ginerjm
Link to comment
Share on other sites

i have define.it still give me error.here is the code that i have change.

<?php
error_reporting(E_ALL);
 ini_set('display_errors', '1');
require("connection.php");
if(isset($_POST['submit2']))

	$name=$_POST["name"];
	$quantity=$_POST["quantity"];
	$price=$_POST["price"];
 $sql_insert = "INSERT INTO order ('name', 'quantity', 'price') 
values ('name', 'quantity', 'price')";
echo "sucess!";



 if(isset($_POST['submit'])){
 
 foreach($_POST['quantity'] as $key => $val) {
 	if($val==0) {
	unset($_SESSION['cart'][$key]);
	}else{
	$_SESSION['cart'][$key]['quantity']=$val;
	


	}
	}
 

 }

?>



<a href="index2.php?page=products">Go back to product page</a>
<h1>View Cart</h1>

<form method="post" action"index2.php?page=cart">

<table>
<tr>
	<th>Name</th>
	<th>Quantity</th>
	<th>Price</th>
	<th>Item Price</th>



</tr>
<?php

	$sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
	$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
	$totalprice+=$subtotal;

?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[<?php echo $row['id_product'] ?>]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td>
<td>RM <?php echo $row['price'] ?></td>
<td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td>



</tr>



<?php 
}

?>
<tr>
<td>Total Price: <?php echo $totalprice  ?></td>


</tr>

</table>
<button type="submit" name="submit"> Update Cart</button>
<button type="submit" name="submit2"> Update Cart</button>


</form>
<br />
<p> To remove an item,set the quantity to 0</p>

here is the error

 

Notice: Undefined index: name in C:\xampp\htdocs\emakengku\cart.phpon line 7

Notice: Undefined index: quantity inC:\xampp\htdocs\emakengku\cart.php on line 8

Notice: Undefined index: price in C:\xampp\htdocs\emakengku\cart.phpon line 9

Link to comment
Share on other sites

Some of your current (error-laden) code:

if(isset($_POST['submit2']))

$name=$_POST["name"];
$quantity=$_POST["quantity"];
$price=$_POST["price"];
$sql_insert = "INSERT INTO order ('name', 'quantity', 'price')
values ('name', 'quantity', 'price')";
echo "sucess!";

#1 - you have an if statement.  In order to determine the statements to be run if that if is TRUE you need to enclose them in curly braces.  You aren't doing that so only the first statement is run to set $name if submit2 was clicked.  The real problem begins when the rest of those statements are run EVEN WHEN submit2 WASN'T CLICKED. 

Do you see that?

 

#2 - your query statement is bogus.  Where are the inputs you want inserted?  Do you REALLY want to insert the words "name", "quantity" and "price" into your table?  That's what your statement says and it probably fails since price is probably a numeric field and you are trying to put alpha chars into it.  Same for the quantity field.

How would you correct this?

 

#3 - WHENEVER you run a query you really should check the result to be sure it ran.  Programs are going to do whatever you tell them to do, and like little children they will do things you don't tell them to do.  If that query fails your code is still going to continue to run along even tho things aren't happening like you planned.  SO - ALWAYS CHECK THE RESULTS OF 'EXTERNAL' THINGS.  This is true for running queries, opening files, copying files to other locations, etc.  You can't just assume that everything happens all the time.

Edited by ginerjm
Link to comment
Share on other sites

i have follow a video how to make a shopping cart, the video didnt teach me how to insert all confirmed data into table,so i try to make it by myself,and im still newbie in php. so after i make some changes at the cart.php, its echo sucess,but no data inserted into table.can someone show me what is my mistake.

 

here is the code:

 

index2.php

<?
session_start();
error_reporting(E_ALL);
require("connection.php");
if(isset($_GET['page'])){

	$pages=array("products", "cart");
	if(in_array($_GET['page'], $pages)) {
	
		$_page=$_GET['page'];
	
	}else{
	
		$_page="products";
	
	}
	
}else{

$_page="products";

}
?>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>



<link rel="stylesheet" href="style2.css" />

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>

<script type="text/javascript">
$(function() {

});
</script> 
</head>

<body>
<div id="container">

<div id="main">
<?php require($_page.".php");     ?>

</div>

<div id="sidebar">
<h1>Cart</h1>

<?php 
if(isset($_SESSION['cart'])){

$sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
while($row=mysql_fetch_array($query)){

?>
<p><?php echo $row['name'] ?> x <?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?></p>
<?php }

?>

<hr />
<a href="index2.php?page=cart">Go to Cart</a>


<?php





}else{

echo "<p>Your Cart is empty</p>";

}
?>
</div>

</div>
</body>


products.php

<?php

	if(isset($_GET['action']) && $_GET['action']=="add"){
	
		$id=intval($_GET['id']);
		
		if(isset($_SESSION['cart'][$id])){
		
			$_SESSION['cart'][$id]['quantity']++;
		
		}else{
		
			$sql_s="SELECT * FROM products
				WHERE id_product={$id}";
			$query_s=mysql_query($sql_s);
			if(mysql_num_rows($query_s)!=0){
				$row_s=mysql_fetch_array($query_s);
				
					
					
					$_SESSION['cart'][$row_s['id_product']]=array(
							"quantity" => 1,
							"price" => $row_s['price']
							
							);
				
				
				}else{
				
					$message="This product id it's invalid!";
	

		}
		}
		}
		
		


?>


<h1>Product List</h1>
<?php 

	if(isset($message)){
	echo "<h2>$message</h2>";
	}
?>
<table>
<tr>
    <th>Name</th>
	<th>Description</th>
	<th>Price</th>
	<th>Action</th>
</tr>
<?php
$sql="SELECT * FROM products ORDER BY name ASC";

$query=mysql_query($sql);

while ($row=mysql_fetch_array($query)) {

?>
<tr>
	<td><?php echo $row['name'] ?></td>
	<td><?php echo $row['description'] ?></td>
	<td>RM <?php echo $row['price'] ?></td>
	<td><a href="index2.php?page=products&action=add&id=<?php echo $row['id_product'] ?>">Add To Cart</a></td>
</tr>
<?php
}
?>

</table>

cart.php

<?php
error_reporting(E_ALL ^ E_NOTICE);

require("connection.php");
if(isset($_POST['submit2'])) {

	$name=$_POST["name"];
	$quantity=$_POST["quantity"];
	$price=$_POST["price"];
$sql_insert = "INSERT INTO order ('name', 'quantity', 'price')  
values('$name', '$quantity', '$price')"; 
mysql_query($sql_insert);   

echo "sucess!";

}

 if(isset($_POST['submit'])){
 
 foreach($_POST['quantity'] as $key => $val) {
 	if($val==0) {
	unset($_SESSION['cart'][$key]);
	}else{
	$_SESSION['cart'][$key]['quantity']=$val;
	


	}
	}
 

 }

?>



<a href="index2.php?page=products">Go back to product page</a>
<h1>View Cart</h1>

<form method="post" action="index2.php?page=cart">

<table>
<tr>
	<th>Name</th>
	<th>Quantity</th>
	<th>Price</th>
	<th>Item Price</th>



</tr>
<?php

	$sql="SELECT * FROM products WHERE id_product IN (";

foreach($_SESSION['cart'] as $id => $value) {
$sql.=$id.",";
}
$sql=substr($sql, 0, -1).") ORDER BY name ASC";
$query=mysql_query($sql);
$totalprice=0;
while($row=mysql_fetch_array($query)){
	$subtotal=$_SESSION['cart'][$row['id_product']]['quantity']*$row['price'];
	$totalprice+=$subtotal;

?>
<tr>
<td><?php echo $row['name'] ?></td>
<td><input type="text" name="quantity[]" size="5" value="<?php echo $_SESSION['cart'][$row['id_product']]['quantity'] ?>"</td> 
<td>RM <?php echo $row['price'] ?></td>
<td>RM <?php echo $_SESSION['cart'][$row['id_product']]['quantity']*$row['price'] ?></td>



</tr>



<?php 
}

?>
<tr>
<td>Total Price: <?php echo $totalprice  ?></td>


</tr>

</table>
<button type="submit" name="submit"> Update Cart</button>
<button type="submit" name="submit2"> Update Cart</button>


</form>
<br />
<p> To remove an item,set the quantity to 0</p>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.