在会话中,我无法将数据插入到我的数据库中

时间:2016-02-21 12:33:23

标签: php html

我的数据库中有表usersregister。我创建了一个使用users表启动会话的登录页面,然后人们填写表格以将数据插入register表。我使用以下代码插入数据。我的代码没有错误,但问题是它没有插入到我的表中。请帮我。谢谢。

<?php
    include("includes/db.php");
    session_start();

    if(!isset($_SESSION['user_name'])){        
        header("location: login.php");
    }
    else { ?>
    <html>
    <body>
        <h2>New users Signup!</h2>
        <form action="login.php" method="post">
            <input type="text" name = "firstname" placeholder="Firstname"/>
            <input type="text" name = "lastname" placeholder="Lastname"/>
            <input type="text" name = "address" placeholder="Address"/>
            <input type="text" name = "contact" placeholder="Contact"/>
            <input type="text" name = "email" placeholder="Email Address"/>
            <input type="password" name = "password" placeholder="Password"/>
            <div class = "bttn">
            <button type="submit" name = "submit" class="btn btn-default">Signup</button>
            </div>
        </form>
    </body>
    </html>
    <?php
    if(isset($_POST['submit']))
    {
        $users_firstname = $_POST['firstname'];
        $users_lastname = $_POST['lastname'];
        $users_address = $_POST['address'];
        $users_contact = $_POST['contact'];
        $users_email = $_POST['email'];
        $users_password = $_POST['password'];
        $users_date = date('Y-m-d');

        if($users_firstname=='' or $users_lastname=='' or $users_address=='' or $users_contact=='' or $users_email=='' or $users_password=='')
        {
            echo "<script>alert('Any of the fields is empty')</script>";
            exit();
        } else {
            $insert_query = mysql_query("insert into users (users_firstname,users_lastname,users_address,users_contact,users_email,users_password,users_date) values ('$users_firstname','$users_lastname','$users_address','$users_contact','$users_email','$users_password','$users_date')");
            $users_id=mysql_insert_id();

            if(mysql_query($insert_query)) {
                echo "<script>alert('post published successfuly')</script>";
            }
        }
    }
} ?>

3 个答案:

答案 0 :(得分:0)

  1. 您应该删除html代码中的空格:

    <强>错误

      <input type="text" name = "firstname" placeholder="Firstname"/>
                            ^^^^^
    

    <强>正确

      <input type="text" name="firstname" placeholder="Firstname"/>
    
  2. 不要将变量放在单引号中:

    $insert_query = mysql_query("INSERT INTO users 
    (users_firstname,users_lastname,users_address,users_contact,users_email,users_password,users_date) 
    VALUES 
    ($users_firstname,$users_lastname,$users_address,$users_contact,$users_email,$users_password,$users_date)");
    
  3. 更新:这是错误的。整个字符串是双引号,因此OP确实正确,我的通知是错误的。出于信息目的,我将在此处提供文档的链接。

      

    注意:与双引号和heredoc语法不同,特殊字符的变量和转义序列在单引号字符串中不会扩展。

    详细了解PHP documentation中的单引号和双引号。

    1. 不要双重运行查询/执行错误的函数调用

      $insert_query = mysql_query(".......");
      ........
      if(mysql_query($insert_query)){
          echo "<script>alert('post published successfuly')</script>";
      }
      

      您已在第一行运行查询。如果要检查它是否成功,则必须使用if($insert_query) {}。调用mysql_query($insert_query)是错误的,因为mysql_query()返回一个ressource而不是sql-query。

    2. 不要使用mysql_*()函数调用。您改为mysqli_*()

        

      警告   此扩展在PHP 5.5.0中已弃用,并且已在PHP 7.0.0中删除。相反,应该使用MySQLi或PDO_MySQL扩展。另请参阅MySQL:选择API指南和相关的常见问题解答以获取更多信息。该功能的替代方案包括:   mysqli_query()   PDO ::查询()

    3. 检查您对session

    4. 的使用情况

      您正在检查$_SESSION user_name,如果未设置,则通过header("location: login.php")进行重定向。 问题是,您永远不会将user_name插入会话中,因此始终不会设置。

      您可以通过$_SESSION['user_name'] = $_POST['user_name']设置值。请记住,您必须在检查会话值之前设置会话。 ; - )

答案 1 :(得分:0)

现在试试这段代码:

