在mysql中使用php插入带有特殊字符的名称

时间:2013-02-16 16:40:30

标签: php mysql apostrophe

我正在尝试使用php将一些带有特殊字符的名称(例如撇号)插入到mysql数据库中。例如:

INSERT INTO PLAYERS VALUES('{$players[$n][1]}','$nom_team');

其中$players是一个包含N'ZogbiaN'Diaye等名称的数组。但是,这会导致错误。

我尝试过使用addslashes函数:

$players[$n][1] = addslashes( $players[$n][1] );

但这不能正常工作。保存在数据库中的内容是N\'Zogbia,有时是N\\\\\\\\\\'Diaye

我已经没有想法来解决这个问题。我希望你们能帮助我。

2 个答案:

答案 0 :(得分:0)

有两种可能的情况

  1. 你有魔术引号。 Turn them off.
  2. 在您的所有输入数据上运行某种 Sanitize Them All 功能。摆脱它。

答案 1 :(得分:-1)

您可以使用预备声明。

<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$city = "N'Zogbia";

/* create a prepared statement */
if ($stmt = mysqli_prepare($link, "SELECT District FROM City WHERE Name=?")) {

    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $city);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* bind result variables */
    mysqli_stmt_bind_result($stmt, $district);

    /* fetch value */
    mysqli_stmt_fetch($stmt);

    printf("%s is in district %s\n", $city, $district);

    /* close statement */
    mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
?>
相关问题