PHP:设置会话时数据没有插入数据库?

时间:2016-03-20 12:54:06

标签: php mysql session

我在PHP和英语方面也不擅长。但在这里我会尽力让你理解我的问题。我的问题是当我在addproduct页面中插入数据而没有设置会话时它工作成功并没有问题在里面。但是当我设置会话并且user_status不是客户时,然后将数据插入到它不起作用的同一页面并将我发送到索引页面,我不知道为什么它将我重定向到该页面< b>更新:

当我尝试进入此页面而未设置会话或我的状态为客户时,此条件成功运行


这是一些addproduct.php页面的代码

     <?php //Condition for sessions
if(!isset($_SESSION['user_email']) || $_SESSION['user_status']=="customer") {
   echo "session is not set";
}

else
{
    //else it will stay in this page (means that session is set and user_status is not customer)


?>
<html>
<head>

    <meta charset="UTF-8">
    <title>Insert form data</title>
</head>
<body>
 //form for data insertion
<form method="post" action ="addproduct.php" id="contact-form">

    <textarea id = "address" name="product_title" placeholder="product_title"  required /></textarea>

    <input type="text" name="product_category" placeholder="product_category"  required />



    <input type="text" name="product_name"  placeholder="product_name" required />
    <input type="text" name="product_brand"  placeholder="product_brand" required />

    <textarea id = "address" name="product_description" placeholder="product_description"  required /></textarea>


    <input type="text" name="product_price"  placeholder="product_price" required />

    <input type="text" name="product_discount_price"  placeholder="product_discount_price" required />


    <input type="text" name="product_quantity"  placeholder="product_quantity" required />



    <input type="text" name="product_image1"  placeholder="product_image1" required />


    <input type="text" name="product_image2"  placeholder="product_image2" required />

    <input type="text" name="product_image3"  placeholder="product_image3" required />




    <div class="btn-group" role="group">
        <input type="submit" class="btn btn-default" name="Add" value="Enter the box" style="margin-top: 15px; margin-right: 15px; border-radius: 4px;">

    </div>

</form>

<?php
include("db.php");

if(isset($_POST['Add'])) //when Post an Ad is clicked. Then it store it in database
{
    $product_title = ($_POST['product_title']);

    $product_category = $_POST['product_category'];
    $product_name = ($_POST['product_name']);
    $product_description = ($_POST['product_description']);
    $product_brand =  ($_POST['product_brand']);
    $product_price = ($_POST['product_price']);
    $product_discount_price = ($_POST['product_discount_price']);
    $product_quantity = ($_POST['product_quantity']);
    $product_image1 = ($_POST['product_image1']);
    $product_image2 = ($_POST['product_image2']);
    $product_image3 = ($_POST['product_image3']);


    //SQL query

    $sql = "INSERT INTO ecommerce.product (product_title,product_category,product_name,product_description,product_price,
product_quantity,product_image1,product_image2,product_image3,product_discount_price,product_brand)
VALUE ('$product_title','$product_category','$product_name','$product_description','$product_price','$product_quantity','$product_image1',
'$product_image2','$product_image3','$product_discount_price','$product_brand')";

    if(mysqli_query($conn,$sql) && isset($_SESSION['user_email']))
    {
        mysqli_select_db($conn,"product");

        $last_id = mysqli_insert_id($conn);


       header("location:showAds.php?id=$last_id");//If sql-query is executed seccessfully then it will redirect to showAdd.php page
    }
    else
       echo "error";


       $conn->close();
}




?>
</body>
</html>
<?php } // end of else
?>

更新:
这是注册页面

