Paul15679 Posted February 13, 2009 Share Posted February 13, 2009 I'm trying to create a script to validate a form using both Javascript and PHP, but when I run it I get this error: "Fatal error: Call to a member function query() on a non-object in C:\wamp\www\java_form_val2.php on line 57" Line 57 is where I run a database query: $result = $connection->query($query); And the rest of the script: <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 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)); } $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($email) : $email); $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 = "SELECT * FROM users WHERE username='$username'"; $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; } ?> <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="text" name="password" value="<?php echo ($password); ?>" /> </td> </tr> <tr> <td align="right">Phone:</td> <td><input type="text" name="phone" value="<?php echo ($phone); ?>" /> </td> </tr> <tr> <td align="right">Email:</td> <td><input type="text" name="email" value="<?php echo ($email); ?>" /> </td> </tr> <tr> <td> </TD> <td>:<input type="submit" name="submit" value="Submit" /></td> </tr> </table> </form> </body> </html> I ran the same script with just Javascript validation and it ran fine, it's as if its not recognising the PEAR connection query object, but I've run other scripts that use PEAR to run queries, and they work fine. I'd really appreciate it if anyone could point out where I'm going wrong. Quote Link to comment Share on other sites More sharing options...
drisate Posted February 13, 2009 Share Posted February 13, 2009 well did you declar query() somewhere? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.