【问题标题】:Inserting data with PHP from a form to MySQL database使用 PHP 将数据从表单插入到 MySQL 数据库
【发布时间】:2016-05-02 09:14:49
【问题描述】:

我创建了一个 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 表

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

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

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

谁能告诉我我做错了什么,谢谢。

【问题讨论】:

  • 使用ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);检查页面错误
  • 请分享db表的视图..
  • 检查这个条件if (($query) == TRUE) {我认为$queryINSERT INTO..
  • 能否提供数据库表的屏幕。
  • 据我所见,您实际上并没有检查查询的状态/成功,现在您正在检查 $query 是否为真,因为它包含数据. - 如果您改为检查查询执行的状态,我想您会看到一个错误。

标签: php html mysql forms


【解决方案1】:

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

不要使用 mysql_* 函数,而是使用 pdo 和准备好的语句!

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-30
    相关资源
    最近更新 更多