I am an independently attempting to learn php. I recently watched a tutorial on youtube on making basic User name and Password fields, once a user types a user name and password, the information is supposed to be saved in a database called Tutorial02. Everytime I submit a user name and pass, nothing is saved in the database.
I'm not sure if I have given enough information for anyone to help. If not, please inform on what information is needed and I will try to explain further what I know.
Thank you
Chris P
CODE
<?php
mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("tutorial02") or die(mysql_error());
?>
<html>
<form method="POST" action="index.php">
<table border="0" style="font-size: 15px; font-family: Tahoma; "border: solid black "">
<td>
Username:
<input type="text" name="username" value="<?php echo $_POST['username']; ?>"
</td>
<td>
Password:
<input type="password" name="password" value="<?php echo $_POST['password']; ?>"
<tr><td colspan="2" align="center">
<input type="submit" name="submit" value="Register"/>
</td>
</tr>
</table>
</form>
</html>
<?php
if($_POST['submit']){
$username = $_POST['username'];
$password = $_POST['password'];
$curnum = 0;
if(!$username) {
$curnum ++;
echo "<font color='red'>" . $curnum . ". You didn't enter a username!</font> <BR>";
}
if(!$password) {
$curnum ++;
echo "<font color='red'>" .$curnum . ". You didn't enter a password! </font><BR>";
}
}
//checks if username already exsists
$sql = "SELECT * FROM users WHERE username='".$username."'";
$res= mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($res) > 0) {
$curnum ++;
echo $curnum . ". The username ' ".$username."' already exists!<BR>\n";
if ($curnum == 0){
mysql_query("INSERT INTO users VALUES(`id`,'".$username."','".$password."')") or die(mysql_error());
}
}
?>