Redisplaying a form!

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Redisplaying a form!

Postby manddox » 22. May 2009 21:32

hi all,

I am trying to check whether a username is already present in my 'users' table and doing validation in both the JavaScript and in the form processing. but I m receiving an error when i run the code below:
Code: Select all
<html>
<head>
<title>Sample Form</title>
<script type="text/javascript" src="source.js"></script>
<script type="text/javascript">
function check_valid(form)
{
   var error = "";
   error += verify_username(form.username.value);
   error += verify_password(form.password.value);
   error += verify_phone(form.phone.value);
   error += verify_email(form.email.value);
   if (error != "")
   {
      alert(error);
      return false;
   }
   return true;
}
</script>
</head>
<body>
<?php
// Check for form post submit
if ($_POST["submit"])
{
   require_once('db_login.php');
   require_once('DB.php');
   $connection = DB::connect("mysql://$db_username:$db_password@$db_host/$db_database");
   if (DB::isError($connection))
   {
      die ("Could not connect to the database: <br />". DB::errorMessage($connection));
   }
   // Remember to use htmlentities to prevent cross-site scripting vulnerabilities
   $username = $_POST["username"];
   $username = mysql_real_escape_string(get_magic_quotes_gpc( ) ? stripslashes($username) :
   $username);
   $password = $_POST["password"];
   $password = htmlentities(get_magic_quotes_gpc( ) ? stripslashes($password) : $password);
   $email = $_POST["email"];
   $email = htmlentities(get_magic_quotes_gpc( ) ? stripslashes($password) : $password);
   $phone = $_POST["phone"];
   $phone = htmlentities(get_magic_quotes_gpc( ) ? stripslashes($phone) : $phone);
   $error = "";
}
if (is_null($username == ""))
{
   $error .= "Username must not be null.<br />";
}
if ($password == "")
{
   $error .= "Password must not be null.<br />";
}
if ($email == "")
{
   $error .= "Email must not be null.<br />";
}
if ($phone == "")
{
   $error .= "Phone must not be null.<br />";
}
// Query the posts with categories and user information
$query = "SELECT * FROM users WHERE username='$username'";
// Execute the database query
$result = $connection->query($query);
if (DB::isError($result))
{
   die("Could not query the database: <br />".$query." ".DB::errorMessage($result));
}
$user_count = $result->numRows( );
if ($user_count > 0)
{
   $error .= "Error: Username $username is taken already. Please select another. <br />";
}
if ($error)
{
   echo $error;
}
else
{
   echo "Username is available.";
   exit;
}
?>
// This script will process the results as well as display the form
<form action="<?php echo htmlentities($_SERVER["PHP_SELF"]); ?>" method="POST"
onsubmit="return check_valid(this);" id="test1" name="test1">
<table>
<tr>
   <td width="30%" align="right">Username:</td>
   <td><input type="text" name="username" value="<?php echo ($username); ?>" /> </td>
</tr>
<tr>
   <td align="right">Password:</td>
   <td><input type="password" name="password" value="<?php echo($password); ?>" /> </td>
</tr>
<tr>
   <td align="right">Phone:</td>
   <td><input type="phone" name="phone" value="<?php echo($phone); ?>" /></td>
</tr>
<tr>
   <td align="right">Email:</td>
   <td><input type="email" name="email" value="<?php echo($email); ?>" /></td>
</tr>
<tr>
   <td>&nbsp;</td>
   <td><input type="submit" name="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>


The error I receive is:

Fatal error: Call to a member function query() on a non-object in RedisplayForm.php on line 65.

Line 65 in the above code is : $result = $connection->query($query);

Please suggest some solution or direct me to the right path where i can find the solution! please note am a beginner in php.....so plz suggest simple solutions! thanx!
manddox
 
Posts: 10
Joined: 03. May 2009 14:22

Re: Redisplaying a form!

Postby manddox » 23. May 2009 14:41

well, i found out the error. The connection to the database is terminated before I made the query and the reason for this was the curly bracket, which when removed solved the issue.
manddox
 
Posts: 10
Joined: 03. May 2009 14:22


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 61 guests