Hi Guys,
Thank you for your help with my previous post. This problem is a carry on from that. My registration form now works perfectly and I will post the code below. Im now having trouble with my submit form.
The form Im using is very simple - message, screen name and password. First of all I would like to check to make sure all the necessary fields have been filled out.
I would then like to check the database agains the screen name for an existing record. If one doesn’t exist I would like to point them to the registration page, if it does I would like to check that the password entered is correct.
If the password is correct I would like the message “story” to be submitted to a separate table along with the screen name. This is what I have come up with…
Submit form
[php]<?php
//This makes sure they did not leave any fields blank
if (!$_POST[‘story’] | !$_POST[‘screen_name’] ) {
die('You did not complete all of the required fields');
}
// Connection to database
$con = mysql_connect(“database”,“user”,“pass”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}
mysql_select_db(“database”) or die(mysql_error());
$qry = “SELECT screen_name FROM login WHERE screen_name=’$screen_name’ AND pass=’”.addslashes($_POST[‘pass’])."’";
$result = mysql_query($qry);
if(mysql_num_rows($result) > 0) {
$sql=“INSERT INTO banter (story, screen_name)
VALUES
(’$_POST[story]’,’$_POST[screen_name]’)”;
if (!mysql_query($sql,$con))
{
die('Error: ’ . mysql_error());
}
echo “Thank You for posting”;
mysql_close($con);
}
?>[/php]
I know at the moment the code looks for the record where both the password and screen name match. Ideally I would like this to be separate so that the user can distinguish whether the username is incorrect or the password
Registration Form
[php]<?php
// Connection to database
$con = mysql_connect(“database”,“user”,“pass!”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}
mysql_select_db(“database”) or die(mysql_error());
//This makes sure they did not leave any fields blank
if (!$_POST[‘screen_name’] | !$_POST[‘pass’] | !$_POST[‘pass2’] ) {
die('You did not complete all of the required fields');
}
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['screen_name'] = addslashes($_POST['screen_name']);
}
$usercheck = $_POST[‘screen_name’];
$check = mysql_query(“SELECT screen_name FROM login WHERE screen_name = ‘$usercheck’”)
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the username '.$_POST['screen_name'].' is already in use.');
}
//this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Your passwords did not match. ');
}
//encrypt the password and add slashes if needed
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['screen_name'] = addslashes($_POST['screen_name']);
}
// now we insert it into the database
$sql=“INSERT INTO login (title, forename, surname, dob, email, screen_name, pass)
VALUES
(’$_POST[title]’,’$_POST[forename]’,’$_POST[surname]’,’$_POST[dob]’,’$_POST[email]’,’$_POST[screen_name]donkey’,’$_POST[pass]’)”;
if (!mysql_query($sql,$con))
{
die('Error: ’ . mysql_error());
}
echo “Thank You for registering a screen name. You can now post freely :)”;
mysql_close($con);
?>[/php]
Many Thanks,
Sam