简体   繁体   English

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

[英]Inserting data with PHP from a form to MySQL database

I have created a HTML form which will need to insert the data that was entered into the form straight into a table in mySQL. 我创建了一个HTML表单,该表单需要将输入到表单中的数据直接插入到mySQL中的表中。

newuser.php file 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();


?>

With the following html form: 使用以下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>

The USERS table USERS表

我的SQL表

The above two blocks of code and the code that I'm working with. 上面的两个代码块以及我正在使用的代码。

My problem here is, when the form is submitted, the data isn't actually being entered into the table. 我的问题是,提交表单时,实际上没有将数据输入到表中。 Note that the first ever submission actually did work. 请注意,第一次提交实际上确实有效。 All submissions after the first one don't seem to be entering anything into the database. 第一个提交之后的所有提交似乎都没有在数据库中输入任何内容。

I'm not quite sure what's wrong with my code for it to not work. 我不太确定我的代码有什么不起作用的地方。 It does go to the 'login.php' page which means there aren't any faults and the query submitted correctly. 确实会进入“ login.php”页面,这意味着没有任何错误并且查询已正确提交。

Could someone please tell me what I'm doing wrong, thank you. 有人可以告诉我我在做什么错,谢谢。

right now you have alot more trouble than your insert problem. 现在,您遇到的麻烦比插入问题还多。 your code is totaly insecure. 您的代码完全不安全。 (mysql injections). (mysql注入)。

Dont use mysql_* functions use pdo instead with prepared statements! 不要使用mysql_ *函数,而要使用带预备语句的pdo!

you output spaces before you send a header you cant send header if you have output. 您在发送标题之前先输出空格,如果有输出,则无法发送标题。 your redirect wouldnt work. 您的重定向将无法正常工作。 dont relay on a clientside redirect use may have it disabled so output a link where you want the user to go. 不要在客户端重定向上中继使用,可能已将其禁用,因此在您希望用户访问的地方输出链接。

anotherthing your radio buttons have no value check html syntax var_dump($_POST) and check if you submit everything. 另外,您的单选按钮没有任何价值,请检查html语法var_dump($ _ POST)并检查是否提交了所有内容。 also check for isset or empty befor assign variables. 还要在分配变量前检查isset或为空。 do some sort of validation. 做某种验证。

have a look at some php frameworks they provide much more flexibility and error checking 看看一些php框架,它们提供了更多的灵活性和错误检查

dont reinvent the wheel by writing everthing by your own in a 10 or more year behind procedural way 不要在程序落后10年或更长时间的情况下自行编写所有内容来重塑方向盘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM