我有一些代码将值输入数据库。我在开始时有一个IF语句来检查字符串$jobno
是否为空,如果是,则重定向回到表单。但它不重定向只是成功运行代码。
if ($jobno=='') {
header( 'Location: add_job.php?error=1');
}
我做错了什么?!
<?php
$status=$_POST["status"];
$jobno=$_POST["jobno"];
$number=$_POST["number"];
$street=$_POST["street"];
$suburb=$_POST["suburb"];
$city=$_POST["city"];
$first_name=$_POST["first_name"];
$first_name = ucfirst($first_name);
$last_name=$_POST["last_name"];
$last_name = ucfirst($last_name);
$landline=$_POST["landline"];
$mobile=$_POST["mobile"];
$fax=$_POST["fax"];
$email=$_POST["email"];
if ($jobno=='') {
header( 'Location: add_job.php?error=1');
}
$con=mysqli_connect("server","user","pass","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to database: " . mysqli_connect_error();
}
mysqli_query($con,"INSERT INTO jobs (status, jobno, number, street, suburb, city, first_name, last_name, landline, mobile, fax, email)
VALUES ('$status', '$jobno', '$number', '$street', '$suburb', '$city', '$first_name', '$last_name', '$landline', '$mobile', '$fax', '$email')");
mysqli_close($con);
header( 'Location: photo_upload.php?new_job_success=y&jobno=' . $jobno ) ;
?>
答案 0 :(得分:2)
<?php
^---this space
是你的代码的外部,所以它是输出,这使得
header( 'Location: add_job.php?error=1');
导致“已发送标头”错误。
你也很开放并且正在寻求SQL injection attack
答案 1 :(得分:1)
可能是两件事
在代码中添加else
子句:
<?php
...
if ($jobno=='') {
header( 'Location: add_job.php?error=1');
}
else {
$con=mysqli_connect("server","user","pass","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to database: " . mysqli_connect_error();
}
else
{
mysqli_query($con,"INSERT INTO jobs (status, jobno, number, street, suburb, city,first_name, last_name, landline, mobile, fax, email) VALUES ('$status', '$jobno', '$number', '$street', '$suburb', '$city', '$first_name', '$last_name', '$landline', '$mobile', '$fax', '$email')");
mysqli_close($con);
header( 'Location: photo_upload.php?new_job_success=y&jobno=' . $jobno ) ;
}
}
?>
答案 2 :(得分:0)
启动中有一个空间导致此错误。在发送标题之前,您不能回显任何输出,甚至不是一个空格。这个小空间导致头部已经发送错误。你应该删除php文件启动时的空间。您的代码应如下所示
<?php
$status=$_POST["status"];
我希望这有助于你
答案 3 :(得分:-1)
我认为你必须使用 isset 功能。 http://php.net/manual/en/function.isset.php
答案 4 :(得分:-1)
尝试使用此代码:
if (!isset($_POST["jobno"])) {
header( 'Location: add_job.php?error=1');
}