简体   繁体   English

PHP SQL注册表格

[英]PHP SQL registration form

I am attempting a registration form that saves the data into a sql db. 我正在尝试将数据保存到sql db中的注册表。 I'm not doing form validation just yet as what is most troubling me is getting this stuff onto sql. 我现在还没有进行表单验证,因为最困扰我的是将这些东西放到sql上。 I'd appreciate any advice!! 我将不胜感激任何建议!

I have a form.php file that should be doing the hard work. 我有一个form.php文件,应该进行艰苦的工作。 When I submit my form, at this point, I get a blank screen and nothing loads into the database. 提交表单时,此时,我得到一个空白屏幕,并且没有任何内容加载到数据库中。

<?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";


?>

And I have an html file that contains this: 我有一个包含以下内容的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>

Finally, I have a sql database (I'm running xampp locally) called "registration" The table I've created is called "userTable" and it contains 8 fields including ID (auto incrementing) and the 7 other values I've included up top. 最后,我有一个名为“ registration”的sql数据库(我正在本地运行xampp),我创建的表称为“ userTable”,它包含8个字段,包括ID(自动递增)和我包含的其他7个值顶上。 Any idea what the heck I'm doing wrong? 知道我做错了什么吗?

What is the problem? 问题是什么?

1) The problem is that it does INSERT query each time you load this page - mean each time it inserts empty values. 1)问题是,每次您加载该页面时,它都会进行INSERT查询-意味着每次它插入空值。 Why? 为什么?

Simply because there's no condition that checks if all fields has been posted, so instead of: 仅仅因为没有条件检查是否所有字段都已过帐,所以代替:

<?php

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

You should check if $_POST super-global has some keys. 您应该检查$_POST super-global是否具有某些密钥。 So before doing any queries - first of all check if $_POST isn't empty 因此,在执行任何查询之前-首先检查$_POST是否为空

<?php

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

  //all your stuff goes here
}

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

2) Are you sure you are in control of your code? 2)确定要控制代码吗? Apparently not. 显然不是。

You MUST check if some function returned TRUE and then make following actions relying on it's one. 您必须检查某个函数是否返回TRUE ,然后根据该函数执行以下操作。

For example, are you sure that mysql_query("your sql query") was succeed at? 例如,您确定mysql_query("your sql query")成功了吗?

3) Enable error_reporting to E_ALL, so just put error_reporting(E_ALL) at the top of your page, like this: 3)启用error_reporting到E_ALL,因此只需将error_reporting(E_ALL)放在页面顶部,如下所示:

<?php

error_reporting(E_ALL);

So that you can always debug your script "on fly" 这样您就可以随时“动态”调试脚本

4) You are doing everything to make this code hard to maintain, Why? 4)您正在尽一切努力使此代码难以维护,为什么? Look at this: 看这个:

<?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