matt.sisto Posted March 24, 2009 Share Posted March 24, 2009 I am trying to get the my form to echo a message if the email address already exists, can someone point out what is wrong with my code. addconsultantform.php <?php require "dbconn2.php"; $error = $_GET['error']; $sql="SELECT rate_id, rate FROM rate"; $result=mysql_query($sql); $rate_option=""; while ($row=mysql_fetch_array($result)) { $id=$row["rate_id"]; $rate=$row["rate"]; $rate_option.="<OPTION VALUE=\"$id\">".$rate; } if ($error !=='0') { echo '<h2>Sorry but an account with this email address already exists</h2>'; } ?> addconsultant.php: $sql = mysql_query("SELECT * FROM consultant WHERE email_address = '$email_address'"); if (mysql_num_rows($sql) == 0) { $sql = "INSERT INTO consultant VALUES (0,'".$first_name."','".$last_name."','".$address_first_line."','".$post_code."', '".$email_address."','".$phone_number."','".$dob."','".$rate."','".$enc."')"; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: addconsultantform.php"); exit(); } else{ $error = $_POST['error']; header("Location: addconsultantform.php"); exit(); } Appreciate any help. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted March 24, 2009 Share Posted March 24, 2009 You're not passing $error in the header try this $error = $_POST['error']; header("Location: addconsultantform.php?error=".$error); otherwise your $_GET['error'] doesn't exist I am trying to get the my form to echo a message if the email address already exists, can someone point out what is wrong with my code. addconsultantform.php <?php require "dbconn2.php"; $error = $_GET['error']; $sql="SELECT rate_id, rate FROM rate"; $result=mysql_query($sql); $rate_option=""; while ($row=mysql_fetch_array($result)) { $id=$row["rate_id"]; $rate=$row["rate"]; $rate_option.="<OPTION VALUE=\"$id\">".$rate; } if ($error !=='0') { echo '<h2>Sorry but an account with this email address already exists</h2>'; } ?> addconsultant.php: $sql = mysql_query("SELECT * FROM consultant WHERE email_address = '$email_address'"); if (mysql_num_rows($sql) == 0) { $sql = "INSERT INTO consultant VALUES (0,'".$first_name."','".$last_name."','".$address_first_line."','".$post_code."', '".$email_address."','".$phone_number."','".$dob."','".$rate."','".$enc."')"; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: addconsultantform.php"); exit(); } else{ $error = $_POST['error']; header("Location: addconsultantform.php"); exit(); } Appreciate any help. Quote Link to comment Share on other sites More sharing options...
matt.sisto Posted March 24, 2009 Author Share Posted March 24, 2009 Now it is just permanently displaying the message. ??? Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 i think it is still incorrect. on addconsultantform.php you are attempting to retrieve $error as $_GET['Error'] however you are launching this page from here header("Location: addconsultantform.php"); This URL will never have Error as a condition for $_GET to retrieve. If you really want to do it this way, you will have to do this <?php else{ $error = $_POST['error']; $url = "Location: addconsultantform.php?error=".$error; header($url); exit(); } ?> furthermore, $error is not always going to be set, so you should be using if(isset($_GET['error'])) { $error = $_GET['error']; } Quote Link to comment Share on other sites More sharing options...
matt.sisto Posted March 24, 2009 Author Share Posted March 24, 2009 addconsultantform.php: <?php require "dbconn2.php"; if(isset($_GET['error'])) { $error = $_GET['error']; } $sql="SELECT rate_id, rate FROM rate"; $result=mysql_query($sql); $rate_option=""; while ($row=mysql_fetch_array($result)) { $id=$row["rate_id"]; $rate=$row["rate"]; $rate_option.="<OPTION VALUE=\"$id\">".$rate; } if ($error !=='0'){ echo '<h2>Sorry but an account with this email address already exists</h2>'; } ?> addconsultant.php: <?php $sql = mysql_query("SELECT * FROM consultant WHERE email_address = '$email_address'"); if (mysql_num_rows($sql) == 0) { $sql = "INSERT INTO consultant VALUES (0,'".$first_name."','".$last_name."','".$address_first_line."','".$post_code."', '".$email_address."','".$phone_number."','".$dob."','".$rate."','".$enc."')"; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: addconsultantform.php"); exit(); } else{ $error = $_POST['error']; $url = "Location: addconsultantform.php?error=".$error; header($url); exit(); } ?> Like this? Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 basically, yes. Does it work better ? of course this is also dependent on what exactly the contents of $_POST['error'] are on addconsultant.php Quote Link to comment Share on other sites More sharing options...
matt.sisto Posted March 24, 2009 Author Share Posted March 24, 2009 No it still doesn't work. is there a better way to do this? Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 well step through it, what exactly are you seeing for it to not work 1) is this for a new entry or an existing entry 2) should an error exist or not 3) if the error exists, does the header redirect include the error condition 4) echo $error to see what it contains on both pages (temporarily comment out the redirect on your form page so you can see the contents of $error) Quote Link to comment Share on other sites More sharing options...
matt.sisto Posted March 24, 2009 Author Share Posted March 24, 2009 Well it doesn't insert a row into the table if the email address is there, and it redirects to the form, but fails to show the message. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 and what are the contents of $error on both pages ? where is $_POST['error'] coming from on your form ? perhaps if you could post your form code as well we can isolate the problem. What page are you submitting from to get to addconsultant.php Quote Link to comment Share on other sites More sharing options...
matt.sisto Posted March 24, 2009 Author Share Posted March 24, 2009 addconsultantform.php(this is my full script, just ignore the css) <?php require "dbconn2.php"; if(isset($_GET['error'])) { $error = $_GET['error']; } $sql="SELECT rate_id, rate FROM rate"; $result=mysql_query($sql); $rate_option=""; while ($row=mysql_fetch_array($result)) { $id=$row["rate_id"]; $rate=$row["rate"]; $rate_option.="<OPTION VALUE=\"$id\">".$rate; } if ($error !=='0'){ echo '<h2>Sorry but an account with this email address already exists</h2>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Add a New Consultant</title> <style type="text/css"> #label1 { text-align:justify; position:absolute; left:170px; top:70px; } #input1 { text-align:right; position:absolute; left:270px; top:65px; } #label2 { text-align:justify; position:absolute; left:170px; top:100px; } #input2 { text-align:right; position:absolute; left:270px; top:95px; } #label3 { text-align:justify; position:absolute; left:170px; top:130px; } #input3 { text-align:right; position:absolute; left:270px; top:125px; } #label4 { text-align:justify; position:absolute; left:170px; top:160px; } #input4 { text-align:right; position:absolute; left:270px; top:155px; } #label5 { text-align:justify; position:absolute; left:170px; top:190px; } #input5 { text-align:right; position:absolute; left:270px; top:185px; } #label6 { text-align:justify; position:absolute; left:170px; top:220px; } #input6 { text-align:right; position:absolute; left:270px; top:215px; } #label7 { text-align:justify; position:absolute; left:170px; top:250px; } #input7 { text-align:right; position:absolute; left:270px; top:245px; } #label8 { text-align:justify; position:absolute; left:170px; top:310px; } #select1 { position:absolute; left:310px; top:305px; } #label9 { text-align:justify; position:absolute; left:170px; top:280px; } #select2 { text-align:right; position:absolute; left:260px; top:275px; width:60px; } #select3 { text-align:right; position:absolute; left:325px; top:275px; width:60px; } #select4 { text-align:right; position:absolute; left:390px; top:275px; width:50px; } form { font-family:Verdana, Geneva, sans-serif; font-size:10px; text-align:justify; } h1 { font-family:Verdana, Geneva, sans-serif; text-indent:2px; font-size:14px; position:absolute; left:10px; top:5px; padding-bottom:5px; } #button1 { width:75px; height:50px; font-family:Verdana, Geneva, sans-serif; font-size:9px; float:left; position:absolute; top:70px; left:2px; } #button2 { width:75px; height:50px; font-family:Verdana, Geneva, sans-serif; font-size:9px; float:left; position:absolute; top:120px; left:2px; } #button3 { width:75px; height:50px; font-family:Verdana, Geneva, sans-serif; font-size:9px; float:left; position:absolute; top:170px; left:2px; } #button4 { width:75px; height:50px; font-family:Verdana, Geneva, sans-serif; font-size:9px; float:left; position:absolute; top:220px; left:2px; } #copyright { position:absolute; font-family:Verdana, Geneva, sans-serif; font-size:9px; bottom:2px; right:450px; } </style> </head> <body> <body> <h1>New Consultant Details</h1> <form name="addconsultant" method="post" action="addconsultant.php"> <p> <label id="label1">Firstname</label> <input id="input1" type="text" name="first_name" value="<?=$row['first_name']?>" /></p> <p> <label id="label2">Surname</label> <input id="input2" type="text" name="last_name" value="<?=$row['last_name']?>" /></p> <p> <label id="label3">Address<font size="-20">[First Line]</font></label> <input id="input3" type="text" name="address_first_line" value="<?=$row['address_first_line']?>" /></p> <p> <label id="label4">Post Code</label> <input id="input4" type="text" name="post_code" value="<?=$row['post_code']?>" /></p> <p> <label id="label5">Email Address</label> <input id="input5" type="text" name="email_address" value="<?=$row['email_address']?>" /></p> <p> <label id="label6">Phone Number</label> <input id="input6" type="text" name="phone_number" value="<?=$row['phone_number']?>" /></p> <p> <label id="label9">Date Of Birth</label> <select id="select2" name="year" value="<?=$row['y']?>"> <?php for ($x= 1950; $x<=1980; $x++) { echo "<option"; if ($x == $y) { echo " selected"; } echo ">$x</option>"; } ?> </select> <select id="select3" name="month" value="<?=$row['m']?>"/> <?php $months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); for ($x=1; $x <= count($months); $x++) { echo"<option value=\"$x\""; if ($x == $m) { echo " selected"; } echo ">".$months[$x-1]."</option>"; } ?> </select> <select id="select4" name="day" value="<?=$row['d']?>"> <?php for ($x= 1; $x<=31; $x++) { echo "<option"; if ($x == $d) { echo " selected"; } echo ">$x</option>"; } ?> </select></p> <p> <label id="label8">Rate ID</label> <select id="select1" name="rate"> <option value="<?=$row['rate']?>">Choose <?=$rate_option?> </option> </select></p> <p> <label id="label7">Password</label> <input id="input7" type="password" name="passwd" value="<?=$row['passwd']?>" /></p> <input id="button1" type ="submit" value="save"/> </form> <input id="button2" type="submit" onClick="javascript:self.close()" value="close"> </body> </html> addconsultant.php: <?php require "dbconn2.php"; $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $address_first_line = $_POST['address_first_line']; $post_code = $_POST['post_code']; $email_address = $_POST['email_address']; $phone_number = $_POST['phone_number']; $y = $_POST['y']; $m = $_POST['m']; $d = $_POST['d']; $rate_id = $_POST['rate']; $passwd = $_POST['passwd']; $enc = sha1($passwd); $dob = $y."-".$m."-".$d." ".$_POST["dob"]; $sql = mysql_query("SELECT * FROM consultant WHERE email_address = '$email_address'"); if (mysql_num_rows($sql) == 0) { $sql = "INSERT INTO consultant VALUES (0,'".$first_name."','".$last_name."','".$address_first_line."','".$post_code."', '".$email_address."','".$phone_number."','".$dob."','".$rate."','".$enc."')"; $result = mysql_query ($sql, $connection) or die ("Couldn't perform query $sql <br />".mysql_error()); header("Location: addconsultantform.php"); exit(); } else{ $error = $_POST['error']; $url = "Location: addconsultantform.php?error=".$error; header($url); exit(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Add Consultant</title> </head> <body> </body> </html> Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 this is the third time you have ignored my question about the contents of $error and $_POST['error'] on your pages Quote Link to comment Share on other sites More sharing options...
matt.sisto Posted March 24, 2009 Author Share Posted March 24, 2009 Sorry, well I am trying to set the $error if the email address exists so it passes the error back to the form to display the message. Quote Link to comment Share on other sites More sharing options...
lonewolf217 Posted March 24, 2009 Share Posted March 24, 2009 it doesn't work like that. as far as I can tell, $_POST['error'] is always going to be empty because the error does not come from your form. it comes from the SQL statement if the address already exists. Considering you seem to only have one error condition, try this addconsultant.php: $url = "Location: addconsultantform.php?error=true"; header($url); addconsultantform.php <?php require "dbconn2.php"; if(isset($_GET['error']) && $_GET['error']=='true') { echo '<h2>Sorry but an account with this email address already exists</h2>'; } else { $sql="SELECT rate_id, rate FROM rate"; $result=mysql_query($sql); $rate_option=""; while ($row=mysql_fetch_array($result)) { $id=$row["rate_id"]; $rate=$row["rate"]; $rate_option.="<OPTION VALUE=\"$id\">".$rate; } } ?> Quote Link to comment Share on other sites More sharing options...
matt.sisto Posted March 24, 2009 Author Share Posted March 24, 2009 This works thanks mate. I made one change: <?php require "dbconn2.php"; if(isset($_GET['error']) && $_GET['error']=='true') { echo '<h2>Sorry but an account with this email address already exists</h2>'; } $sql="SELECT rate_id, rate FROM rate"; $result=mysql_query($sql); $rate_option=""; while ($row=mysql_fetch_array($result)) { $id=$row["rate_id"]; $rate=$row["rate"]; $rate_option.="<OPTION VALUE=\"$id\">".$rate; } ?> This is now solved. Quote Link to comment 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.