2
$show=mysql_query("SELECT MAX(photoid) FROM photos WHERE userid='".$_SESSION['user']."'");
$row = mysql_fetch_array($show);
$newpp=mysql_query("UPDATE photos SET pp='1' WHERE photoid='".$row['photoid']."'");

The code displayed is meant to change a value 'pp' to 1 once run. The condition for the exact row to be change is MAX(photo) where photoid happens to be the primary key. The value pp on the max photoid is not changing. What could be the problem.

Khan Luke
  • 168
  • 1
  • 13
  • 5
    use `SELECT MAX(photoid) as photoid` and stop using mysql_* extension its deprecated and closed in php 7, use msyqli_* or PDO – devpro Apr 05 '16 at 14:12
  • 1
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Jay Blanchard Apr 05 '16 at 14:13
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 05 '16 at 14:13
  • You need to get in the habit of [accepting answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) which help you to solve your issues. You'll earn points and others will be encouraged to help you. – Jay Blanchard Apr 05 '16 at 14:14
  • Thanks it works! How critical is the AS phrase? I mean why wouldnt the code work without it. – Khan Luke Apr 05 '16 at 14:15
  • 1
    It wouldn't work without it because you were selecting `MAX(identifier)`. If you had put `WHERE photoid = '" . $row['MAX(photoid)']` it would've worked because it is returned from the first query that way. `AS` makes it easier, most folks use `AS` all of the time when they are calculating something in the `SELECT`. – Jay Blanchard Apr 05 '16 at 14:23
  • agreed with @JayBlanchard: here `as photoid` is a alias of your column not column name. (alternate name of your column). – devpro Apr 05 '16 at 14:32
  • 1
    Thanks Jay, you're the absolute best! The MAX(identifier) has shed some needed light. – Khan Luke Apr 05 '16 at 14:34

0 Answers0