4 Replies - 295 Views - Last Post: 03 October 2017 - 09:39 AM Rate Topic: -----

#1 amrin12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 17

Problem inserting data into mysql

Posted 03 October 2017 - 08:56 AM

Hi guys, im new to php and currently following a tutorial on youtube...i got some problems when i tried to insert data into my database into mysql..im using xampp as my local server. I copied the original code given in the desc of the video and nothing change

here's my code

<?php

$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "loginsystem";

$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);


my signup form that takes the data with error handler

<?php

if (isset($_POST['submit'])) {
	
	include_once 'dbh.inc.php';

	$first = mysqli_real_escape_string($conn, $_POST['first']);
	$last = mysqli_real_escape_string($conn, $_POST['last']);
	$email = mysqli_real_escape_string($conn, $_POST['email']);
	$uid = mysqli_real_escape_string($conn, $_POST['uid']);
	$pwd = mysqli_real_escape_string($conn, $_POST['pwd']);

	//Error handlers
	//Check for empty fields
	if (empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)) {
		header("Location: ../signup.php?signup=empty");
		exit();
	} else {
		//Check if input characters are valid
		if (!preg_match("/^[a-zA-Z]*$/", $first) || !preg_match("/^[a-zA-Z]*$/", $last)) {
			header("Location: ../signup.php?signup=invalid");
			exit();
		} else {
			//Check if email is valid
			if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
				header("Location: ../signup.php?signup=email");
				exit();
			} else {
				$sql = "SELECT * FROM users WHERE user_uid='$uid'";
				$result = mysqli_query($conn, $sql);
				$resultCheck = mysqli_num_rows($result);

				if ($resultCheck > 0) {
					header("Location: ../signup.php?signup=usertaken");
					exit();
				} else {
					//Hashing the password
					$hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
					//Insert the user into the database
					$sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) VALUES ('$first', '$last', '$email', '$uid', '$hashedPwd');";
					mysqli_query($conn, $sql);
					header("Location: ../signup.php?signup=success");
					exit();
				}
			}
		}
	}

} else {
	header("Location: ../signup.php");
	exit();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem inserting data into mysql

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,900
  • Joined: 12-June 08

Re: Problem inserting data into mysql

Posted 03 October 2017 - 09:02 AM

What's the problem?
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 13,474
  • Joined: 08-August 08

Re: Problem inserting data into mysql

Posted 03 October 2017 - 09:19 AM

The first problem I see is that YOU'RE NOT USING PREPARED STATEMENTS! I stop looking when I see that.
Was This Post Helpful? 0
  • +
  • -

#4 amrin12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 08-August 17

Re: Problem inserting data into mysql

Posted 03 October 2017 - 09:27 AM

View Postmodi123_1, on 04 October 2017 - 12:02 AM, said:

What's the problem?


The data wasn't saved into the database. It should saved the username, password, email,and names into database
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1830
  • View blog
  • Posts: 5,761
  • Joined: 15-January 14

Re: Problem inserting data into mysql

Posted 03 October 2017 - 09:39 AM

You need to be more specific, what exactly happens? There are a bunch of redirects to signup.php, so which one of those is it redirecting to? Each of them has a different value in the URL based on what the problem is, if any.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1