Jump to content

FruitFaster

Members
  • Posts

    6
  • Joined

  • Last visited

FruitFaster's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Please can someone give me a hint as why my database is being updated wrongly as explained ^^^^
  2. 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));
  3. 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.
  4. Okay so I don't get any errors now but it doesn't seem to be inserting the rating or it is inserting 0 over the top of the existing 0. I'm not sure.
  5. Thankyou most kindly requinix. To be honest I think I could have stared at it for hours and not seen it. What's worse is the previous php file has exactly the same type of array in it. Just not seeing the woods for the trees. Again, thankyou!
  6. I'm making a page that takes user comments and outputs them on another page where they can be rated from 1-10. I'm having trouble with myqli_fetch_array ...it says: The code I have is: <?php $con2=mysqli_connect("host.com","user","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' "); $query2 = "SELECT ratingsum, totalraters FROM table WHERE id='$id' "; $result2=mysqli_query($con2, $query2); while ($row2= mysqli_fetch_array($con2, $result2)) { $ratingsum=$row2['ratingsum']; $totalraters=$row2['totalraters']; IF ($totalraters != 0) { ($rating = round($ratingsum/$totalraters)); mysqli_query ($con2, "INSERT INTO table (rating) VALUES ($rating) WHERE id='$id' ");} include ('index.html'); } ?> The id is sent by post from the previous php file that has a form. It updates the database with everything but the final rating and works up to the mysqli_fetch_array fine. Any help appreciated. I've spent a lot of time reading documentation and googling and changing millions of things. At first I had the boolean error now this. I have no clue why.
×
×
  • 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.