PHP / MySQL (Die)

Status
Not open for further replies.

! Whitey !

Baseband Member
Messages
23
Hi,

Could anyone help?!

I have some code that checks to see if some fields are filled in and then if they are it adds them to a database, if not display a message and kill the command (die).

But it gets to die and it doesn't write the rest of the page.

This is some of the code:

PHP:
if(isset($_POST['join'])){
   /* Make sure fields are entered */
   if(!$_POST['firstname'] || !$_POST['surname']){
      die('You haven\'t filled in all the fields!');
   }

But when the page is generated this is what happens:

HTML:
<!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>Registration Page</title>
</head>

<body>

<h1>Register</h1>
You haven't filled in all the fields!

And it doesn't write the rest of the page.

I could make it write the rest of the page in the IF statement but I don't want to do that because the content could change and there is a lot to come after it.

Please suggest something!

Thanks

Dave
 
Hi,

If I change it to echo it will work, but then it will add to the database, when I don't want it to because it doesn't end the query.
 
Sorry im not following why you dont want to use an IF statement. Its late and its hard to understand. Haha.

Die pretty much means, end the entire page from that point, just if you where wondering. Explain a bit more and ill try to help. Thanks.
 
Ok,

I will rephrase what I want it to do I'm probably not explaining it properly.

I want the IF statement, I got it to check if the fields are filled in. If they are then I want it to insert it into the database, if not I want it to end the query so it doesn't insert into the database and display a message in the page.

Hope this is a better explanation?!

Thanks for your help!
 
PHP:
<?php

if (isset($_POST['join'])) {
	$firstname = $_POST['firstname'];
	$surname   = $_POST['surname'];
	
	if(empty ($firstname) || empty ($surname)) {
		echo 'You haven\'t filled in all the fields!';
	} else {
		$sql = mysql_query ("INSERT INTO table (firstname,surname) VALUES ('".$firstname."','".$surname."')");
		echo 'Your entry has been saved to the database!';
	}
} else {
	// get form
}

?>
 
Status
Not open for further replies.
Back
Top Bottom