2

I have the following sql update statement and the variables are being sanitized prior. The information is coming from a form and the database columns are TEXT. INSERTS work flawlessly.

DB Structure:

file_title=text file_description=text id=INT

mysqli_query($con,"UPDATE images
        SET file_title=$FileTitle, file_description=$FileDescription
        WHERE id=$image_id");

For some unknown reason I can only update using numeric values as it just won't update using any text which is frustrating.

I must just be missing something really basic. Any advice would be appreciated.

Daniel
  • 143
  • 1
  • 2
  • 6

4 Answers4

1

You need to quote text and string values:

mysqli_query($con,"UPDATE images
        SET file_title='$FileTitle', file_description='$FileDescription'
        WHERE id=$image_id");

Numeric values could be passed without quoting.

ozahorulia
  • 9,798
  • 8
  • 48
  • 72
  • Thanks. I just put to together mysqli_query($con,"UPDATE images SET file_title='".$FileTitle."', file_description='".$FileDescription."' WHERE id='".$image_id."'"); and it seems to work now. – Daniel Feb 28 '13 at 09:58
1

because string needs to be wrapped with single quotes. Single quotes are for string literals while backtick are for identifiers,

UPDATE images
SET    file_title='$FileTitle', file_description='$FileDescription'
WHERE id=$image_id

As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.

Community
  • 1
  • 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

Use single quote for text values.

mysqli_query($con,"UPDATE images
        SET file_title='$FileTitle', file_description='$FileDescription'
        WHERE id=$image_id");
Edwin Alex
  • 5,118
  • 4
  • 28
  • 50
0

CHAR values must be quoted, but more importantly you should be using prepared statements:

$stmt = mysqli_prepare($con,"UPDATE images
    SET file_title=?, file_description=?
    WHERE id=?");
mysqli_stmt_bind_param($stmt, 'ssd', $FileTitle, $FileDescription, $image_id);
mysqli_stmt_execute($stmt);
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309