无法从HTML表单中将值插入数据库

时间:2016-08-06 07:24:59

标签: php html

我无法从HTML表单向数据库插入值。这是我的代码:

http://pastebin.com/HhynqRnF

有人可以帮帮我吗?谢谢!

2 个答案:

答案 0 :(得分:1)

首先,您需要创建mysql connection。并将代码更改为此代码。

    <?php 
       $con = mysqli_connect("localhost", "root", "", "my_db"); //create connection
        if(isset($_POST['submit']))
        {        

          $name = $_POST['fullname'];
          $username = $_POST['username'];
          $email = $_POST['email'];
          $password = $_POST['password'];
          $sql = "INSERT INTO users (name, username, email, password) VALUES ('$name', '$username', '$email', '$password')";
          $result = mysqli_query($con, $sql);
        }
      ?>

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


            <div class="field-wrap">
              <label>
                Name<span class="req">*</span>
              </label>
              <input name="fullname" type="text" required autocomplete="off" />
            </div>

            <div class="field-wrap">
              <label>
                Username<span class="req">*</span>
              </label>
              <input name="username" type="text" required autocomplete="off" />
            </div>


          <div class="field-wrap">
            <label>
              Email Address<span class="req">*</span>
            </label>
            <input name="email" type="text" required autocomplete="off"/>
          </div>

          <div class="field-wrap">
            <label>
              Set A Password<span class="req">*</span>
            </label>
            <input name="password" type="password" required autocomplete="off"/>
          </div>

          <button type="submit" class="button button-block"/>Create Account</button>



          </form>

答案 1 :(得分:0)

尝试使用此

$sql = "INSERT INTO `users` (`name`, `username`, `email`, `password`) VALUES ('$name', '$username', '$email', '$password')";