PHP无法从HTML表单中选择数据

时间:2016-11-06 08:28:50

标签: php html

我的PHP页面无法从HTML表单中选择值。它将空字符串发送到数据库。这是我的HTML和PHP代码。请找错误。我是PHP的新手,无法解决问题。

my html page:
    <!DOCTYPE html>

    <html >
     <head>
       <meta charset="UTF-8">

       <title>LOGIN</title>



        <link rel="stylesheet" href="css/reset.css">


      <link rel='stylesheet prefetch' href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700,900|RobotoDraft:400,100,300,500,700,900'>

    <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'>


        <link rel="stylesheet" href="css/style.css">






      </head>



    <body>



    <!-- Mixins-->
    <!-- Pen Title-->

    <div class="pen-title">

    <h1>SYNCHPHONY</h1>
    </div>
    <div class="rerun"><a href="">Rerun Pen</a></div>
    <div class="container">

     <div class="card"></div>
      <div class="card">

       <h1 class="title">Login</h1>

     <form name="login" action="login.php" method="POST">

        <div class="input-container">

       <input type="text" id="loginid" required="required"/>


     <label for="loginid">Login ID</label>

        <div class="bar"></div>
          </div>

      <div class="input-container">


      <input type="password" id="password" required="required"/>

         <label for="password">Password</label>

      <div class="bar"></div>
          </div>

       <div class="button-container">

      <button><span>Go</span></button>
          </div>


      </form>

      </div>

    <div class="card alt">
        <div class="toggle"></div>

      <h1 class="title">Register

    <div class="close"></div>
        </h1>

       <form name="register" action="register.php" method="POST">

     <div class="input-container">

         <input type="text" id="loginid" required="loginid"/>

        <label for="loginid">Login ID</label>

    <div class="bar"></div>
          </div>

        <div class="input-container">

        <input type="password" id="password" required="required"/>

     <label for="password">Password</label>

     <div class="bar"></div>
          </div>


    <div class="button-container">

        <button value'submitb'><span>Next</span></button>
          </div>

      </form>

     </div>
    </div>


        <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>

            <script src="js/index.js"></script>



      </body>
    </html>

my php page:

**strong text**    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "syncphony";
    $loginid="";
    $password="";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    if(isset($_POST['loginid'])){ $loginid = $_POST['loginid']; }
    if(isset($_POST['password'])){ $password = $_POST['password']; }

    // Escape user inputs for security
    $loginid = mysqli_real_escape_string($conn,$loginid);
    $password = mysqli_real_escape_string($conn,$password);

    // attempt insert query execution
    $sql = "INSERT INTO users (loginid, password ) VALUES ('$loginid', '$password')";
    if(mysqli_query($conn, $sql)){
        echo "Records added successfully.";
    } else{
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
    }

    // close connection
    mysqli_close($conn);
    ?>

1 个答案:

答案 0 :(得分:1)

input标记中的form s没有name个。试试这个登录:

<input type="text" id="loginid" required="required" name="loginid"/>

这是密码:

<input type="password" id="password" required="required" name="password"/>

如果您保护用户免受XSS attacks的侵害,并在存储密码时使用encryption,那就太好了。此外,您应该构建代码并确保HTML有效。