【问题标题】:Not working mysql_query in php (quotation marks probs)在 php 中无法使用 mysql_query(引号问题)
【发布时间】:2011-04-20 05:51:16
【问题描述】:
$ikona = "layout/achiv/a_icon.png";
//$opis = string of text without quotation marks

$addit = '<img src="'.$ikona.'" onclick="alert(/'On day '.date("Y-m-d H:i:s").' user has '.htmlspecialchars($opis).'/'); ">';

mysql_query("UPDATE `accounts` SET `this_damn_cell`='".$addit."'
WHERE id='".$_POST["id"]."' ") or die(mysql_error()); //error is not showing up

echo $addit; //shows correctly

它似乎工作正常,但在 sql base 中没有添加任何内容。所有字段都存在。 this_damn_cell 类型为 TEXT 如有任何帮助,请提前感谢:)

【问题讨论】:

    标签: mysql quotes


    【解决方案1】:

    您的代码一直读取 MySQL 注入。

    对于mysql_escape_string 的快速修复,请将您的代码更改为以下内容:

    $sql = sprintf('UPDATE accounts 
                    SET this_damn_cell = %s
                    WHERE id='%i", mysql_escape_string($addit), mysql_escape_string($_POST['id']);
    
    mysql_query($sql) or die(mysql_error()); //error is not showing up
    

    但是请阅读一些准备好的语句。使用 PDO 或 MySQLi。

    Taken from PHP site 这是一个使用 MySQL 绑定的简单示例(它确实可以防止 MySQL 注入和您面临的那种错误)。

    $stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
    $stmt->bind_param('sssd', $code, $language, $official, $percent);
    

    【讨论】:

    • 我需要做一些改进,但效果很好。谢谢! :)
    猜你喜欢
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 2011-07-11
    • 2012-10-28
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多