簡體   English   中英

PHP SQL注冊表格

[英]PHP SQL registration form

我正在嘗試將數據保存到sql db中的注冊表。 我現在還沒有進行表單驗證,因為最困擾我的是將這些東西放到sql上。 我將不勝感激任何建議!

我有一個form.php文件,應該進行艱苦的工作。 提交表單時,此時,我得到一個空白屏幕,並且沒有任何內容加載到數據庫中。

<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$password = $_POST['password'];
$email = $_POST['email'];
$cell = $_POST['cell'];
$experience = $_POST['experience'];
$ip = $_POST['ip'];

$password = md5($_POST['password']);

$connection = msql_connect(localhost, USERNAME, PASSWORD);
$db = mysql_select_db(registration,$connection);    

mysql_query("INSERT INTO userTable (fname,lname,password,email,cell,experience,ip) VALUES ('$fname', '$lname', '$password', '$email', '$cell', '$experience', '$ip')")
    or die (mysql_error());

echo "Thank you for your registration";


?>

我有一個包含以下內容的html文件:

            <form method = "post" action = "form.php">
            <h2>User Information</h2>

            <div><label>First Name:</label>
                <input type = "text" name = "fname"></div>
            <div><label>Last Name:</label>
                <input type = "text" name = "lname"></div>
            <div><label>Password:</label>
                <input type = "password" name = "password"></div>
            <div><label>Email:</label>
                <input type="text" name="email"></div>
            <div><label>Cellphone:</label>
                <input type="text" name="cell"></div>
                <input type="hidden" name="ip" value='<?php echo $IP ?>'/>
            <h2>What Is Your Experience Mountain Biking?</h2>
            <p><input type="radio" name="experience" value="n00b"
                checked>n00b
                <input type="radio" name="experience" value="intermediate">Intermediate
                <input type="radio" name="experience" value="extreme">Extreme                   
                </p>
            <p><input type="submit" name="submit" value="Register"></p>
        </form>

最后,我有一個名為“ registration”的sql數據庫(我正在本地運行xampp),我創建的表稱為“ userTable”,它包含8個字段,包括ID(自動遞增)和我包含的其他7個值頂上。 知道我做錯了什么嗎?

問題是什么?

1)問題是,每次您加載該頁面時,它都會進行INSERT查詢-意味着每次它插入空值。 為什么?

僅僅因為沒有條件檢查是否所有字段都已過帳,所以代替:

<?php

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$password = $_POST['password'];
$email = $_POST['email'];
$cell = $_POST['cell'];
$experience = $_POST['experience'];
$ip = $_POST['ip'];

您應該檢查$_POST super-global是否具有某些密鑰。 因此,在執行任何查詢之前-首先檢查$_POST是否為空

<?php

//This means that user did submit the form
if ( !empty($_POST) ){

  //all your stuff goes here
}

?>
<html>
.....
</html>

2)確定要控制代碼嗎? 顯然不是。

您必須檢查某個函數是否返回TRUE ,然后根據該函數執行以下操作。

例如,您確定mysql_query("your sql query")成功了嗎?

3)啟用error_reporting到E_ALL,因此只需將error_reporting(E_ALL)放在頁面頂部,如下所示:

<?php

error_reporting(E_ALL);

這樣您就可以隨時“動態”調試腳本

4)您正在盡一切努力使此代碼難以維護,為什么? 看這個:

<?php

//Debug mode:
error_reporting(E_ALL);

//Sure you want to show some error if smth went wrong:
$errors = array(); 

/**
 * 
 * @return TRUE if connection established 
 * FALSE on error
 */
function connect(){

 $connection = mysql_connect(localhost, USERNAME, PASSWORD);
 $db = mysql_select_db(registration,$connection);    

 if (!$connection || !$db ){
   return false; 
 } else {
   return true;
 }
}


//So this code will run if user did submit the form:
if (!empty($_POST)){

 //Connect sql server:
 if ( !connect() ){
   $errors[] = "Can't establish link to MySQL server";
 }

 $fname = $_POST['fname'];
 $lname = $_POST['lname'];
 $password = $_POST['password'];
 $email = $_POST['email'];
 $cell = $_POST['cell'];
 $experience = $_POST['experience'];
 //Why post ip? not smth like $_SERVER['REMOTE_ADDR']...
 $ip = $_POST['ip'];

 $password = md5($_POST['password']);

 //No error at this point - means that it successfully connected to SQL server: 
 if ( empty($errors) ){

  //let's prevent sql injection:

  $fname = mysql_real_escape_string($fname);
  //Please do this for all of them..
 }



//Now we should try to INSERT the vals:

$query = "INSERT INTO `userTable` (`fname`,`lname`,`password`,`email`,`cell`,`experience`,`ip`) VALUES ('$fname', '$lname', '$password', '$email', '$cell', '$experience', '$ip')";

//So try it:
if ( !mysql_query($query) ){
   // 
   //die (mysql_error());
   $errors[] = "Can't insert the vals";
} else {
   //Or on success:
   print ("Thank you for your registration");
   //or you can do redirect to some page, like this:

  //header('location: /thanks.php');
}


}

?>

<form method="post">
            <h2>User Information</h2>

            <div><label>First Name:</label>
                <input type = "text" name = "fname"></div>
            <div><label>Last Name:</label>
                <input type = "text" name = "lname"></div>
            <div><label>Password:</label>
                <input type = "password" name = "password"></div>
            <div><label>Email:</label>
                <input type="text" name="email"></div>
            <div><label>Cellphone:</label>
                <input type="text" name="cell"></div>
                <input type="hidden" name="ip" value='<?php echo $IP ?>'/>
            <h2>What Is Your Experience Mountain Biking?</h2>
            <p><input type="radio" name="experience" value="n00b"
                checked>n00b
                <input type="radio" name="experience" value="intermediate">Intermediate
                <input type="radio" name="experience" value="extreme">Extreme                   
                </p>

            <?php if ( !empty($errors) ) : ?>

            <?php foreach($errors as $error): ?> 
             <p><b><?php echo $error; ?></b></p>
            <?php endforeach; ?> 
             <?php endif; ?>


            <p><input type="submit" name="submit" value="Register"></p>
        </form>
Solved my own problem


<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$password = $_POST['password'];
$email = $_POST['email'];
$cell = $_POST['cell'];
$experience = $_POST['experience'];
$ip = $_POST['ip'];

$fname = mysql_real_escape_string($fname);
$lname = mysql_real_escape_string($lname);
$email = mysql_real_escape_string($email);
$password = md5($_POST['password']);

$servername="localhost";
$username="user";
$conn=  mysql_connect($servername,$username, password)or die(mysql_error());
mysql_select_db("registration",$conn);
$sql="insert into userTable (fname,lname,password,email,cell,experience,ip) VALUES ('$fname', '$lname', '$password', '$email', '$cell', '$experience', '$ip')";

$result=mysql_query($sql,$conn) or die(mysql_error());          
print "<h1>you have registered sucessfully</h1>";


echo "Thank you for your registration to the ";

mysql_close($connection);

?>

暫無
暫無

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

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