PHP - Association
what I would like to do is have a query that if $product_id with specific $cart_id is already present in database, then it will update quantity instead of inserting a new row, but if $product_id is not present with specific $cart_id(important that it isn't with this) then it will insert a new row. can anyone help?
Code: [Select] <?php include_once("connect.php"); session_start(); if($_POST['submit']) { // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM cart where cart_id = ".$_SESSION['cart_id'].""); while($row = mysql_fetch_array($sql)){ $product = $row["product123"]; $price1 = $row["price"]; $id = $row["product_id"]; $qty = $row["quantity"]; } $product = mysql_real_escape_string($_POST['hiddenField']); $price = mysql_real_escape_string($_POST['hiddenField1']); $id = mysql_real_escape_string($_POST['hiddenField2']); $sql = "INSERT INTO cart (price, product123, quantity, cart_id, product_id) VALUES('$price', '$product', '1', '".$_SESSION['cart_id']."', '$id' )"; $rs = mysql_query($sql) or die ("Problem with the query: $sql <br />" . mysql_error()); } ?> Similar TutorialsI get this message with the code at the bottom: Quote PHP Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home1/acunishi/public_html/go/auth/birthdays_holidays-greetings.php on line 35 Code: [Select] session_start(); $connection=mysql_connect("localhost","database username","db user password"); $db=mysql_select_db("database name",$connection ); $users_table = 'table name'; $username = $_POST['myusername']; $result3 = mysql_query("SELECT `nickname` from `databasename`.`tablename` WHERE `loginsv4_md5`.`username` = '$myusername'"); $result4 = mysql_query("SELECT `lastlogin` from `databasename'.'tablename` WHERE `tablename`.`username` = '$myusername'"); $result5 = mysql_query("SELECT `announce` from `databasename`.`tablename` WHERE `tablename`.`username` = '$myusername'"); $num_rows = mysql_num_rows($result3); $row3 = mysql_fetch_assoc($result3); while ($row3 = mysql_fetch_assoc($result3)) { echo " \n <br>"; echo $row3['nickname']; echo " "; echo " \n"; } $row5 = mysql_fetch_assoc($result5); while ($row5 = mysql_fetch_assoc($result5)) { echo $row5['announce']; echo " "; } $row4 = mysql_fetch_assoc($result4); while ($row4 = mysql_fetch_assoc($result4)) { echo $row4['lastlogin']; echo " "; } ..... code not displayed Error message seems to point to mysql_fetch_assoc. Other pieces of this code are being executed, so I know the module is called and executing, and of course, there is the PHP warning message from the log. Please help |