【问题标题】:Issue with mysql_query INSERT (PHP 5.3) not adding to tablemysql_query INSERT (PHP 5.3) 未添加到表中的问题
【发布时间】:2014-07-10 19:00:31
【问题描述】:

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

但是,它没有插入值,我不知道为什么。

我实际上是从我客户的旧版本网站上的不同域和服务器上获取了这个脚本,它仍然在那个网站上完美运行,所以我不知道为什么它在这里不起作用。我唯一改变的是数据库细节。在某一时刻,我认为问题可能是因为 mysql_connect 等已被弃用,有利于 mysqli-equivalents,但客户端的服务器仍在运行 PHP 5.3,所以这不应该是问题。

通过 PHPMyAdmin,我有一个数据库和一个名为“团队”的表。我根据旧网站上的原始设置创建了所有列,因此您在下面的插入查询中看到的每个项目都会有去处。

我的 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();

【问题讨论】:

  • 您收到回信了吗?在将它们放入查询之前,您是否查看过这些值是什么?
  • a) mysql_* 函数已弃用。不要使用它们。 b) 除了单个or die(),您根本没有进行任何类型的错误检查/恢复。您进行的所有 mysql 调用都可能失败,如果失败,将返回 FALSE。
  • 在开发过程中将错误报告添加到文件顶部error_reporting(E_ALL); ini_set('display_errors', 1);。您的某些值可能缺少引号。
  • $filePath 未显示在您的代码中。如果它没有被使用,那么它是未定义的并且会破坏你的查询。
  • 一个带有' 的文本字段并且您的查询中断。阅读 sql injection 并切换到 PDO 或 mysqli 和prepared statements。

标签: php mysql database forms mysqli


【解决方案1】:

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

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

此外,我为mysql_query 添加了$result 字符串检查,然后检查是否返回。如果失败,请通过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();

另外,不相关但值得指出的是mysql_* 扩展在 PHP 5.3 和 PHP 5.4 中已被贬值。你应该切换到mysqli_* 扩展,因为mysql_* 扩展在 PHP 5.5 中被淘汰了。

【讨论】:

  • 感谢@JakeGould 提供的代码,我明天早上试试,看看会发生什么。我以为 mysql_* 扩展直到后来才被弃用?
  • @tristanojbacon “我认为 mysql_* 扩展直到后来才被弃用?”这意味着您的代码将不适用于升级到 PHP 5.5。所以现在你应该努力将你的代码移动到mysqli_*(这并不难)以避免将来无意中的头痛。
  • 好的,一旦我整理好这个,我会调查的!回复:错误输出,我得到以下信息:Invalid query: Unknown column 't_date' in 'field list'
  • @tristanojbacon Invalid query: Unknown column 't_date' in 'field list' 那是你的错误。您的数据库是否有名为t_date 的列?好像没有。但是您的查询正在尝试将数据插入该列。所以修复它。祝你好运!
  • 是的!这修复了它:D 我为 t_date 添加了一个新列,它运行良好。感谢您的帮助!
猜你喜欢
  • 2015-09-22
  • 2017-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
  • 2011-04-20
相关资源
最近更新 更多