<?php
        include("includes/db.php");
    session_start();

    if(!isset($_SESSION['user_name'])){

    header("location: login.php");
    }
    else {

    ?>
    <html>
    <body>


        <?php
    if(isset($_POST['submit']))
    {

          $users_firstname = $_POST['firstname'];
          $users_lastname = $_POST['lastname'];
          $users_address = $_POST['address'];
          $users_contact = $_POST['contact'];
          $users_email = $_POST['email'];
          $users_password = $_POST['password'];
          $users_date = date('Y-m-d');

        if($users_firstname=='' or $users_lastname=='' or $users_address=='' or $users_contact=='' or $users_email=='' or $users_password=='')
        {
        echo "<script>alert('Any of the fields is empty')</script>";
        exit();
        }

        else 
        {
            $insert_query = mysql_query("INSERT INTO `users` (users_firstname, users_lastname, users_address, users_contact, users_email, users_password, users_date) values ('$users_firstname', '$users_lastname', '$users_address', '$users_contact', '$users_email', '$users_password', '$users_date')");
            $users_id=mysql_insert_id();


          echo "<script>alert('post published successfuly')</script>";

        }
    }
    ?>

        <h2>New users Signup!</h2>
        <form action="" method="post">
            <input type="text" name="firstname" placeholder="Firstname"/>
            <input type="text" name="lastname" placeholder="Lastname"/>
            <input type="text" name="address" placeholder="Address"/>
            <input type="text" name="contact" placeholder="Contact"/>
            <input type="text" name="email" placeholder="Email Address"/>
            <input type="password" name="password" placeholder="Password"/>
            <div class="bttn">
            <button type="submit" name="submit" class="btn btn-default">Signup</button>
            </div>
        </form>
    </body>
    </html>

    <?php } ?>

我有:

  1. 重新定位您的PHP代码以便插入位于表单顶部
  2. <form action="login.php"更改为<form action="",因为我们正在从同一页面执行
  3. 您的查询已经运行,因此删除了if(mysql_query...
  4. 删除表单中的空格,例如name = " nameofform"来命名=“nameofform”
  5. 我认为没有任何理由让$users_id=mysql_insert_id();,你应该对数据库中的userID使用自动增量
  6. 但是,由于我们不知道您如何连接到您的数据库,因为这也是一个问题:您也可以尝试这种方式

     <?php
    //connect to DB
    
    $hostname_localhost = "localhost"; //hostname if it is not localhost
    $database_localhost = "databasename";
    $username_localhost = "root"; //the username if it is not root
    $password_localhost = "password_if_any"; //if no password leave empty
    $localhost = mysql_pconnect($hostname_localhost, $username_localhost, $password_localhost) or trigger_error(mysql_error(),E_USER_ERROR); 
    ?>
    
    <?php 
        // include("includes/db.php");     
        if (!isset($_SESSION)) {
         session_start();
        }
        if(!isset($_SESSION['user_name'])){
        header("location: login.php");
        }
        else {
    ?>
        <html>
        <body>
    
    
    <?php
        if(isset($_POST['submit']))
        {
    
              $users_firstname = $_POST['firstname'];
              $users_lastname = $_POST['lastname'];
              $users_address = $_POST['address'];
              $users_contact = $_POST['contact'];
              $users_email = $_POST['email'];
              $users_password = $_POST['password'];
              $users_date = date('Y-m-d');
    
            if($users_firstname=='' or $users_lastname=='' or $users_address=='' or $users_contact=='' or $users_email=='' or $users_password=='')
            {
            echo "<script>alert('Any of the fields is empty')</script>";
            exit();
            }
    
            else 
            {
    
    
    $insert_query = sprintf("INSERT INTO `users` (users_firstname, users_lastname, users_address, users_contact, users_email, users_password, users_date) values ('$users_firstname', '$users_lastname', '$users_address', '$users_contact', '$users_email', '$users_password', '$users_date')");
    
    mysql_select_db($database_localhost, $localhost);
    $Result = mysql_query($insert_query, $localhost) or die(mysql_error());
    
    
              echo "<script>alert('post published successfuly')</script>";
    
            }
        }
        ?>
    
            <h2>New users Signup!</h2>
            <form action="" method="post">
                <input type="text" name="firstname" placeholder="Firstname"/>
                <input type="text" name="lastname" placeholder="Lastname"/>
                <input type="text" name="address" placeholder="Address"/>
                <input type="text" name="contact" placeholder="Contact"/>
                <input type="text" name="email" placeholder="Email Address"/>
                <input type="password" name="password" placeholder="Password"/>
                <div class = "bttn">
                <button type="submit" name="submit" class="btn btn-default">Signup</button>
                </div>
            </form>
        </body>
        </html>
    
        <?php } ?>
    

答案 2 :(得分:-1)

删除action
试试这个

 <form action="" method="post">