MYSQL Insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imperium2335
Forum Newbie
Posts: 10
Joined: Fri Mar 20, 2009 9:24 pm

MYSQL Insert

Post by imperium2335 »

Hi, I want to update some values in my database but what ever I try it doesn't work. I've also tried setting permissions for the database to allow everyone and everything but no success.

I know its partially working because I get this:

13 people have voted, total amount in array adds up to 67 with overall rating of 5.2
you voted 7UPDATE image_bank SET numratings = 7 4 4 4 5 9 1 3 2 7 7 7 7 7, rating = 5.2 WHERE image_id = 5

But the base just doesn't update :(

Here is what i have so far (its for a rating system):

Code: Select all

$userrate = $_GET['rating'] ;
$currentimage = $_SESSION['currentimage'] ;
 
$query = "SELECT * FROM image_bank WHERE image_id = $currentimage" ;
 
$result = mysql_query($query) ;
 
while($row = mysql_fetch_array($result))
{
 
    $currentrating = $row['rating'] ;
    $numratings = $row['numratings'] ;
 
}
 
$breakdown = explode(" ",$numratings) ;
$count = count($breakdown) ;
 
 
 
$i = 0 ;
 
while($i < $count)
{
 
    $range += $breakdown[$i]  ;
    
    $i++ ;
 
}
$currentrating = round($range / $count,1) ;
 
$newratecnt = $numratings . " " . $userrate ;
 
echo "$count people have voted, total amount in array adds up to $range
with overall rating of $currentrating" ;
 
echo "<br /> you voted $userrate" ;
 
if($userrate < 0)
{
    echo "vote to small" ;
}
elseif($userrate > 10)
{
    echo "vote to big" ;
    
}
else
{
    $urate = "UPDATE image_bank SET numratings = $newratecnt, rating = $currentrating WHERE image_id = $currentimage" ;
    mysql_query($urate) ;
    echo "$urate" ;
}
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: MYSQL Insert

Post by jayshields »

Are you trying to update more than one row at a time with different values? I don't think you can do that.

You'll probably need to issue a seperate UPDATE query for each different value you want to update the table with. Using

Code: Select all

mysql_query() or die(mysql_error()
will help you with SQL query debugging.
imperium2335
Forum Newbie
Posts: 10
Joined: Fri Mar 20, 2009 9:24 pm

Re: MYSQL Insert

Post by imperium2335 »

Thanks, I found out what was wrong my code should have been :

$urate = "UPDATE image_bank SET numratings = '$newratecnt', rating = '$currentrating' WHERE image_id = '$currentimage'" ;

with the 's around the vars.
Post Reply