簡體   English   中英

使用PHP將數據從表單插入MySQL數據庫

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

我創建了一個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表

我的SQL表

上面的兩個代碼塊以及我正在使用的代碼。

我的問題是,提交表單時,實際上沒有將數據輸入到表中。 請注意,第一次提交實際上確實有效。 第一個提交之后的所有提交似乎都沒有在數據庫中輸入任何內容。

我不太確定我的代碼有什么不起作用的地方。 確實會進入“ login.php”頁面,這意味着沒有任何錯誤並且查詢已正確提交。

有人可以告訴我我在做什么錯,謝謝。

現在,您遇到的麻煩比插入問題還多。 您的代碼完全不安全。 (mysql注入)。

不要使用mysql_ *函數,而要使用帶預備語句的pdo!

您在發送標題之前先輸出空格,如果有輸出,則無法發送標題。 您的重定向將無法正常工作。 不要在客戶端重定向上中繼使用,可能已將其禁用,因此在您希望用戶訪問的地方輸出鏈接。

另外,您的單選按鈕沒有任何價值,請檢查html語法var_dump($ _ POST)並檢查是否提交了所有內容。 還要在分配變量前檢查isset或為空。 做某種驗證。

看看一些php框架,它們提供了更多的靈活性和錯誤檢查

不要在程序落后10年或更長時間的情況下自行編寫所有內容來重塑方向盤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM