Jump to content

Update or Insert Help!!


anne3marie

Recommended Posts

I am trying to create a simple update or insert form which is not working. This is the code for my form

<form action="add_to_cart5.php" method="post" name="form1" onSubmit="MM_validateForm('banana_qnt','','NisNum');return document.MM_returnValue">
  <label for="banana_qnt">3 Organic Bananas $1.49 Quantity</label>
  <input name="banana_qnt" type="text" id="banana_qnt" value="<?php "SELECT quantity FROM grocery_cart WHERE username='cust_username' and product_id='bananaid' "
  ?>
  ">
  <input type="submit" name="add_to_cart" id="add_to_cart" value="update">
</form>

 

I can add new rows fine but when I try to update or insert using the following code, nothing works.

<?php
include 'db.php';

//register variable
$banana_qnt = $_POST['banana_qnt'];



$sql="UPDATE grocery_cart SET quantity='$banana_qnt' WHERE username='cust_username' and product_id='bananaid'";

//Insert cart line
if (!$sql){$sql2 = mysql_query("INSERT INTO grocery_cart (username, product_id, quantity, price)
	VALUES('cust_username','bananaid	','$banana_qnt','1.49')ON DUPLICATE KEY UPDATE quantity=$banana_qnt") ;}


	if(!$sql and !$sql2){
echo 'Item quantity not updated, please contact NewLeaf Delivery';include'grocery_cart.php';}
else{
echo "Item Quantity Updated!";include 'grocery_cart.php';
}


?>

if anyone has a better way of doing this that would help. Each customer gets a new line in the database when they enter a quantity. For this reason, there is no way to use duplicate key function because there is no unique primary key except for auto increment #. Also on my form I would like for the text box to b=have an initial value set to the "banana_qnt" in the database. Please help. Thanks for reading!

Link to comment
https://forums.phpfreaks.com/topic/264388-update-or-insert-help/
Share on other sites

Are you sure about this?

 

<?php "SELECT quantity FROM grocery_cart WHERE username='cust_username' and product_id='bananaid' "
  ?>

 

Do you mean this?

 

<?php mysql_result(mysql_query("SELECT quantity FROM grocery_cart WHERE username='cust_username' and product_id='bananaid' "),0,0);  ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.