php重定向如果条件在页面加载时执行

时间:2015-04-22 03:30:21

标签: php redirect

else部分中的重定向正在页面加载时执行。我想只在执行插入时执行它。

 $conn = new mysqli($hn, $un, $pw, $db);
 if ($conn->connect_error) die($conn->connect_error);

    $query    = "INSERT INTO colombia_plaza_new (lease_owner,request_poster,email,address,start_date,end_date,expected_rent, is_deposit,notes)  VALUES" .
      "('$name','$pos_name', '$email', '$apart', '$start', '$end', '$rent', '$deposit', '$how')";


    $result   = $conn->query($query);


if (!$result) 
    {
        echo "INSERT failed: $query<br>" .
      $conn->error . "<br><br>";
      $checksum=1;
      $conn->close();
    }

    else
    {
        $conn->close();

        echo "<meta http-equiv=\"refresh\" content=\"0;URL=page3.html\">"; 
    }

4 个答案:

答案 0 :(得分:0)

您应该使用它在PHP中重定向....

header("Location: http://www.blah.com/blah_blah_blah.php");

答案 1 :(得分:0)

您的var $结果将始终具有值,因此您的if语句不正确。 尝试使用mysqli_affected_rows查看受影响的行数。 如果行影响&gt; 0重定向。

答案 2 :(得分:0)

您似乎已经犯了一些musqli错误。您可以尝试如下。

<?php
    $conn = mysqli_connect($hn, $un, $pw, $db);
    if (mysqli_connect_errno()) {
        die(mysqli_connect_error());
    }
    $query = "INSERT INTO colombia_plaza_new (lease_owner,request_poster,email,address,start_date,end_date,expected_rent, is_deposit, notes)  VALUES ('$name','$pos_name', '$email', '$apart', '$start', '$end', '$rent', '$deposit', '$how')";

    $result   = $conn->query($query);
    if(!mysqli_query($conn,$query)){
        echo "INSERT failed: ".$query."<br>";
        echo mysqli_error($conn);. "<br><br>";
        $checksum = 1;
    }else{
        mysqli_close($conn);
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=page3.html\">"; 
    }
?>

答案 3 :(得分:0)

更改条件方法。如果成功,重定向;否则表明它失败了。

$conn = new mysqli($hn, $un, $pw, $db);

 if ($conn->connect_error) {
    die('Connect Error: ' . $conn->connect_error);
}

    $query = "INSERT INTO colombia_plaza_new (lease_owner,request_poster,email,address,start_date,end_date,expected_rent, is_deposit,notes)  VALUES" .
      "('$name','$pos_name', '$email', '$apart', '$start', '$end', '$rent', '$deposit', '$how')";

    $result   = $conn->query($query);

if ($result) 
    {
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=page3.html\">";
        $conn->close();
    }

    else
    {
        echo "INSERT failed: $query<br>" .
        $conn->error . "<br><br>";
        $checksum=1;
        $conn->close();
    }

使用mysqli_affected_rows()是另一种选择。

$conn = new mysqli($hn, $un, $pw, $db);

 if ($conn->connect_error) {
    die('Connect Error: ' . $conn->connect_error);
}

    $query = "INSERT INTO colombia_plaza_new (lease_owner,request_poster,email,address,start_date,end_date,expected_rent, is_deposit,notes)  VALUES" .
      "('$name','$pos_name', '$email', '$apart', '$start', '$end', '$rent', '$deposit', '$how')";

    $result   = $conn->query($query);

    if (mysqli_affected_rows($conn) > 0){
        echo "<meta http-equiv=\"refresh\" content=\"0;URL=page3.html\">";
        $conn->close();
    }

    else
    {
        echo "INSERT failed: $query<br>" .
        $conn->error . "<br><br>";
        $checksum=1;
        $conn->close();
    }

  • 但是,另一个原因可能是您的查询可能失败了。

因此,您最好检查所有变量是否确实已填充,并且最有可能来自HTML表单,并对所有变量使用条件!empty()

此时,尚不清楚变量的分配位置和方式。


旁注:您目前的代码向SQL injection开放。使用mysqli with prepared statementsPDO with prepared statements他们更安全