Jump to content

[SOLVED] or die to redirect


mrheff

Recommended Posts

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

 

Link to comment
Share on other sites

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);

}
}
?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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');

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.