Can't Insert Data into Database


#1

Hello! I am currently making an app and I ran into an error when writing PHP to insert data into an SQL database. StackOverflow wasn't every helpful so I came here.

When I ran my script, I got these errors:

Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /Users/DannyTran/Desktop/WebServer/app/php/database/connect.php on line 4

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /users/DannyTran/Desktop/WebServer/app/register.php on line 81

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /users/DannyTran/Desktop/WebServer/app/register.php on line 96

Warning: mysql_query() expects parameter 1 to be string, resource given in /users/DannyTran/Desktop/WebServer/app/register.php on line 127
No database selected

Here is my database connection script:

<?php


	$conn = mysql_connect("localhost", "username", "password", "root");

	if (!$conn) {
		die("Connection failed: ".mysql_connect_error());
	}


?>

Here is my registration script:

<?php require_once 'elements/header.php'; ?>
<?php

	ob_start();
	session_start();
	if( isset($_SESSION['user'])!="" ){
		header("Location: home.php");
	}
	include 'php/database/connect.php';

	$error = false;

	if (isset($_POST['register'])) {
  
	// Prevent SQL Injection
	$first_name = trim($_POST['first_name']);
	$first_name = strip_tags($first_name);
	$first_name = htmlspecialchars($first_name);
  
	$last_name = trim($_POST['last_name']);
  	$last_name = strip_tags($last_name);
  	$last_name = htmlspecialchars($last_name);
  
  	$username = trim($_POST['username']);
  	$username = strip_tags($username);
  	$username = htmlspecialchars($username);

  	$email = trim($_POST['email']);
  	$email = strip_tags($email);
  	$email = htmlspecialchars($email);

  	$area = $_POST['area'];
  	$phone = $_POST['area'].$_POST['phone'];
  	$phone = strip_tags($phone);
  	$phone = htmlspecialchars($phone);

  	$password = trim($_POST['password']);
  	$password = strip_tags($password);
  	$password = htmlspecialchars($password);
  
  	// Other database fields
  	$date_of_birth = $_POST['year']."-".$_POST['month']."-".$_POST['day'];
	$birthday = $_POST['month']."/".$_POST['day'];
	$country = $_POST['country'];
	$gender = $_POST['gender'];
	$year = date('Y');
	date_default_timezone_set('America/New_York');
	$date = date('m/d/Y h:i:s a', time());
	// Get Age
	$now = time();
	$dob = strtotime($date_of_birth);
	$difference = $now - $dob;
	$age = floor($difference / 31556926);

  	// Check first name
  	if (empty($first_name)) {
   		$error = true;
   		$firstNameError = "Please provide a first name!";
  	} else if (strlen($first_name) < 3) {
   		$error = true;
   		$firstNameError = "First name field must contain at least 3 characters!";
  	}

  	// Check last name
  	if (empty($last_name)) {
   		$error = true;
   		$lastNameError = "Please fill in this field!";
  	} else if (strlen($last_name) < 3) {
   		$error = true;
   		$lastNameError = "Field must contain at least 3 characters!";
  	}

  	// Check username
  	if (empty($username)) {
   		$error = true;
   		$usernameError = "Please fill in this field!";
   	} else {
 	 	// Does email address exist?
   		$query = "SELECT username FROM users WHERE username='$username'";
   		$result = mysql_query($query);
   		$count = mysql_num_rows($result);
   		if($count!=0){
    		$error = true;
   			$usernameError = "Username is taken!";
   		}
   	}
  
  	// Check email
 	if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
 	 	$error = true;
 		$emailError = "Please enter valid email address.";
 	} else {
 	 	// Does email address exist?
   		$query = "SELECT email FROM users WHERE email='$email'";
   		$result = mysql_query($query);
   		$count = mysql_num_rows($result);
   		if($count!=0){
    		$error = true;
   			$emailError = "Email is already in use!";
   		}
	}

	// password validation
	if (empty($password)){
		$error = true;
	   	$passError = "Please fill in this field!";
	} else if(strlen($password) < 6) {
	   	$error = true;
	   	$passError = "Field must contain at least 6 characters!";
	}
	  
	// Check age
	if ($age < 13) {
		$error = true;
		$ageError = "You must be 13 years or older to join! Stay tuned for HoogleyBoogley Kids!";
	}



	// password encrypt using SHA256();
	$password = hash('sha256', $password);
  
	// if there's no error, continue to signup
	if( !$error ) {
 
   	$query = "INSERT INTO users (username, first_name, last_name, email, phone, password, gender, country, picture, birthday, website, bio, status, joined, last_login, role, moderator, admin) VALUES ('$username','$first_name', '$last_name', '$email', '$phone', '$password', '$gender', '$country', 'defualt.png', '$date_of_birth', '', '', '', '$year', '$date', 'Member', '0', '0')";
	$res = mysql_query($conn, $query) or die(mysql_error());
    
	if ($res) {
    	$errTyp = "success";
    	$errMSG = "Successfully registered, you may login now";
    	unset($name);
    	unset($email);
    	unset($pass);
   	} else {
    	$errTyp = "danger";
    	$errMSG = "Something went wrong, try again later..."; 
   	} 
    
  	}
}
?>

Here is my Database:

CREATE TABLE `users` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL DEFAULT '',
  `first_name` varchar(255) NOT NULL DEFAULT '',
  `last_name` varchar(255) NOT NULL DEFAULT '',
  `email` varchar(255) NOT NULL DEFAULT '',
  `phone` varchar(255) DEFAULT NULL,
  `password` varchar(255) NOT NULL DEFAULT '',
  `gender` varchar(255) NOT NULL DEFAULT '',
  `country` varchar(255) NOT NULL DEFAULT '',
  `picture` varchar(255) NOT NULL DEFAULT '',
  `birthday` date NOT NULL,
  `website` text,
  `bio` text,
  `status` varchar(255) DEFAULT NULL,
  `joined` varchar(255) NOT NULL DEFAULT '',
  `last_login` varchar(255) NOT NULL DEFAULT '',
  `role` varchar(255) NOT NULL DEFAULT '',
  `moderator` varchar(255) NOT NULL DEFAULT '',
  `admin` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Thank you for your help!


#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.