FruitFaster Posted May 4, 2013 Share Posted May 4, 2013 The value I want storing is not being stored ($final) in the database which is staying at 0 or being updated as 0. Please can someone hint me in the right direction. <?php $con2=mysqli_connect("host.com","uname","pwd","dbase"); // Check connection if (mysqli_connect_errno($con2)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $value=$_POST['rating']; $id=$_POST['id']; mysqli_query ($con2, "UPDATE table SET totalraters = totalraters + 1 WHERE id='$id' "); mysqli_query ($con2, "UPDATE table SET ratingsum = ratingsum + '$value' WHERE id='$id' "); $query = "SELECT ratingsum, totalraters FROM table WHERE id='$id' "; $result2=mysqli_query($con2, $query); while ($row2= mysqli_fetch_array($result2)) { $ratingsum=$row2['ratingsum']; $totalraters=$row2['totalraters']; IF ($totalraters != 0) { $rating = ($ratingsum/$totalraters); $final = (int)round($rating); } mysqli_query ($con2, "INSERT INTO table rating VALUES '$final' WHERE id='$id' "); include ('index.html'); } ?> The table has a field called 'rating' which is set to int (6) NOT NULL. Quote Link to comment Share on other sites More sharing options...
jcbones Posted May 4, 2013 Share Posted May 4, 2013 Being that there is obviously more than one column in the `table rating` table, then you would need to specify the column that you want the value stored in. You should always add de-bugging into your scripts, it will help you with these time consuming errors: change mysqli_query ($con2, "INSERT INTO table rating VALUES '$final' WHERE id='$id' "); to mysqli_query ($con2, "INSERT INTO table rating VALUES '$final' WHERE id='$id' ") or trigger_error(mysqli_error($con2)); Quote Link to comment Share on other sites More sharing options...
FruitFaster Posted May 5, 2013 Author Share Posted May 5, 2013 (edited) Thankyou! I changed that and realised what was wrong with that line (there's no WHERE for INSERT INTO) so I changed it. I checked my database but the number in the rating field is wrong. It updated it to 1 and it should be 4. The line I changed is now: mysqli_query ($con2, "UPDATE table SET rating='$final' AND id='$id' ") or trigger_error(mysqli_error($con2)); Edited May 5, 2013 by FruitFaster Quote Link to comment Share on other sites More sharing options...
FruitFaster Posted May 5, 2013 Author Share Posted May 5, 2013 Please can someone give me a hint as why my database is being updated wrongly as explained ^^^^ Quote Link to comment Share on other sites More sharing options...
jcbones Posted May 5, 2013 Share Posted May 5, 2013 You will need to detail the expected values, AND your table structures. these two mysqli_query ($con2, "UPDATE table SET totalraters = totalraters + 1 WHERE id='$id' "); mysqli_query ($con2, "UPDATE table SET ratingsum = ratingsum + '$value' WHERE id='$id' "); Can be combined mysqli_query($con2,"UPDATE table SET totalraters = totalraters + 1, ratingsum = ratingsum + $value WHERE id = $id"); I'm still not sure what `table rating` looks like, nor how it ties into the other tables, so I won't put a query here for it as it may lead down the wrong rabbit hole, until structure and usage is addressed. Quote Link to comment 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.