使用撇号插入MySQL失败


MySQL insert fails with apostrophe

由于我的网站主机已经更新了服务器上的php从5.2到5.3我无法更新数据库中的任何字段,如果文本包含撇号。我已经尝试使用mysql_real_escape_string()没有成功。

我试过了....

    $id = uniqid();
    $case_account = $_POST['case_account'];
    $contact = ucwords($_POST['contact']);
    $subject = mysqli_real_escape_string(ucfirst($_POST['name']));
    $desc = mysqli_real_escape_string(ucfirst($_POST['description']));
    $resolution = mysqli_real_escape_string(ucfirst($_POST['resolution']));
    $account_name = $_POST['case_account_name'];
    $entered_by = $_POST['entered_by'];
    $sql="INSERT INTO cases (id, account_id, name, description, resolution, account_name1, created_by, date_entered) VALUES ('$id', '$case_account','$subject', '$desc', '$resolution', '$account_name', '$entered_by', NOW())";
    $result = mysqli_query($sql)or die(mysqli_error());

我也试过在实际查询中使用它(我只试过round $subject to test)。

$sql="INSERT INTO cases (id, account_id, name, description, resolution, account_name1, created_by, date_entered) VALUES ('$id', '$case_account',".mysqli_real_escape_string."('$subject'), '$desc', '$resolution', '$account_name', '$entered_by', NOW())";
        $result = mysql_query($sql)or die(mysql_error());

我还尝试将数据库中的字段从varChar更改为文本,然后再返回,但没有成功。我知道这应该是简单的,但由于某些原因,我不能使它工作。

mysqli_real_escape_string: mysqli_real_escape_string ( mysqli $link, string $escapestr )
其他mysqli函数也需要同样的处理。

经验法则:如果某些功能没有按预期工作- 首先检查手册页

同样,您需要使API函数的使用保持一致。选择一个API并只使用它的函数,要么mysqli_*,要么mysql_*,但不能同时使用。

你试过使用addslashes()吗?