简体   繁体   中英

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

for some reason this code of mine just wont go through, im darn right sure that the syntax of this is proper yet it just won't go. Please help!

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

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0, 1, status) values('testuser', 'Testuser', 'abc2', 'RSLR', 'tes', 'testuserema' at line 1

EDIT: the insert function:

    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;
}

the insert command

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不应该是列名

Use backticks to escape special keywords ('0' and '1' in this case), like this:

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

In fact it is a best-practice to always use backticks for column names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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