php - 使用 PHP 将数据从表单插入 MySQL 数据库

标签 php html mysql forms

我创建了一个 HTML 表单,需要将输入表单的数据直接插入 mySQL 中的表中。

newuser.php 文件

    <?php
//including the connection page
include('./DB_Connect.php');

//get an instance
$db = new Connection();

//connect to database
$db->connect();

  //fetch username and password
  $usertype =  $_POST['userType'];
  $firstname =  $_POST['firstName'];
  $lastname =  $_POST['lastName'];
  $username =  $_POST['userName'];
  $password =  $_POST['password'];
  $address1 =  $_POST['add1'];
  $address2 =  $_POST['add2'];



  //write the sql statement
  $query = "INSERT INTO USERS (usertype, fname, lname, username, password, add1, add2)
  VALUES ('$usertype', '$firstname', '$lastname', '$username', '$password', '$address1', '$address2')";




mysql_query($query,$db);



if (($query) == TRUE) {
  echo "New record created successfully";
   header("Location: login.php", true);
   exit();
}
 else
{
  echo "Error: " . $sql . "<br>" . $conn->error;
   header("Location: register.php", true);
 exit();
}



  //close once finished to free up resources
  $db->close();


?>

使用以下 html 表单:

    <form action="newuser.php" method="POST" class="form" id="registerForm">
        <label> Type of user: </label> <br>
        <input type="radio" name="userType"  id="itemSeeker">Item Seeker    </input>
        <input type="radio" name="userType"  id="itemDonor">Item Donor </input>
        <input type="radio" name="userType"  id="peopleSeeker">People Seeker </input>
        <input type="radio" name="userType"  id="peopleDonor">People Donor </input>
        <br>
        <br>



        <div class="form-inline">
            <div class="form-group">
                <input type="text" name="firstName" placeholder="First Name: "  align="center" class="form-control" ></input>
            </div>
            <div class="form-group">
                <input type="text" name="lastName" placeholder="Last Name: "  align="center" class="form-control" ></input>
            </div>
        </div>
<br>

        <input type="text" name="email" placeholder="Email Address: "align="center" class="form-control" ></input>
    <br>



    <div class="form-inline">
            <div class="form-group">
                    <input type="text" name="userName" placeholder="Username: " align="center" class="form-control" ></input>
            </div>
            <div class="form-group">
                    <input type="password" name="password" placeholder="Password: " align="center" class="form-control" ></input>
            </div>
    </div>
    <br>

  <!-- <label> Address 1: </label>-->
        <input type="text" name="add1" placeholder="Address 1: " align="center" class="form-control" ></input>
  <!-- <label> Address 2: </label>-->
        <input type="text" name="add2" placeholder="Address 2: " align="center" class="form-control" ></input>
        <br>
  <button class="btn btn-primary" name="submitReg" type="submit">Submit</button><br>
  <br>
  <a href="login.php" >Already have an account?</a>

</form>

USERS 表

my table in SQL

上面的两个代码块和我正在使用的代码。

我的问题是,提交表单时,数据实际上并未输入到表中。请注意,第一次提交确实有效。第一次提交之后的所有提交似乎都没有将任何内容输入数据库。

我不太确定我的代码有什么问题导致它无法工作。它确实会转到“login.php”页面,这意味着没有任何错误并且查询已正确提交。

有人可以告诉我我做错了什么吗,谢谢。

最佳答案

现在你遇到的麻烦比插入问题还要多。你的代码完全不安全。 (mysql注入(inject))。

不要使用 mysql_* 函数,使用 pdo 代替准备好的语句!

在发送 header 之前输出空格,如果有输出,则无法发送 header 。你的重定向不起作用。不要中继客户端重定向,使用可能会禁用它,因此输出您希望用户前往的链接。

另一个你的单选按钮没有值检查 html 语法 var_dump($_POST) 并检查您是否提交了所有内容。在分配变量之前还要检查 isset 或空。进行某种验证。

看看一些 php 框架,它们提供了更多的灵 active 和错误检查

不要在落后程序10年或更长时间的情况下自己编写所有内容来重新发明轮子

关于php - 使用 PHP 将数据从表单插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36978926/

相关文章:

php - mysql搜索标题、描述和多行标签(关于条件)

php - 如何解码 Facebook 链接

php - 使用 Bootstrap 导航栏重叠 div

html - 在对齐 Chrome 中遗留的元素符号时遇到问题

javascript - IndexedDB 支持类似 SQL 的事务吗?

mysql - varchar(50) 可以插入的最大数据大小是多少;

php - Laravel 邮件队列 : change transport on fly

html - 无法弄清楚如何设置这些图标的样式,使它们具有不同的颜色

javascript - 哪一种是在 mysql 中删除和更新查询的最佳方法?

php - EasyPHP win7 服务器上的 Kohana Web 应用程序中的 SQL 查询速度极慢