mrheff Posted June 25, 2007 Share Posted June 25, 2007 hi guys. im doing a little online newletter signup type jobby for my bands website and all has been going well asides from one thing, I changed my database so as the email field was unique (which makes sense) but now if there is a mulitple inserted i get a nasty message multiple entruies in key 2, this does its job but look ugly ive got all my others going to nice redirect pages ie ok.php or unvalid.php this is the bit of code that is breaking my soul. $result=mysql_query($query) or die("Error in query:".mysql_error() and i was hoping i could do something like this if ($result=mysql_query($query) or die("Error in query:".mysql_error()); ) else ( print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; but that sure dont work. So if anybody has five minutes to take a peek i would sure appreciate it. Thanks Mr Heff Quote Link to comment Share on other sites More sharing options...
mrheff Posted June 25, 2007 Author Share Posted June 25, 2007 i also just tried this... $someurl= 'error.htm' $result=mysql_query($query) or die(header ('Location: ' . $someurl);()); Quote Link to comment Share on other sites More sharing options...
redarrow Posted June 25, 2007 Share Posted June 25, 2007 here a very simple example not using unique in the database ok. if you use this code please valadate all data sent to the database cheers. <?php //select the database. $db=mysql_connect("localhost","username","password"); $result=mysql_select_db("database",$bd); // post all the varables from the form $id=addslashes($_POST['id']); $name=addslashes($_POST['name']); $email=addslashes($_POST['email']); // if s user post from a form under the name submit. ifi(sset($_POST['submit'])){ // select the emails from the database . $query="SELECT 'id',`name`,`email` from `what_ever` "; $result2=mysql_query($query)or die("mysql_error()"); //if the database has got the same email as posted then error a me //ssage. if(mysql_num_rows($reslt2) > 0){ // message. echo "sorry database got that email try another please cheers"; }else{ //insert if all ok. $query2="insert into database_field(id,name,email)values('$id','$name','$email')"; $result3=mysql_query($query2); } //if the database is updated then send a message. if(mysql_affected_rows($result2)){ echo" thank you database entry updated and i sent you a confirmation email"; $to = $email; //from you the web site owner. $from='xxxxxxx'; //Subject line what ever. $subject = 'Thank you for joining my web site.'; $mess='<html><head><title>Thank you for joing my web site</title> <body> <h1>we look foward in given you a reel good time on our web site!<p></p> <a href="http://www.google.com">Go to my website know!</a></h1><p></p> </body></html>'; $message = $mess; $headers = "From: $from\r\n" . 'X-Mailer: PHP/' . phpversion() . "\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: text/html; charset=utf-8\r\n" . "Content-Transfer-Encoding: 8bit\r\n\r\n"; // send if correct. mail($to, $subject, $message, $headers); } } ?> Quote Link to comment Share on other sites More sharing options...
mrheff Posted June 25, 2007 Author Share Posted June 25, 2007 thanks for a great reply, however ive made my whole site and this is the last little bit Do you know of a way i can just add another line of code stating a redirect if i get back the mysql_error? $result=mysql_query($query) or die(("Error in query:".mysql_error);()); could i do simple if ($result=error) then (go to page)? thanks again for your reply if i have no luck going this way round then ive got another way round, thanks! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted June 25, 2007 Share Posted June 25, 2007 Personally i would recommed redarrows method of testing the email for a previous entry before you put it in the database. That way you know exactly what the problem is. It shouldn't be difficult to implement it into your current page. However, you COULD do: <?php $result = mysql_query(); if($result === false){ header("location:whatever.php"); } The problem with that is you will be assuming that the only error that will be generated is that of a non unique email. What if there is a another problem with the query? Quote Link to comment Share on other sites More sharing options...
mrheff Posted June 25, 2007 Author Share Posted June 25, 2007 Thankyou, I see your reasoning guys.... my method is rather a botch job.... i think ill still roll with it for now, just because i am comfortable with the current code should i come back to editing. cheers again! Quote Link to comment Share on other sites More sharing options...
mrheff Posted June 25, 2007 Author Share Posted June 25, 2007 Really odd, it just seems to bypass that part..... and now when i enter a duplicate, it still goes to ok.htm instead error.... odd. Quote Link to comment Share on other sites More sharing options...
mrheff Posted June 25, 2007 Author Share Posted June 25, 2007 So whats the simploest way i could do this, what is the simplest way for me to see if the entry exists then i could do a if (entry is in datbase) headr:duplicate.html else ( all the rest of my script ) do you think that would be viable? I use a simple form that passes on this info ip email and date the table has also got an "in" field this is just used by the confirmation process. Quote Link to comment Share on other sites More sharing options...
Jenk Posted June 25, 2007 Share Posted June 25, 2007 You need to read up on simple syntax, and also need to take note that "or die" is not the keyword, "or" is the keyword. <?php mysql_connect($host, $user, $pass) or header('Location: http://host/somepage.php'); ?> Quote Link to comment Share on other sites More sharing options...
mrheff Posted June 25, 2007 Author Share Posted June 25, 2007 thankyou. this is just what i needed, i see exactly what you mean, in the end i used or - print thankyou for clarifiying what my options where. ill take your advice and do that bit of reading. 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.