Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/277634-database-not-taking-value/
Share on other sites

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));

 

 

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 by FruitFaster

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.