anne3marie Posted June 18, 2012 Share Posted June 18, 2012 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 More sharing options...
Pikachu2000 Posted June 18, 2012 Share Posted June 18, 2012 Why are you using a query string as the value= attribute in an html form, and why are the values for `username` and `product_id` hard coded in the query string? Link to comment https://forums.phpfreaks.com/topic/264388-update-or-insert-help/#findComment-1354890 Share on other sites More sharing options...
anne3marie Posted June 18, 2012 Author Share Posted June 18, 2012 I am hard coding those values currently until I can get the script to work! I also want the text field in the form to show the item quantity that the customer has currently selected. Link to comment https://forums.phpfreaks.com/topic/264388-update-or-insert-help/#findComment-1354939 Share on other sites More sharing options...
beyzad Posted June 18, 2012 Share Posted June 18, 2012 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); ?> Link to comment https://forums.phpfreaks.com/topic/264388-update-or-insert-help/#findComment-1354948 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.