qwerty77asdf qwerty77asdf - 1 year ago 62
MySQL Question

MySQL UPDATE query (PDO) inexplicably not working

I have been working on a website and recently added some changes to it, despite attempting to comment out said changes, my UPDATE query is not working- I have been attempting to figure out why for the last 3ish hours to no avail.

if (isset($_POST['submit']))
if (count($rated)<$_SESSION['id'])
$difference = $_SESSION['id'] - count($rated);

$rated = implode(',',$rated);
while ($difference >= 0)
$rated .= "0,";
$rated = explode(',',$rated);

$rated[$_SESSION['id']] = $_POST['rating'];

$ratings = 0;
$ratingsadded = 0;
foreach ($rated as $user => $rating)
if ($rating != 0)
$query = $db->prepare("SELECT id, active FROM accounts WHERE id = :id");
$query->execute(array('id' => $user));
$useractive = $query->fetch();

if ($useractive['active'] == 1 || $user == 0 || $user == 50)
$ratingsadded += $rating;
$rated[$user] = 0;
$ratingtotal = $ratingsadded / $ratings;

$query = $db->prepare("UPDATE accounts SET rating = :rating, rated = :rated WHERE id = :id");
$query->execute(array('rating' => $ratingtotal, 'rated' => implode(",",$rated), 'id' => $user['id']));
header('Location: ?user=' . $_GET['user']);

Note: The query at the very end is the one I am referring to.

Secondary note: I know that there are quite a few inefficiencies in my code.

Full code: http://pastebin.com/ybb71U6k

Answer Source

Whilst Rajdeep has deleted his answers, I intended on responding with the following:

Thank you very much, whilst your answer was a tiny bit off, it pointed out the problem I had- I was using $user as a foreach key value and as a query result.

I have found the problem and it was a case of simple stupidity and assigning variables atop of other variables.

I am sorry for putting you all through the trouble of attempting to assist me for this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download