Jump to content

Login/Register SQL Code works in phpMyAdmin but Has Errors


wildbuddha

Recommended Posts

Hello all.  This is my first post in a php forum.  Thanks, in advance, for any help. This code:

<?php
// database table 'users' is setup with the following fields: 
//     first_name, last_name, user_name, password  

$db_hostname = "localhost"; 
$db_username = "root";
$db_password = "";
$db_name =     "justinalba_module3_db";

//connection to the database
$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
     or die("Unable to connect to MySQL");

$sql = "INSERT INTO user (name_first, name_last, name_username, password) VALUES ('Sam', 'Row', 'samrow', 'pw123')";
mysqli_query ($dbc, $sql) or die("Problem executing query");
echo "Rows inserted: ", mysqli_affected_rows($dbc), "<br><br>";

$rowcount = 0;

$q = mysqli_query($dbc, "select * from user");
while ($dbc = mysqli_fetch_row($q)) {
    $rowcount++;

    for ($k=0; $k<count($dbc); $k++){
        echo " $dbc[$k] ";
    }
    echo "<br>";
}

echo "<p> A total of  $rowcount rows<br>";


?>

 

works perfectly...then I add it to a function:

function register(){
if(isset($_POST['submitRegistration']))
	$username=$_POST['username'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$password=$_POST['password2'];
	{
	if($_POST['username']=="")
		{
			echo "Please type username";
		}
	elseif (!preg_match("/^[a-zA-Z1-9]+$/", $username)) //checks for illegal characters, succcess!
		{
  			echo 'Use alphanumeric characters only.';
		}
	elseif (strlen($username) < 6 || strlen($username) > 15) // checks length of username
		{
			echo "Username must be 6 to 15 characters";
		}
	elseif($_POST['password1']==""|$_POST['password2']=="")
		{
			echo "Please type password";
		}
	elseif($_POST['password1']!=$_POST['password2'])
		{
			echo "Uh-oh.  Your passwords don't match.";
		}
	else
		{
			$username=$_POST['username'];
			$firstName = $_POST['firstName'];
			$lastName = $_POST['lastName'];
			$password=$_POST['password2'];
			$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
			     or die("Unable to connect to MySQL");
			$sql = "INSERT INTO user (id, name_first, name_last, name_username, password) VALUES 
				('', '$firstName', '$lastName', '$username', '$password')";
			mysqli_query ($dbc, $sql) or die("Problem executing query");
			echo "Rows inserted: ", mysqli_affected_rows($dbc), "<br><br>";

$rowcount = 0;

$q = mysqli_query($dbc, "select * from user");
while ($dbc = mysqli_fetch_row($q)) {
    $rowcount++;

    for ($k=0; $k<count($dbc); $k++){
        echo " $dbc[$k] ";
    }
    echo "<br>";
}

echo "<p> A total of  $rowcount rows<br>";
		}
	}
}

 

and I get the "die("Unable to connect to MySQL")" error.

 

Any thoughts?  I know I restated several variables inside the whatif statement.  I was just trying to see what the problem was.

 

Thanks,

JA.

module3_2.php

registration.php

localhost.sql.zip

You need to change your variable names.

 

In the first snippet of code, they're called db_hostname, db_username etc, and then you've straight copied that into your second snippet, but not updated the names of the variables (they're called just username, firstName etc).

 

So either change the name of the variables when you instantiate them (when you says $username = $_POST['username']), or change the variable that you pass to the mysqli_connect function.

 

Hope that helps.

 

Denno

Hi, thanks so much for the reply!  Correct me if I'm wrong, and I probably am, but aren't the variables you speak of for the database and not the values of the form?  I actually was able to solve the problem by moving:

            $db_hostname = "localhost";
            $db_username = "root";
            $db_password = "";
            $db_name = "justinalba_module3_db";

inside the "else" statement that contains the MySql.  Regardless, I would never have tried that if you didn't respond :) So thank you.

function register(){
if(isset($_POST['submitRegistration']))
	$username=$_POST['username'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$password=$_POST['password2'];
	{
	if($_POST['username']=="")
		{
			echo "Please type username";
		}
	elseif (!preg_match("/^[a-zA-Z1-9]+$/", $username)) //checks for illegal characters, succcess!
		{
  			echo 'Use alphanumeric characters only.';
		}
	elseif (strlen($username) < 6 || strlen($username) > 15) // checks length of username
		{
			echo "Username must be 6 to 15 characters";
		}
	elseif($_POST['password1']==""|$_POST['password2']=="")
		{
			echo "Please type password";
		}
	elseif($_POST['password1']!=$_POST['password2'])
		{
			echo "Uh-oh.  Your passwords don't match.";
		}
	else
		{	
			$db_hostname = "localhost"; 
			$db_username = "root";
			$db_password = "";
			$db_name =     "justinalba_module3_db";
			$username=$_POST['username'];
			$firstName = $_POST['firstName'];
			$lastName = $_POST['lastName'];
			$password=$_POST['password2'];
			$dbc = @mysqli_connect($db_hostname, $db_username, $db_password, $db_name)
			     or die("Unable to connect to MySQL");
			$sql = "INSERT INTO user (id, name_first, name_last, name_username, password) VALUES 
				('', '$firstName', '$lastName', '$username', '$password')";
			mysqli_query ($dbc, $sql) or die("Problem executing query");
			echo "Rows inserted: ", mysqli_affected_rows($dbc), "<br><br>";

$rowcount = 0;

$q = mysqli_query($dbc, "select * from user");
while ($dbc = mysqli_fetch_row($q)) {
    $rowcount++;

    for ($k=0; $k<count($dbc); $k++){
        echo " $dbc[$k] ";
    }
    echo "<br>";
}

echo "<p> A total of  $rowcount rows<br>";
		}
	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.