Snooble Posted July 18, 2007 Share Posted July 18, 2007 Hello everyone, I have a register submit form and a register check form. The register check form is supposed to send back what went wrong. and the register form is supposed to display the issues to the user. my registercheck.php file looks like this: <?php session_start(); $host="localhost"; $username="username"; $password="password"; $db_name="wezzsmusic"; $tbl_name="wmusers"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $errors = array(); if (empty($_POST['username'])) { $errors[] = 'Please supply a username'; } if (empty($_POST['password']) || strlen($_POST['password']) < 6) { $errors[] = 'You entered an invalid password'; } if (empty($_POST['email']) || strpos($_POST['email'], '@') === false) { $errors[] = 'You entered an invalid email'; } $check = "SELECT * FROM wmusers where username='".$_POST['username']."' LIMIT 1"; $checkresult = mysql_query($check); if (mysql_num_rows($checkresult) != 0) { $errors[] = 'You entered a taken username'; } $check2 = "SELECT * FROM wmusers where email='".$_POST['email']."' LIMIT 1"; $checkresult2 = mysql_query($check2); if (mysql_num_rows($checkresult2) != 0) { $errors[] = 'You entered a taken email'; } if (count($errors)) { header("Location: register.php"); exit; } else { $sql = "INSERT INTO wmusers VALUES ('0', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '0')"; mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error()); } ?> how would i send back the problems to register.php. I've never really worked with arrays. Thanks Snooble Quote Link to comment https://forums.phpfreaks.com/topic/60532-solved-register-check-easy-just-need-another-pair-of-eyes/ Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 <?php session_start(); $host="localhost"; $username="username"; $password="password"; $db_name="wezzsmusic"; $tbl_name="wmusers"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $errors = array(); $_SESSION['errors']= array(); //ADDED if (empty($_POST['username'])) { $errors[] = 'Please supply a username'; } if (empty($_POST['password']) || strlen($_POST['password']) < 6) { $errors[] = 'You entered an invalid password'; } if (empty($_POST['email']) || strpos($_POST['email'], '@') === false) { $errors[] = 'You entered an invalid email'; } $check = "SELECT * FROM wmusers where username='".$_POST['username']."' LIMIT 1"; $checkresult = mysql_query($check); if (mysql_num_rows($checkresult) != 0) { $errors[] = 'You entered a taken username'; } $check2 = "SELECT * FROM wmusers where email='".$_POST['email']."' LIMIT 1"; $checkresult2 = mysql_query($check2); if (mysql_num_rows($checkresult2) != 0) { $errors[] = 'You entered a taken email'; } $_SESSION['errors'] = $errors; //ADDED if (count($errors)) { header("Location: register.php"); exit; } else { $sql = "INSERT INTO wmusers VALUES ('0', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '0')"; mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error()); } ?> in register.php <?php session_start(); echo "<pre>"; print_r($_SESSION['errors']); ?> **UNTESTED Quote Link to comment https://forums.phpfreaks.com/topic/60532-solved-register-check-easy-just-need-another-pair-of-eyes/#findComment-301112 Share on other sites More sharing options...
DeadEvil Posted July 18, 2007 Share Posted July 18, 2007 Try this one. <?php session_start(); $host="localhost"; $username="username"; $password="password"; $db_name="wezzsmusic"; $tbl_name="wmusers"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $errors = ""; $errors = array(); if (empty($_POST['username'])){ $errors[] = 'Please supply a username'; } if (empty($_POST['password']) || strlen($_POST['password']) < 6){ $errors[] = 'You entered an invalid password'; } if (empty($_POST['email']) || strpos($_POST['email'], '@') === false){ $errors[] = 'You entered an invalid email'; } $check = "SELECT * FROM wmusers where username='".$_POST['username']."' LIMIT 1"; $checkresult = mysql_query($check); if (mysql_num_rows($checkresult) != 0){ $errors[] = 'You entered a taken username'; } $check2 = "SELECT * FROM wmusers where email='".$_POST['email']."' LIMIT 1"; $checkresult2 = mysql_query($check2); if (mysql_num_rows($checkresult2) != 0){ $errors[] = 'You entered a taken email'; } if (!empty($errors)){ header("Location: register.php?".http_build_query($errors)); exit; } else{ $sql = "INSERT INTO wmusers VALUES ('0', '".$_POST['username']."', '".$_POST['password']."', '".$_POST['email']."', '0')"; mysql_query($sql) or die ("Couldn't execute $sql: " . mysql_error()); } ?> register.php <? # Print Error Msg print $_GET['0']; print $_GET['1']; print $_GET['2']; print $_GET['3']; print $_GET['4']; ?> Let me know if it's work Quote Link to comment https://forums.phpfreaks.com/topic/60532-solved-register-check-easy-just-need-another-pair-of-eyes/#findComment-301116 Share on other sites More sharing options...
Snooble Posted July 18, 2007 Author Share Posted July 18, 2007 MadTechie... That looks good to me... but the output to the user looks like this: Array ( [0] => Please supply a username [1] => You entered an invalid password [2] => You entered an invalid email [3] => You entered a taken username [4] => You entered a taken email ) i dont want the Array nor the () bits. nor the [number] => bits. thanks though Snooble Quote Link to comment https://forums.phpfreaks.com/topic/60532-solved-register-check-easy-just-need-another-pair-of-eyes/#findComment-301122 Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 OK change <?php session_start(); echo "<pre>"; print_r($_SESSION['errors']); ?> to <?php session_start(); foreach ($_SESSION['errors'] as $errorlog) { echo "$errorlog<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/60532-solved-register-check-easy-just-need-another-pair-of-eyes/#findComment-301151 Share on other sites More sharing options...
Snooble Posted July 18, 2007 Author Share Posted July 18, 2007 that give me an error: Warning: Invalid argument supplied for foreach() in C:\wamp\www\e\2\register.php on line 115 thanks... what now? Snoobs Quote Link to comment https://forums.phpfreaks.com/topic/60532-solved-register-check-easy-just-need-another-pair-of-eyes/#findComment-301222 Share on other sites More sharing options...
MadTechie Posted July 18, 2007 Share Posted July 18, 2007 humm try <?php session_start(); $errors =$_SESSION['errors']; //var_dump($errors); //uncomment if doesn't work and post the output if(count($errors) > 0) { foreach($errors as $errorlog) { echo "$errorlog<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/60532-solved-register-check-easy-just-need-another-pair-of-eyes/#findComment-301231 Share on other sites More sharing options...
Snooble Posted July 18, 2007 Author Share Posted July 18, 2007 that works perfectly... thank you very much for all your help... it's a very customisable script too! thanks Snooble TOPIC SOLVED Quote Link to comment https://forums.phpfreaks.com/topic/60532-solved-register-check-easy-just-need-another-pair-of-eyes/#findComment-301308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.