I am a very new Beginner In PHP I am getting SQL Error and not Accessing into Database Can someone point me in the right direction where I am Going Wrong In
My Code. How Should I remove
non-object error
getting errors in these two lines
if( $result->num_rows !== 0 )
..........// }
$result->free();
thanks in advance.
<?php
if ( !empty( $_POST['submit'] ) ) {
if ( empty( $_POST['user_name'] ) || empty( $_POST['email'] ) || empty( $_POST['password'] ) || empty( $_POST['re_password'] ) ) {
exit( "please fill all the form field.<a href='./form.php'>return</a>" );
}
if ( $_POST['password'] !== $_POST['re_password'] ) {
exit( "please check your password.<a href='./form.php'>return</a>" );
}
$pattern = "/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/";
if ( !preg_match( $pattern, $_POST['email'] ) ) {
exit("please use a valid email address.<a href='./form.php'>return</a>");
}
$pattern = "/^.{6,20}$/";
if ( !preg_match( $pattern, $_POST['password'] ) ) {
exit( "password should contain atleast 8 characters and no more 20 characters.<a href='./form.php'>return</a>" );
}
$user_name = addslashes( $_POST['user_name'] );
$email = addslashes( $_POST['email'] );
$password = addslashes( $_POST['password'] );
require_once( "./connect.php" );
$sql = "SELECT * FROM 'user' WHERE 'email'='{$email}'";
$result = $db->query($sql);
if ( $db->connect_error ) {
exit( "SQL error.<a href='./form.php'>return</a>" );
}
if ( $result->num_rows !== 0 ) {
exit( "please use another email address.<a href='./form.php'>return</a>" );
}
$result->free();
$password = md5( $password );
$sql = "INSERT INTO 'user' SET 'user_name'='{$user_name}', 'email'='{$email}', 'password'='{$password}'";
$result = $db->query( $sql );
if ( $result === true ) {
echo "registration successful.<br/>";
} else {
echo "registration failed.<br/>";
}
$db->close();
}
?>
<form action="" method="POST">
Username: <input type="text" name="user_name" value=""/><br/>
Email-Address: <input type="text" name="email" value=""/><br/>
Password: <input type="password" name="password" value=""/><br/>
Re-Enter Password: <input type="password" name="re_password" value=""/>
<br/>
<input type="submit" name="submit" value="submit"/>
</form>
<!-- code of connect file to Database-->
<?php
$db = new mysqli( "localhost", "root", "", "registration" );
if ( $db->connect_error ) {
printf("Connect failed: %s\n", $db->connect_error);
exit();
}
?>