【发布时间】: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