Jump to content

PHP MySQL insert issue


wizzy886
Go to solution Solved by Barand,

Recommended Posts

Here is the code for both the connection file and the main body of script. I assure you the passwords and actual names do match as I have done other tests before hand. 

 

register.php

<?php 
	$title = "Register";
	include 'includes/header.php';  

    if($_SERVER['REQUEST_METHOD'] == 'POST') {
    	require 'includes/connection.php';

    		//create FALSE figures
    		$username = $password = FALSE;

    		//trim all data
    		$trimmed = array_map('trim', $_POST);

    		$errors = array();

    	//check username
	    if(preg_match ('/[A-Za-z0-9]{2,20}/', $trimmed['username'])) {
	    	$username = mysqli_real_escape_string($dbc, $trimmed['username']);
	    } else {
	    	$errors[] = 'Enter a username';
	    }

	    //check password
	    if(preg_match ('/[A-Za-z0-9]{4,20}/', $trimmed['password'])) {
	    	$username = mysqli_real_escape_string($dbc, $trimmed['password']);
	    } else {
	    	$errors[] = 'Enter a password';
	    }

	    //variables not == FALSE
	    if($username && $password) {

		    $q = "SELECT user_id FROM users WHERE username='$username'";
		    $r = mysqli_query ($dbc, $q) OR trigger_error("Query: $q\n<br />MYSQL Error: ". mysql_error($dbc));

		    if(mysqli_num_rows($r) == 0) {
		    	$q = "INSERT INTO users (username, password, registration_date) VALUES ('$username', SHA1('$password'), NOW() )";
		    	$r = mysqli_query ($dbc, $q) OR trigger_error("Query: $q\n<br />MYSQL Error: ". mysql_error($dbc));
		    }
	 
    		if(mysqli_affected_rows($dbc) == 1) {
    			echo "success";
    			//header('Location: database.php');
    		}

		} else {
			foreach ($errors as $msg) {
		    	echo " - $msg<br />\n";
			}	
		}
	}

?>

    <form action="register.php" method="POST">
        <input type="text" name="username" value="<?php if(isset($trimmed['username'])) echo $trimmed['username']; ?>" placeholder="Username"/>
        <input type="password" name="password" value="<?php if(isset($trimmed['username'])) echo $trimmed['password']; ?>" placeholder="Password"/>
        <input type="submit" class="button1" name="Sign Up" />
    </form>

connection.php

<?php 
    $dbhost = 'localhost';
    $dbuser = 'root';
    $dbpass = 'password';
    $db = 'ze';

    $dbc = mysqli_connect($dbhost, $dbuser, $dbpass) OR die ('Could not connect. MYSQL:' .mysql_error() );

        mysql_select_db($db);

Whenever I run this it does not seem to want to work, and also outputs no errors. I probably cant see something and need a second opinion. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.