问题与mysql_query INSERT(PHP 5.3)没有添加到表

时间:2014-05-21 22:34:33

标签: php mysql database forms mysqli

我有一个工作申请表,用于发送包含该信息的电子邮件,还应该将表单中的值(其值导入到顶部的下面的handler.php中)插入数据库中的表中在服务器上。

然而,它没有插入值,我无法弄清楚原因。

我实际上从我的客户端旧版本的网站上的不同域和服务器上获取了这个脚本,并且它仍然在那个上完美运行,所以我无法弄清楚为什么它在这里不起作用。我唯一改变的是数据库细节。有一次我认为问题可能是因为mysql_connect等被弃用而支持mysqli-equivalent,但是客户端的服务器仍在运行PHP 5.3,所以这应该不是问题。

通过PHPMyAdmin,我有一个数据库和一个名为'team'的表。我根据旧网站上的原始设置创建了所有列,因此您在下面的INSEERT查询中看到的每个项目都将有所不同。

我在SQL连接代码中遗漏了什么?用户名,密码,域和数据库都是正确的......

$date = date("d/M/Y");
$name = Trim(stripslashes($_POST['name'])); 
$dob = Trim(stripslashes($_POST['dob']));
$contact = Trim(stripslashes($_POST['contact']));
$email = Trim(stripslashes($_POST['email'])); 
$preferredCity = Trim(stripslashes($_POST['preferredCity']));
$nightsMo = Trim(stripslashes($_POST['nightsMo']));
$nightsTu = Trim(stripslashes($_POST['nightsTu']));
$nightsWe = Trim(stripslashes($_POST['nightsWe']));
$nightsTh = Trim(stripslashes($_POST['nightsTh']));
$nightsFr = Trim(stripslashes($_POST['nightsFr']));
$nightsSa = Trim(stripslashes($_POST['nightsSa']));
$nightsSu = Trim(stripslashes($_POST['nightsSu']));
$ownCar = Trim(stripslashes($_POST['ownCar']));
$previousWork = Trim(stripslashes($_POST['previousWork']));

if (Trim($nightsMo)=="") $nightsMo="NULL";
if (Trim($nightsTu)=="") $nightsTu="NULL";
if (Trim($nightsWe)=="") $nightsWe="NULL";
if (Trim($nightsTh)=="") $nightsTh="NULL";
if (Trim($nightsFr)=="") $nightsFr="NULL";
if (Trim($nightsSa)=="") $nightsSa="NULL";
if (Trim($nightsSu)=="") $nightsSu="NULL";
if (Trim($ownCar)=="") $ownCar="NULL";

$username="XXX";
$password="XXX";
$database="XXX";


mysql_connect("example.co.uk",$username,$password);

@mysql_select_db($database) or die( "Unable to select database");


mysql_query("INSERT INTO team (t_date, t_name, t_dob, t_contact, t_email, t_preferredCity, t_nightsMo, t_nightsTu, t_nightsWe, t_nightsTh, t_nightsFr, t_nightsSa, t_nightsSu, t_ownCar, t_previousWork, t_picture) VALUES ('$date', '$name','$dob','$contact','$email','$preferredCity',$nightsMo,$nightsTu,$nightsWe,$nightsTh,$nightsFr,$nightsSa,$nightsSu,$ownCar,'$previousWork', '$filePath')");

mysql_close();

1 个答案:

答案 0 :(得分:1)

不清楚原因是什么,但重构了您的代码以简化和优化简化操作,以便更容易调试。

我做的第一件事就是将所有$_POST项检查放入使用一个主数组($post_array)的结构中,然后滚动该数组以处理值&将它们分配给类似命名的变量。

此外,我为$result添加了mysql_query字符串检查,然后检查是否返回。如果失败,请通过mysql_error()告诉我们错误是什么。这应该是帮助您调试正在发生的事情的关键。

$date = date("d/M/Y");

// Set the post values array.
$post_array('name','dob','contact','email','preferredCity','nightsMo','nightsTu','nightsWe','nightsTh','nightsFr','nightsSa','nightsSu');

// Role through the post values array.
foreach($post_array as $post_key => $post_value) {
  $$post_key = isset($_POST[$post_key]) ? Trim(stripslashes($_POST[$post_key])) : null; 
}

$username="XXX";
$password="XXX";
$database="XXX";

mysql_connect("example.co.uk",$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO team (t_date, t_name, t_dob, t_contact, t_email, t_preferredCity, t_nightsMo, t_nightsTu, t_nightsWe, t_nightsTh, t_nightsFr, t_nightsSa, t_nightsSu, t_ownCar, t_previousWork, t_picture) VALUES ('$date', '$name','$dob','$contact','$email','$preferredCity','$nightsMo','$nightsTu','$nightsWe','$nightsTh','$nightsFr','$nightsSa','$nightsSu','$ownCar','$previousWork', '$filePath')";

// Run the query & return the result.
$result = mysql_query($query);

// Check the mysql query result. If there is none, there is an error. So tell us what the error is.
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

mysql_close();

另外,不相关但值得指出的是PHP {5.3}中的mysql_*扩展名已被折旧。 PHP 5.4。您应该切换到mysqli_*扩展名,因为mysql_*扩展名将在PHP 5.5中被删除。