Silvar Posted December 31, 2012 Share Posted December 31, 2012 (edited) Hello everyone. This seems to be a nice forum! You will be seeing me here a lot in the future. Anyway, I got a problem with this code: $editId = $_GET["edit"]; $getCurrentAmount = mysql_query("SELECT amount FROM guilt_list WHERE id = $editId") or die(mysql_error()); $amountIsCurrently = mysql_fetch_row($getCurrentAmount); $listAmount = $amountIsCurrently+$gAmount; mysql_query("UPDATE guilt_list SET name='$gName', amount='$listAmount' WHERE id='$editId'"); It gives me: "Fatal error: Unsupported operand types in /var/www/********/public_html/guilt.php on line 171" Line 171 is: $listAmount = $amountIsCurrently+$gAmount; What seems to be the problem? I can't figure it out. Thanks in advance and happy new year! Best regards, - Silvar Edited December 31, 2012 by Silvar Quote Link to comment https://forums.phpfreaks.com/topic/272554-unsupported-operand-types/ Share on other sites More sharing options...
requinix Posted December 31, 2012 Share Posted December 31, 2012 (edited) I don't know about $gAmount but $amountIsCurrently is an array. You have to go into it to grab the "amount" you selected in the query. If you aren't sure of the structure of the array, print_r() or var_dump() it. [edit] It would be a lot easier for you to add the $gAmount in a single query. Don't have to get PHP involved in any of that. UPDATE guilt_list SET name = '$gName', amount = amount + $gAmount WHERE id = $editId Edited December 31, 2012 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/272554-unsupported-operand-types/#findComment-1402422 Share on other sites More sharing options...
Silvar Posted December 31, 2012 Author Share Posted December 31, 2012 (edited) I don't know about $gAmount but $amountIsCurrently is an array. You have to go into it to grab the "amount" you selected in the query. If you aren't sure of the structure of the array, print_r() or var_dump() it. [edit] It would be a lot easier for you to add the $gAmount in a single query. Don't have to get PHP involved in any of that. UPDATE guilt_list SET name = '$gName', amount = amount + $gAmount WHERE id = $editId So how would you rewrite this code to do the proper action: $gName = htmlspecialchars(addslashes($_POST["editName"])); $gAmount = $_POST["editAmount"]; $gDate = date("d/m-Y"); if(!empty($gName) && !empty($gAmount)) { $editId = $_GET["edit"]; $getCurrentAmount = mysql_query("SELECT amount FROM guilt_list WHERE id = $editId") or die(mysql_error()); $amountIsCurrently = mysql_fetch_row($getCurrentAmount); $listAmount = $amountIsCurrently+$gAmount; mysql_query("UPDATE guilt_list SET name='$gName', amount='$listAmount' WHERE id='$editId'"); mysql_query("INSERT INTO guilt_changes(id, amount, date) VALUES('$editId', '$gAmount', '$gDate')") or die(mysql_error()); echo "<h2>Success: The guilt that ".$gName." owes has been edited!</h2>"; echo "<div style=\"width: 680px; margin: 10px 0 10px 0\"> </div>"; } Edited December 31, 2012 by Silvar Quote Link to comment https://forums.phpfreaks.com/topic/272554-unsupported-operand-types/#findComment-1402424 Share on other sites More sharing options...
Silvar Posted December 31, 2012 Author Share Posted December 31, 2012 Okay I moved the $amountIsCurrently+$gAmount to the mysql_query, now I get this error: Duplicate entry '1' for key 'PRIMARY' Quote Link to comment https://forums.phpfreaks.com/topic/272554-unsupported-operand-types/#findComment-1402426 Share on other sites More sharing options...
Silvar Posted December 31, 2012 Author Share Posted December 31, 2012 The problem seems to be problems with the id it inserts into my mysql because of duplication? Quote Link to comment https://forums.phpfreaks.com/topic/272554-unsupported-operand-types/#findComment-1402432 Share on other sites More sharing options...
requinix Posted December 31, 2012 Share Posted December 31, 2012 Is the error happening on the insert into guilt_changes? Is there a primary key on a field when there shouldn't be one? Quote Link to comment https://forums.phpfreaks.com/topic/272554-unsupported-operand-types/#findComment-1402446 Share on other sites More sharing options...
Silvar Posted December 31, 2012 Author Share Posted December 31, 2012 Is the error happening on the insert into guilt_changes? Is there a primary key on a field when there shouldn't be one? I fixed the problem now :-) Quote Link to comment https://forums.phpfreaks.com/topic/272554-unsupported-operand-types/#findComment-1402460 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.