English 中文(简体)
MySQL 插入错误
原标题:MySQL Insert error

Ok在试图将这一错误插入数据库时

"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 @email.com, UT, 84505, NOW(), 69.169.186.192) at line 1"

我可以指出这个问题。 下面是我的发言守则。

$insert_query = sprintf("INSERT INTO contacts (first_name, last_name, email, state, zip, date, ip) VALUES (%s, %s, %s, %s, %s, NOW(), %s)",
                        $fname,
                        $lname,
                        $email,
                        $state,
                        $zip,
                        $ip);


$result = mysql_query($insert_query, $connection) or die(mysql_error());

我的议席结构如下:

    id int(11)                              
    first_name  varchar(100)                                 
    last_name   varchar(100)                             
    email   varchar(100)                                 
    state   varchar(3)                               
    zip int(10)                             
    date    datetime                                
    ip  varchar(255)
最佳回答

您需要引用说明中所有类型的栏目。 将<代码>%<>/code>改为 %s in the sprintf Format.

Please read about SQL Injection if you haven t done so already.

问题回答

这可能有助于你。

$insert_query = "INSERT INTO contacts set first_name =  $fname , last_name =  $lname , email =  $email , state =  $state , zip =  $zip , date = ". time() .", ip =  $ip )";


$result = mysql_query($insert_query, $connection) or die(mysql_error());

如果你想要检查询问

echo $insert_query;

如果你能够回过头来,那将有所帮助,但是,它希望你不要在var的参数上引用。

$insert_query = sprintf("INSERT INTO contacts (first_name, last_name, email, state, zip, date, ip) VALUES ( %s ,  %s ,  %s ,  %s ,  %s , NOW(),  %s )",
                        $fname,
                        $lname,
                        $email,
                        $state,
                        $zip,
                        $ip);

By the way, you have an extra column in your insert - NOW doesn t appear related to a column. I m assuming ZIP is a varchar column, not a number, by the way.





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签