dqf60304 2014-08-15 13:35
浏览 19
已采纳

PHP If语句不重定向空字符串

I have some code that is entering values into a database. I have an IF statement at the start to check if the string $jobno is empty and if it is, it redirects back to the form. But its not redirecting just runs the code successfully.

if ($jobno=='') {
        header( 'Location: add_job.php?error=1');
    }

What am I doing wrong?!

 <?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 ) ;

?>
  • 写回答

5条回答 默认 最新

  • doukou1718 2014-08-15 13:41
    关注

    It could be 2 things

    • $jobno is not ''
    • since you aren't returning after your redirect, and you are doing a second redirect a bit later, your header would look like:
      • Location: add_job.php?error=1
      • Location: photo_upload.php?new_job_success=y&jobno=

    put an else clause in your code:

    <?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 ) ;
      }
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器