开发者

PHP Form Validator/MySQL Insert

开发者 https://www.devze.com 2023-01-17 15:15 出处:网络
I have written a small PHP file that gets the information that was posted to it, then checks to make sure it is no empty. If it isn\'t empty, it checks to make sure the username doesn\'t already exist

I have written a small PHP file that gets the information that was posted to it, then checks to make sure it is no empty. If it isn't empty, it checks to make sure the username doesn't already exist. If it does, it redirects. If not, it adds the information to the MySQL database. I don't know what the problem is, but when attempting to navigate to it after pressing the submit but开发者_如何转开发ton on the form, the browser displays an error saying that the page cannot be displayed. Here is the code.

<?php
$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$email = $_POST['email'];
$username = $_POST['user'];
$password = $_POST['pass'];


$con = mysql_connect("localhost","USER","PASS");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("zach_blogin", $con);

$query="SELECT username FROM members WHERE username=$username";



if (mysql_num_rows($username) > 0 ) {
  header("Location: register.php?invalid");
} else {
  $sql=("INSERT INTO members (username, password, FirstName, LastName, Email)
  VALUES ($username, $password, $firstname, $lastname, $email)");
  if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
else {
  header("Location: register.php?required");
}
?>


You are not executing the query:

$query="SELECT username FROM members WHERE username=$username";
//You need to execute the query here before you get num of rows.
if (mysql_num_rows($username) > 0 ) // also mysql_num_query takes an object.

Something like:

$query="SELECT username FROM members WHERE username=$username";
if(($result = mysql_query($query)) !== false) {
  if (mysql_num_rows($result) > 0 ) {
   .....
  }
}


Change this

if (mysql_num_rows($username) > 0 ) {

to

if (mysql_num_rows(mysql_query($query)) > 0 ) {
0

精彩评论

暂无评论...
验证码 换一张
取 消