PHP注册错误

时间:2013-12-17 05:14:34

标签: php mysql login

我正在尝试为我的网站制作一个注册脚本。

表单有效,但唯一的问题是当我提交时,它会给出这些错误:

    Notice: Undefined index: username in /home/toonbox/public_html/Rework/register.php on line 22

    Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in /home/toonbox/public_html/Rework/register.php on line 28

    Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in /home/toonbox/public_html/Rework/register.php on line 29

    Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in /home/toonbox/public_html/Rework/register.php on line 30

    Warning: mysql_real_escape_string() expects parameter 1 to be string, resource given in /home/toonbox/public_html/Rework/register.php on line 31
    Error: Please fill in the form!
    Notice: Undefined variable: row in /home/toonbox/public_html/Rework/register.php on line 43

    Warning: mysql_query() expects parameter 1 to be string, resource given in /home/toonbox/public_html/Rework/register.php on line 47
    Unable to access the table!

这是代码:           

        //temp
        //used to check the errors in the script
        ini_set('display_errors', 1);
        error_reporting(E_ALL);

        session_start(); //starts php session

        require "register.inc.php"; //include the config file

        if(isset($_SESSION['username'])){
           header("location: play.php");
        }

        if(isset($_POST['submit']))
        {

        //variables
        $userPOST = $_POST['username'];
        $passPOST = $_POST['pass'];
        //$passchk = $_POST['pass']; //to do soon
        $emailPOST = $_POST['email'];
        $keyPOST = $_POST['key'];

        $user = mysql_real_escape_string($dbcon, $userPOST);
        $pass = mysql_real_escape_string($dbcon, $passPOST);
        $email = mysql_real_escape_string($dbcon, $emailPOST);
        $key = mysql_real_escape_string($dbcon, $keyPOST);

        if($user == "" || $pass = "" || $email = "" || $key = "")
        {
          echo "Error: Please fill in the form!";
        }

        else
        {
           $query = mysql_query($dbcon,"SELECT * FROM users WHERE username = '$user'") or die ("Unable to access the table!");
            $row = mysql_num_rows($query);
            }
            if($row == 1)
              echo "User already exist. Please choose a different username!";
            {
            //verify key is valid
            $querykey = mysql_query($dbcon,"SELECT * FROM users WHERE keys = '$key'") or die ("Unable to access the table!");
            $rowkey = mysql_num_rows($querykey);
            if(!$rowkey == 1)
              echo "Key isn't valid. Try again!";
            }
            if($rowkey == 1)
            {
            $reg = mysql_query($dbcon,"INSERT INTO users (id, username, password, email, key) VALUES (null, '$user', '$pass', '$email', '$key')")  or die ("Cannot register. An error occured!");
            echo "Sucessfully registered!";
           }
        }
        ?>   

我尝试修复用户名部分 但它没有做任何结果

我现在真的很绝望......     出现这种情况的原因是什么?

编辑: 它修复了错误,但现在它给出了这个:

Notice: Undefined index: username in /home/toonbox/public_html/Rework/register.php on line 22
Error: Please fill in the form!
Notice: Undefined variable: row in /home/toonbox/public_html/Rework/register.php on line 43

Warning: mysql_query() expects parameter 1 to be string, resource given in /home/toonbox/public_html/Rework/register.php on line 47
Unable to access the table!

编辑2: 这是:

<html>
<div align="center">
<!-- Yes joe, ill add style later. Lemme just test this !-->
<form name="register" method="post" action="register.php">
<input name="user" type="text" id="username" value="Your Username"><br><br>
<input name="pass" type="password" id="pass" value="Your Password"><br><br>
<input name="email" type="text" id="email"  value="Your E-mail"><br><br>
<input name="key" type="text" id="key"  value="An Valid Alpha Key"><br><br>
<input type="submit" name="submit" value="Register">
</div>
</form>
</html>

编辑3: 我修复了dbcon,由于某种原因,它现在给了我这个错误:

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in /home/toonbox/public_html/Rework/register.php on line 28

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in /home/toonbox/public_html/Rework/register.php on line 29

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in /home/toonbox/public_html/Rework/register.php on line 30

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, string given in /home/toonbox/public_html/Rework/register.php on line 31
Error: Please fill in the form!
Notice: Undefined variable: row in /home/toonbox/public_html/Rework/register.php on line 44

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/toonbox/public_html/Rework/register.php on line 48
Unable to access the table!

现在是实际的代码:

<?php
//REGISTER.PHP
//By Sheriff Reggie.

//temp
//used to check the errors in the script
ini_set('display_errors', 1);
error_reporting(E_ALL);

session_start(); //starts php session

require "register.inc.php"; //include the config file

if(isset($_SESSION['username'])){
   header("location: play.php");
}

if(isset($_POST['submit']))
{

//variables
$userPOST = isset($_POST['username']) ? $_POST['username'] : '';
$passPOST = $_POST['pass'];
//$passchk = $_POST['pass']; //to do soon
$emailPOST = $_POST['email'];
$keyPOST = $_POST['key'];

$user = mysqli_real_escape_string($userPOST, $dbcon);
$pass = mysqli_real_escape_string($passPOST, $dbcon);
$email = mysqli_real_escape_string($emailPOST, $dbcon);
$key = mysqli_real_escape_string($keyPOST, $dbcon);

if($user == "" || $pass = "" || $email = "" || $key = "")
{
  echo "Error: Please fill in the form!";
}

else
{
     $row = mysqli_num_rows($query);
     $query = mysql_query("SELECT * FROM users WHERE username = '$user'",$dbcon) or die ("Unable to access the table!");

    }
    if($row == 1)
      echo "User already exist. Please choose a different username!";
    {
    //verify key is valid
    $querykey = mysqli_query("SELECT * FROM users WHERE keys = '$key'", $dbcon) or die ("Unable to access the table!");
    $rowkey = mysqli_num_rows($querykey);
    if(!$rowkey == 1)
      echo "Key isn't valid. Try again!";
    }
    if($rowkey == 1)
    {
     $reg = mysql_query("INSERT INTO users (id, username, password, email, key) VALUES (null, '$user', '$pass', '$email', '$key'),$dbcon")  or die ("Cannot register. An error occured!");
     echo "Sucessfully registered!";
   }
}
?>
<html>
<div align="center">
<!-- Yes joe, ill add style later. Lemme just test this !-->
<form name="register" method="post" action="register.php">
<input name="user" type="text" id="username" value="Your Username"><br><br>
<input name="pass" type="password" id="pass" value="Your Password"><br><br>
<input name="email" type="text" id="email"  value="Your E-mail"><br><br>
<input name="key" type="text" id="key"  value="An Valid Alpha Key"><br><br>
<input type="submit" name="submit" value="Register">
</div>
</form>
</html>

2 个答案:

答案 0 :(得分:1)

对于Notice: Undefined index: username,在html中,您使用user并使用username。因此$_POST['username']应为$_POST['user']

 $userPOST = isset($_POST['user']) ? $_POST['user'] : '';

Warning: mysql_real_escape_string() expects parameter 1 to be string。您交换了函数参数mysql_real_escape_string()

$user = mysql_real_escape_string($userPOST, $dbcon);
$pass = mysql_real_escape_string($passPOST, $dbcon);
$email = mysql_real_escape_string($emailPOST, $dbcon);
$key = mysql_real_escape_string(keyPOST, $dbcon$);

Warning: mysql_query() expects parameter 1 to be string。再次交换mysql_query

的参数

答案 1 :(得分:1)

更改代码$userPOST = $_POST['user'];

而不是$userPOST = $_POST['username'];