<?php
session_start();
if(isset($_SESSION['user_fname']) && isset($_SESSION['user_lname']) && isset($_SESSION['user_email'])) {
    header("location:index.php");
}
else
{

?>





    <html>

    <head><title>Registration</title></head>
    <body>
    <form action="UserSignup.php" method="post">
        <input type="text" name="user_fname" placeholder="First Name" REQUIRED>
        <input type="text" name="user_lname" placeholder="Last Name" REQUIRED>
        <input type="email" name="user_email" placeholder="User Email" REQUIRED>
        <input type="password" name="user_password" placeholder="USER PASSWORD" REQUIRED>
        <input type="hidden" name="user_status" value="customer" REQUIRED>
        <input type="submit" name="Go" value="SUBMIT!" REQUIRED>
        </br></br><a href="login.php">Already have an account?</a></br>



    </form>

    <?php
    include("db.php");
    /*if(isset($_POST['Go'])) {    SIGNUP
        $user_name = $_POST['user_name'];
        $user_password = $_POST['user_password'];
        $user_email = $_POST['user_email'];
        echo $user_name . "<br>";
        echo $user_email . "<br>";
        echo $user_password . "<br>";
        $sql = "INSERT INTO user(user_name,user_email,user_password) VALUE ('$user_name','$user_email','$user_password')";
        if(mysqli_query($conn,$sql))
        {
            echo "stored";
            header("location:http://localhost/window.php");
        }
        else
        {
          die(mysqli_error($sql));
        }
    }*/
    if(isset($_POST['Go']))
    {
        $user_fname = $_POST['user_fname'];//real_escape_string
        $user_lname = $_POST['user_lname'];
        $user_email = $_POST['user_email'];
        $user_password = $_POST['user_password'];
        $user_status = $_POST['user_status'];

        $sql = "INSERT INTO user(user_fname,user_lname,user_password,user_status,user_email) VALUE ('$user_fname','$user_lname','$user_password','$user_status','$user_email')";
        if(mysqli_query($conn,$sql))
        {
            $last_id = mysqli_insert_id($conn);
            //echo $last_id;
            $login_query="SELECT * FROM user WHERE user_email='$user_email' AND user_password='$user_password'  AND user_id='$last_id'";
            $run=mysqli_query($conn,$login_query);
            if(mysqli_num_rows($run)>0)

            {
                //$res = mysqli_query($conn, "SELECT * FROM ecommerce.user WHERE user_email='$user_email'");
                while ($record = mysqli_fetch_array($run)) {

                    $_SESSION['user_fname']=$record['user_fname'];
                    $_SESSION['user_lname'] = $record['user_lname'];
                    $_SESSION['user_status'] = $record['user_status'];
                    $_SESSION['user_email']=$user_email;
                    $_SESSION['user_id']=$last_id;



                }



                if(isset($_SESSION['user_email']))
                header("location:index.php");



               // header("location:window.php");
            }


         //  header("location:index.php");
        }
        else
        {
            echo "error";
        }
    }




    ?>

    </body>
    </html>
<?php }?>

这是登录页面

<?php
session_start();
if(isset($_SESSION['user_fname']) && isset($_SESSION['user_lname']) && isset($_SESSION['user_email'])) {
    header("location:index.php");
}
    else
    {

?>

<html>

<head><title>Login</title></head>
<body>
<form action="login.php" method="post">
    <input type="email" name="user_email" placeholder="USER EMAIL" REQUIRED>
    <input type="password" name="user_password" placeholder="USER PASSWORD" REQUIRED>

    <input type="submit" name="Go" value="SUBMIT!" placeholder="USER NAME" REQUIRED>
    </br></br><a href="UserSignup.php">SignIn with new account</a></br>



</form>

<?php
include("db.php");
/*if(isset($_POST['Go'])) {    SIGNUP
    $user_name = $_POST['user_name'];
    $user_password = $_POST['user_password'];
    $user_email = $_POST['user_email'];
    echo $user_name . "<br>";
    echo $user_email . "<br>";
    echo $user_password . "<br>";
    $sql = "INSERT INTO user(user_name,user_email,user_password) VALUE ('$user_name','$user_email','$user_password')";
    if(mysqli_query($conn,$sql))
    {
        echo "stored";
        header("location:http://localhost/window.php");
    }
    else
    {
      die(mysqli_error($sql));
    }
}*/
if(isset($_POST['Go']))
{
    $user_email = $_POST['user_email'];//real_escape_string
    $user_password = $_POST['user_password'];
    $login_query="SELECT * FROM user WHERE user_email='$user_email' AND user_password='$user_password'";
    $run=mysqli_query($conn,$login_query);
    if(mysqli_num_rows($run)>0)

    {
        $res = mysqli_query($conn, "SELECT * FROM ecommerce.user WHERE user_email='$user_email'");

        while ($record = mysqli_fetch_array($res)) {

            $_SESSION['user_fname']=$record['user_fname'];
            $_SESSION['user_lname'] = $record['user_lname'];
            $_SESSION['user_status'] = $record['user_status'];
            $_SESSION['user_id'] = $record['user_id'];
            $_SESSION['user_password'] = $record['user_password'];

        }


        $_SESSION['user_email']=$user_email;
       //echo $_SESSION['user_fname'] . $_SESSION['user_lname'];


        header("location:index.php");
    }
    else
        echo "<p style='color: red; margin-top: -28px;'>User name or password incorrect</p>";
}




?>

</body>
</html>
<?php }?>

1 个答案:

答案 0 :(得分:0)

根据设置会话的条件和会话是$ _SESSION ['user_status'] ==“客户”,它将重定向,你要做的是根据职业改变你的条件