Page 1 of 1

No data being populated in database..

Posted: Mon Nov 21, 2011 1:24 am
by brisk
I've copied the code exactly and adapted it to being on my machine...I am getting no errors etc but when I try register nothing get's entered into my database :-S

Are there any common issues or code pages you want to look at?

Thank you, love the tutorials!!

Re: No data being populated in database..

Posted: Mon Nov 21, 2011 9:52 am
by Temor
I'm not sure I understand what the problem is.
Is this your code not inserting info in the db or is your actual db malfunctioning?

If it's your code, post it here.

Re: No data being populated in database..

Posted: Mon Nov 21, 2011 11:00 am
by EcazS
Probably something wrong with your query.

Re: No data being populated in database..

Posted: Mon Nov 21, 2011 2:41 pm
by brisk
So yea I'll hit register and I'll get no errors or anything, just doesn't store the registered user in my database at all..

Here is my code...

REGISTER.PHP

[syntax=php]<?php

include ('core/init.inc.php');

$errors = array();

if (isset($_POST['username'], $_POST['password'], $_POST['repeat_password'])){
if (empty($_POST['username'])){
$errors[] = 'The username cannot be empty';
}

if (empty($_POST['password']) || empty($_POST['repeat_password'])){
$errors[] = 'The password cannot be empty.';
}

if ($_POST['password'] !== $_POST['repeat_password']){
$errors[] = 'Passwords not the same.';
}

if (user_exists($_POST['username'])){
$errors[] = 'The username already exists';
}

if (empty($errors)){
add_user($_POST['username'], $_POST['password']);

$_SESSION['username'] = htmlentities($_POST['username']);

header('Location: protected.php');
die();

}
}



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div>
<?php

if (empty($errors) === false){
?>
<ul>
<?php

foreach ($errors as $error){
echo "<li>{$error}</li>";
}

?>
</ul>

<?php

}


?>
</div>
<form action"" method="post">
<p>
<label for="username">Username:</label>
<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
</p>
<p>
<label for="password">Password:</label>
<input type="password" name="password" id="password" />
</p>
<p>
<label for="repeat_password">Repeat Password:</label>
<input type="password" name="repeat_password" id="repeat_password" />
</p>
<p>
<input type="submit" value="Register" />
</p>

</body>
</html>[/syntax]

INIT.INC.PHP

[syntax=php]<?php

session_start();

echo $_SERVER['SCRIPT_NAME'];


?>[/syntax]

USER.INC.PHP[/u]

[syntax=php]<?php


//checks if the given username exists in the database
function user_exists($user){
$user = mysql_real_escape_string($user);

$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}'");

return (mysql_result($total, 0) === '1') ? true : false;
}

// checks if the given username and password combination is valid

function valid_credentials($user, $pass){
$user = mysql_real_escape_string($user);
$pass = sha1($pass);

$total = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'user_name' = '{$user}' AND 'user_password' = '($pass)'");

return (mysql_result($total, 0) == '1') ? true : false;
}

//adds a user to the database.
function add_user($user, $pass){
$user = mysql_real_escape_string(htmlentities($user));
$pass = sha1($pass);

mysql_query("INSERT INTO 'users' ('user_name', 'user_password') VALUES ('{$user}','{$pass}')");
}


?>[/syntax]

Re: No data being populated in database..

Posted: Mon Nov 21, 2011 5:15 pm
by Temor
please, use the code tags.

[syntax=text][syntax=php]<?php code in here ?>[/syntax][/syntax]

Re: No data being populated in database..

Posted: Mon Nov 21, 2011 9:25 pm
by bowersbros
Im going to hazard a guess.. Have you connected to the database?

[syntax=php]<?php

mysql_connect(host, username, password);
mysql_select_db(database_name);

?>[/syntax]

if you have, then do:

[syntax=php]<?php

echo mysql_error();

?>[/syntax]

Anywhere below where you do your call in add_users() function

Re: No data being populated in database..

Posted: Wed Nov 23, 2011 8:47 pm
by Tino
Pretty common problem nowadays.

[syntax=php]mysql_query("INSERT INTO 'users' ('user_name', 'user_password') VALUES ('{$user}','{$pass}')");[/syntax]

You're using quotes around the table and field names whereas you should use backticks (or nothing, if you prefer).

[syntax=php]mysql_query("INSERT INTO `users` (`user_name`, `user_password`) VALUES ('{$user}','{$pass}')");[/syntax]