0

Here is my code.

    $rs = $db->query('UPDATE shorturl SET originalUrl = :originalUrl, status = :status, expiryDate=null, shortUrlFolder=:shortUrlFolder WHERE id = :id', array('originalUrl'    => $original_url, 'status'         => $status, 'shortUrlFolder' => $shortUrlFolder, 'id'             => $urlId));

If i change "status = :status" to "status = active" or "status = 'active'" I get a 500 error from my page when i run it, why is this am i getting the string wrong ? if i change back to "status = :status" it all works.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
Michael JS
  • 23
  • 6
  • 3
    A 500 error is a generic error message and covers pretty much every single thing that can go wrong with a script. Check your server error logs to find out the exact error message. – aynber May 01 '19 at 13:07
  • it's only when i change "status = :status" to "status = active" is i getting the string wrong ? – Michael JS May 01 '19 at 13:26
  • Yes. Unquoted strings are treated as column names in MySQL. `active` would need to be quoted. – aynber May 01 '19 at 13:27
  • Mention the `status` value as string while binding it. – Mayank Pandeyz May 01 '19 at 14:13
  • Possible duplicate of [When to use single quotes, double quotes, and backticks in MySQL](https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks-in-mysql) – miken32 May 01 '19 at 14:59

1 Answers1

0

Two things.

1) string constants in SQL need to be surrounded by single-quote characters. So use status='active' rather than status=active.

2) 500 errors often result from error returns from database statements, possibly due to incorrectly formed queries. You should check for errors ... something like this:

$rs = $db->query('...
if (!$rs) {
      die "query failed: " . $db->error;
}

It's good practice always to check for these errors.

O. Jones
  • 103,626
  • 17
  • 118
  • 172