Jump to content

[SOLVED] Mysql wont insert into database


akiznin

Recommended Posts

I created a basic register script to see what I have learned so far but why wont it submit the username and password into the database? I get no errors in the script or the database, it just does not submit it.

 

<?php
include('../database.php');
$session = $_SESSION['username'];

if (isset($session)) {
header('Location: ../index.php');
}

$username = $_POST['username'];
$password = $_POST['password'];

if (!isset($username) OR !isset($password) OR !ctype_alnum($username) OR !ctype_alnum($password) OR strlen($username) > 20 OR strlen($username) < 3 OR strlen($password) > 20 OR strlen($password) > 3) {
echo "<form method=\"post\" action=\"index.php\">\n"; 
echo "Username: <input type=\"text\" size=\"10\" maxlength=\"20\" name=\"name\"> <br />\n"; 
echo "Password: <input type=\"password\" size=\"10\" maxlength=\"20\" name=\"password\"><br />\n"; 
echo "\n"; 
echo "<input type=\"submit\" value=\"Register\"> \n"; 
echo "</form>\n";
} else {
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);

$sql=mysql_query("SELECT * FROM `members` WHERE username='$username'");
$count=mysql_num_rows($sql);
if($count == 1) {
echo "Username has been taken.";
} else {

$password_md5 = md5($password);

$query = "INSERT INTO members (id, username, password) VALUES ('', '$username', '$password_md5')";
$results = mysql_query($query);

if ($results) {
echo "Thank you for registering, " . htmlentities($username) . "!";
}
mysql_close();
}
}
?>

Link to comment
Share on other sites

Hi

 

Take it you are just getting the form presented again.

 

Presume you really want to check the password is more than 3 long and less than 20 long

 

if (!isset($username) OR !isset($password) OR !ctype_alnum($username) OR !ctype_alnum($password) OR strlen($username) > 20 OR strlen($username) < 3 OR strlen($password) > 20 OR strlen($password) > 3)

 

All the best

 

Keith

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.