No data being populated in database..

Ask about a PHP problem here.
Post Reply
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

No data being populated in database..

Post 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!!
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: No data being populated in database..

Post 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.
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: No data being populated in database..

Post by EcazS »

Probably something wrong with your query.
brisk
Posts: 26
Joined: Mon Nov 21, 2011 1:22 am

Re: No data being populated in database..

Post 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]
Last edited by jacek on Mon Nov 21, 2011 6:02 pm, edited 2 times in total.
Reason: code tags...
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: No data being populated in database..

Post by Temor »

please, use the code tags.

[syntax=text][syntax=php]<?php code in here ?>[/syntax][/syntax]
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: No data being populated in database..

Post 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
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
Tino
Posts: 360
Joined: Thu May 05, 2011 8:55 pm
Location: The Netherlands

Re: No data being populated in database..

Post 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]
Please check out my CodeCanyon items.
Post Reply