if there are no errors, you don't define the $message variable. To avoid the error, just put the next line before you start checking the form data:
$message = "";
| SitePoint Sponsor |
if there are no errors, you don't define the $message variable. To avoid the error, just put the next line before you start checking the form data:
$message = "";
$link what?dbconn.inc
PHP Code:<?php
$link;
If you want to define it, write
PHP Code:$link = "";

OK, thanks
How about the button 'Undefined Index' message? It is this that must be causing the page not to be redirected.


Oracle,
I have simplified the code a bit. You can add your error functions and cleanMemberSession in as you like.
Should work but I haven't tested it.PHP Code:<?
# put your connections into a normal php page and include them....
#$link = mysql_connect("localhost","root","quiksilver");
# incoming link from previous page. Defined as 'action'
# Use either text link or graphical button, if you do use a form set the method to GET
# set the links like <a href="formpage.php?action=page_1">link</a>
if(isset($_GET['action'])) {
if($_GET['action'] == 'page1') {
$redirect = "redirect_page_1.php";
}
if($_GET['action'] == 'page2') {
$redirect = "redirect_page_2.php";
}
if($_GET['action'] == 'page3') {
$redirect = "redirect_page_3.php";
}
if($_GET['action'] == 'page4') {
$redirect = "redirect_page_4.php";
}
}
if(isset($_POST['formsubmit'])) {
$error_header = "The following errors occurred, please correct them<br>";
#put in your error checking
if(!$_POST['email']) {
$error_msg = "Please Fill in the email field<br>";
}
if(!$_POST['password']) {
# note the .= bit.
$error_msg .= "Please fill in the password field<br>";
}
if(!$error_msg) {
mysql_select_db($dbase, $link);
$result=mysql_query("insert into user_details (username, password)
values('$login', '$pass')") or die(mysql_error());
header("Location: ".$redirect);
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<? if(isset($error_msg)) {
echo $error_header."<br>". $error_msg;
} ?>
<form action="<? $_SERVER['PHP_SELF']; ?>" method="post" name="email">
<input type="text" name="email">
email<br>
<input type="password" name="password">
password <br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</form>
</body>
</html>
SpikeZ
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

SpikeZ,
Thanks very much for your help, but there appears to be bits missing (other than what you have specified).
Mainly, the GetRow function which checks against the database and stops duplicate rows being added.
I'm also not that clear on what I need to include. Do I keep my original dbconn.inc file and funclib.inc file.
Also, on the redirects at the top, should I still use SID, and if so what exactly does this do (some kind of Session thing??).
Sorry, but as I am quite new to PHP it kinda makes a bit harder to decipher someone elses code.
Many thanks


Oracle,
SID is the Session Id and is useful for tracking purposes.
Below is the revised code
create a php page with the first part called link.php
Functions are vital for things that you will be using time and time again, however if you are just using it once like the checkRows here, just do a basic select check.PHP Code:# link.php
<?php
$hostname_pdb_conn = "host";
$database_pdb_conn = "database";
$username_pdb_conn = "username";
$password_pdb_conn = "password";
$link = mysql_pconnect($hostname_pdb_conn, $username_pdb_conn, $password_pdb_conn) or trigger_error(mysql_error(),E_USER_ERROR);
?>
<?
require_once('link.php');
include("includes/funclib.inc");
# put your connections into a normal php page and include them....
#$link = mysql_connect("localhost","root","quiksilver");
# incoming link from previous page. Defined as 'action'
# Use either text link or graphical button, if you do use a form set the method to GET
# set the links like <a href="formpage.php?action=page_1">link</a>
if(isset($_GET['action'])) {
if($_GET['action'] == 'page1') {
$redirect = "redirect_page_1.php";
}
if($_GET['action'] == 'page2') {
$redirect = "redirect_page_2.php";
}
if($_GET['action'] == 'page3') {
$redirect = "redirect_page_3.php";
}
if($_GET['action'] == 'page4') {
$redirect = "redirect_page_4.php";
}
}
if(isset($_POST['formsubmit'])) {
$error_header = "The following errors occurred, please correct them<br>";
#put in your error checking
if(!$_POST['email']) {
$error_msg = "Please Fill in the email field<br>";
}
if(!$_POST['password']) {
# note the .= bit.
$error_msg .= "Please fill in the password field<br>";
}
if(strlen($password) > 8)
$error_msg .= "Your password must be less than 8 characters<br>";
if (!preg_match('/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/',$login))
$error_msg .= "Please enter a valid email address<br>" ;
# check for duplicate rows etc
$rowCheck = mysql_query("select username from user_details where username = ".$_POST['username']);
if(mysql_num_rows($rowCheck >0) {
$error_msg .= "The username/ email already exists, please select another one";
}
###########
if(!$error_msg) {
# your function #
cleanMemberSession($id, $login, $password);
##
mysql_select_db($database_pdb_conn, $link);
$result=mysql_query("insert into user_details (username, password)
values('$login', '$pass')") or die(mysql_error());
header("Location: ".$redirect);
}
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<? if(isset($error_msg)) {
echo $error_header."<br>". $error_msg;
} ?>
<form action="<? $_SERVER['PHP_SELF']; ?>" method="post" name="email">
<input type="text" name="email">
email<br>
<input type="password" name="password">
password <br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" value="Reset">
</form>
</body>
</html>
SpikeZ
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

Hey SpikeZ,
Did you also miss out a hidden field or something for the 'formsubmit' variable? And the form should have 2 password fields. Not sure if that makes a difference to your code?
Is the $dbase variable supposed to be a mysql_select_db in one of the includes?


Yep, missed out the hidden field, just checking you were paying attention!
I also missed out mysql_select_db($database_pdb_conn, $link); before the duplicate check and yes, this is the link to $link.php
For the two passwords, simply put back the bit of code from your original script that reads
PHP Code:if($password!=$password2) {
$message .= "<span class='style32'>Your passwords did not match<BR></span> \n";
}
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

Hi SpikeZ, I had to fiddle with the syntax a little (FEW ERRORS), BUT i GOT IT TO DISPLAY THE FORM, AND NOW WHEN i ENTER SOMETHING i GET:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\phpWeb\Right-Track\authtest.php on line 50
This is the line:
Any ideas?PHP Code:if(mysql_num_rows($rowCheck > 0))

SpikeZ,
I also tried changing it to:
but I still get the same errorPHP Code:$rowCheckResult = mysql_num_rows($rowCheck);
if($rowCheckResult > 0)


OK, change this bit
toPHP Code:$rowCheck = mysql_query("select username from user_details where username = ".$_POST['username']);
This will give you the error code and give you a clue what's going on.PHP Code:$rowCheck = mysql_query("select username from user_details where username = ".$_POST['username']) or die(mysql_error());
Just passing through BTW so will only be online for 30minutes or so!
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

I get this when running the script:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Ok, I fixed that error.
Only problem I have now is that even if I put in a valid email address it comes up with, please enter a valid email address. Every time!!!!


Sorry was a bit tired yesterday,
this line
should have read...PHP Code:if(mysql_num_rows($rowCheck >0) {
so that if the result is 0, add to the error message.PHP Code:if(mysql_num_rows($rowCheck = 0) {
Sorry bud, my bad
SpikeZ
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

Hi SpikeZ,
Thanks. I did try that ,but it wasn't even getting that far. The error was in the select. The $_POST['username'] should have read = ' ".$_POST['username']" ' ")
This seemed to stop the error occuring. Although, as per my previous post, it just says 'Please enter a valid email address' all the time now (once data is submitted) even if it is a valid email address!
Any ideas?

THis is the code as it now stands:
PHP Code:<?php
ob_start();
require_once('includes/link.php');
include("includes/funclib.inc");
# incoming link from previous page. Defined as 'action'
# Use either text link or graphical button, if you do use a form set the method to GET
# set the links like <a href="formpage.php?action=page_1">link</a>
if(isset($_GET['action'])) //variable set by URL on index.php
{
if($_GET['action'] == 'page_1')
{
$redirect = "redirect_page_1.php";
}
if($_GET['action'] == 'page_2')
{
$redirect = "redirect_page_2.php";
}
if($_GET['action'] == 'page_3')
{
$redirect = "redirect_page_3.php";
}
if($_GET['action'] == 'page_4')
{
$redirect = "redirect_page_4.php";
}
}
if(isset($_POST['formsubmit'])) //run when form submitted
{
$error_header = "The following errors occurred, please correct them<br>";
if(!$_POST['email'])
{
$error_msg = "Please Fill in the email field<br>";
}
if(!$_POST['password'])
{
$error_msg .= "Please fill in the password field<br>";
}
if($_POST['password']!=$_POST['password2'])
{
$message .= "<span class='style32'>Your passwords did not match<BR></span> \n";
}
if(strlen($password) > 8)
{
$error_msg .= "Your password must be less than 8 characters<br>";
}
if (!preg_match('/^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/',$login))
{
$error_msg .= "Please enter a valid email address<br>" ;
}
$rowCheck = mysql_query("select username from user_details where username = '".$_POST['username']."'") or die(mysql_error()); // check for duplicate rows
if (mysql_num_rows($rowCheck) > 0)
{
$error_msg .= "This email address has already been used, If you would like to make another application, please contact us by telephone";
}
if(!$error_msg) // run this if no errors
{
cleanMemberSession($id, $login, $password); //set session variables
mysql_select_db($database_pdb_conn, $link);
$result=mysql_query("insert into user_details (username, password)
values('$login', '$pass')") or die(mysql_error());
header("Location: ".$redirect);
}
}
?>

OK, I figured it out. The error checking for the email address is still looking for the $login variable which has changed to $_POST['email'], and also the select is trying to find &_POST['username'] instead of $_POST['email']. I have now changed both of these, but I think there is an error in the syntax somewhere becasue when I submit the form now I get:
Duplicate entry '' for key 2
Any ideas?


OK, so now we have got past all the error checking etc and are up to the insert query.
Check that the variables $login and $pass are correct and exist. Also check your database table settings to see if there are any unecessary line entries.
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

Thankyou sooo much for all your help SpikeZ
I changed the insert statement to read:
And I then checked in phpMyAdmin and it had inserted the correct data into the table and redirected me to the specified page.PHP Code:$result=mysql_query("insert into user_details (username, password)
values('".$_POST['email']."', '".$_POST['password']."')") or die(mysql_error());
I have now checked all of the error checking aswell, and the only bit that doesn't work is the following:
Is it a syntax thing again? It just accepts the values and redirects.PHP Code:if($_POST['password']!=$_POST['password2'])
{
$message .= "<span class='style32'>Your passwords did not match<BR></span> \n";
}

Sorry, ignore me, I hadn't changed the variable from $message to $error_msg
D'oh!
Thanks again SpikeZ. I'm sure I'll need to call on you for help again!


No problem Oracle,
Good luck with the rest of it!
SpikeZ
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

Haha, I knew it was too good to be true. Not really.
Just a quick one. What is the value of adding .SID after .$redirect i.e. .$redirect.SID
I still wasn't sure what it did?


SID is the unique id of the generated session you are currently using. It's is good for monitoring users and also for security if you use the session id as your session name.
Mike Swiffin - Community Team Leader
Only a woman can read between the lines of a one word answer.....
I started out with nothing... and still got most of it left!

Ok, so is it supposed to show up at the end of the URL when they are redirected, or is it hidden?
Bookmarks