繁体   English   中英

通过php输出时,MySQL-PHP语法错误(似乎没有)

[英]MySQL-PHP syntax error when outputting via php (there seems to be none)

由于某种原因,我的代码将无法通过,请确保该语法正确,但不会通过。 请帮忙!

insert into users(username, name, password, type, accounts_prefix, comments, 0, 1, status) values('testuser', 'Testuser', 'abc2', 'RSLR', 'tes', 'testuseremailcom', 'username_owner', null, 'A')

您的SQL语法有误; 检查与您的MySQL服务器版本相对应的手册以获取正确语法以在'0,1,status)值('testuser','Testuser','abc2','RSLR','tes','testuserema'附近使用1行

编辑:插入功能:

    function db_insert( $table, $values )
{
    if( count($values) == 0 ) return FALSE;

    $sql = "insert into $table(";
    foreach( $values as $name => $value )
    {
        $sql .= $name.", ";
    }

    $sql = substr($sql, 0, strlen($sql) - 2).") values(";
    foreach( $values as $name => $value )
    {
        if( gettype( $value ) == "string" )
        {
            if( $value == "[null]" )
                $sql .= "null, ";
            else
                $sql .= "'".$value."', ";
        }
        else
            $sql .= $value.", ";
    }
    $sql = substr($sql, 0, strlen($sql) - 2).")";

    echo $sql."|";
    $result = mysql_query($sql);
    echo mysql_error()."|";
    return $result;
}

插入命令

if( !db_insert("users", array(
        "username" => $_REQUEST["r_username"],
        "name" => $_REQUEST["r_name"],
        "password" => $_REQUEST["r_password"],
        "type" => "RSLR",
        "accounts_prefix" => $_REQUEST["r_prefix"],
        "comments" => $_REQUEST["r_comments"],
        "username_owner", $_REQUEST["r_username_owner"],
        "status" => "A")
    ) )
    {
        echo("NOT OK Failed to add"); }

最可能是因为0和1不应该是列名

使用反引号转义特殊关键字(在这种情况下为'0'和'1'),如下所示:

insert into users(username, name, password, type, accounts_prefix, comments, `0`, `1`, status)
values('testuser', 'Testuser', 'abc2', 'RSLR', 'tes', 'testuseremailcom', 'username_owner', null, 'A')

实际上,最佳做法是始终对行名使用反引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM