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
https://forums.phpfreaks.com/topic/163333-solved-mysql-wont-insert-into-database/
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

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.