Arihant
Arihant

Reputation: 4047

Update query running but not updating table php

Here's the code i am using withing a for loop which run 10 times:

$query = "UPDATE fblikes SET likes = '$var[$i]' WHERE link = '$web[$i]'";

if(mysql_query($query))
{
    echo $query;
}                         
else
{
    echo mysql_error();
}

The code runs, I do get ok! printed 10 times but nothing happens in the table. I also checked the 2 arrays i.e. $var and $web, they contain the correct values.

The query looks okay to me. Here's what i got (one of the 10 outputs) : UPDATE fblikes SET likes = '5' WHERE link = 'xxxxxxx.com/xxxx/iet.php';

Upvotes: 0

Views: 583

Answers (1)

gd1
gd1

Reputation: 11403

I don't know what the problem exactly is, and to figure out you should print the value of $query, and show us what you get. More, please tell us the value of mysql_affected_rows() after the call to mysql_query().

However, your code implements some wrong patterns.

First of all, you are not escaping $var[$i] and $web[$i] with two potential effects:

  • You can produce wrong queries
  • You don't sanitize the input to the database, thus exposing your application to security issues

Moreover, you perform several similar queries that differ only on the inputs provided.

The solution, for both issues, is the use of prepared statements that will give you more control, security and performance. Consider abandoning mysql_* functions and switching to mysqli_* or PDO, and read about prepared statements.

Upvotes: 2

Related Questions