0

我将我认为是对 php 表单处理脚本的简单补充(在将数据发送到电子邮件后将数据插入数据库)放在一起。

出于某种原因,我总是收到错误消息,并且数据的实际输入永远不会进入数据库。我已将查询添加到输出中,并且所有接收到的值似乎都正常。

提前致谢。

// username, passw, database are all defined earlier in code.
// all the variables are taken out of form inputs (and they all arrive fine by email.
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

//inserting data order
$order = "INSERT INTO flights('restime', 'flyfrom', 'flyto', 'dateoutbound', 'outboundflex', 'datereturn', 'returnflex', 'pax', 'klasse', 'name', 'email', 'phone', 'comments', 'status') VALUES('$date', '$fly_from', '$fly_to', '$date_outbound', '$outbound_flex', '$date_return', '$return_flex', '$pax', '$class', '$name', '$email', '$phone', '$comments', '1')";

//declare in the order variable
$result = mysql_query($order);  //order executes
if($result){
    echo("<br>Input data is succeed");
} else{
    echo("<br>Input data is fail");
    echo $order;
}

提前非常感谢。

4

3 回答 3

0

您不应该对查询中的字段使用单引号。如果你使用反引号,那么你不会得到错误。就像下面这样

您必须使用 mysql_error() 函数来检查查询中的错误。检查下面的代码。

我又发现了一个问题。您必须在 mysql_connect 函数中的主机名周围使用单引号。这也可能导致错误。

我使用了以下代码。它对我来说很好。

<?php mysql_connect('localhost','root','');
@mysql_select_db('test') or die( "Unable to select database");





//inserting data order
$order = "INSERT INTO testone (`restime`, `flyfrom`, `flyto`, `dateoutbound`, `outboundflex`, `datereturn`, `returnflex`, `pax`, `klasse`, `name`, `email`, `phone`, `comments`, `status`) VALUES('$date', '$fly_from', '$fly_to', '$date_outbound', '$outbound_flex', '$date_return', '$return_flex', '$pax', '$class', '$name', '$email', '$phone', '$comments', '1')";

//declare in the order variable
$result = mysql_query($order) or die(mysql_error());  //order executes
if($result){
    echo("<br>Input data is succeed");
} else{
    echo("<br>Input data is fail");
    echo $order;
}
于 2013-08-19T13:38:31.713 回答
0

数据库配置设置必须如下

$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database,$link) or die( "Unable to select database"); 

在此之后,您可以编写插入查询等......

于 2013-08-19T14:03:16.980 回答
0

错误在此查询中:

$order = "INSERT INTO flights('restime', 'flyfrom', 'flyto', 'dateoutbound', 'outboundflex', 'datereturn', 'returnflex', 'pax', 'klasse', 'name', 'email', 'phone', 'comments', 'status') VALUES('$date', '$fly_from', '$fly_to', '$date_outbound', '$outbound_flex', '$date_return', '$return_flex', '$pax', '$class', '$name', '$email', '$phone', '$comments', '1')";

'尽管使用`作为表中的列名,但您仍在使用。

使用这个查询,我相信它会运行得很好

$order = "INSERT INTO testone (`restime`, `flyfrom`, `flyto`, `dateoutbound`, `outboundflex`, `datereturn`, `returnflex`, `pax`, `klasse`, `name`, `email`, `phone`, `comments`, `status`) VALUES('$date', '$fly_from', '$fly_to', '$date_outbound', '$outbound_flex', '$date_return', '$return_flex', '$pax', '$class', '$name', '$email', '$phone', '$comments', '1')";

:)

于 2013-08-19T14:19:27.717 回答