Search code examples
phpmysqlreserved-words

Delete record in MySQL with php targeting auto_incremented int?


Why doesnt this delete work to delete the whole record:

$query = 'DELETE FROM tblEvents WHERE index = $_GET["id"]';
$result = mysql_query($query, $db) or die(mysql_error($db));

Where index is variable of type int, auto_incremented in MySQL?


Solution

  • Your question php is related, not mysql.
    print $query; and see.
    then refer to php strings syntax, http://php.net/types.string for the proper syntax.

    Also, a variable that goes to the query, must be properly prepared, escaped, or, in case of integer value, manually cast to this type,

    $id=intval($_GET["id"]);
    

    or, to make it single line,

    $query = 'DELETE FROM tblEvents WHERE `index` = '.intval($_GET["id"]);
    

    also, index is reserved word that can cause problems too, you can escape it with backticks,

    `index`
    

    but it will be much better if you rename it to just id