Simple Code Not Working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jballou
Forum Newbie
Posts: 15
Joined: Mon Mar 08, 2004 2:34 pm
Location: San Francisco, CA

Simple Code Not Working

Post by jballou »

Hey everyone,

I have NO idea why the following code isn't executing the header command located in the if statement. I've put a echo in there to make sure it was evaluating correctly, and that printed as expected. I've also moved the header command elsewhere to make sure it worked, which it did. But for some reason this isn't working. The SQL is also executing and putting everything in the database as expected. The only thing that doesn't work is the header command and it only fails when it's placed inside the if statement, even though the if statement is true. I've used similar code tons of times with no problems and I'm getting frustrated with this. Any ideas?

Code: Select all

<?php

if(isset($_POST['addNew'])) {
	
	require("sources/connection.php");
	
	$court_name = $_POST['model_name'];
	$court_type = $_POST['height'];
	$court_num = $_POST['weight'];
	$eye_color = $_POST['eye_color'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$country = $_POST['country'];
	
	$interview = $_POST['interview'];
	$gallery = $_POST['gallery'];
	$video = $_POST['video'];
	$test1 = $_POST['test1'];
	$test2 = $_POST['test2'];
	$test3 = $_POST['test3'];
	$test4 = $_POST['test4'];
	

	//$date_added = date ("Y-m-d H:i:s");
	
	
	$sql = "INSERT INTO tester VALUES ('', '$court_name', '$court_type', '$court_num', '$eye_color', '$city', '$state', '$country', '$interview', '$gallery', 
	'$video', '$test1', '$test2', '$test3', '$test4')";
	
	$result = $conn->query($sql) or die(mysqli_error());
	
	if($result){
		header("Location: cmsAdd.php?message=1");
	}
	else {
		echo "Update was not successful";
	}
}

?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple Code Not Working

Post by AbraCadaver »

Headers might already have been sent so to see that try this at the top:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');
Also, the Location header is supposed to have an absolute URL (http://example.com/cmsAdd.php?message=1). And for completeness, you need an exit; after the header.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Simple Code Not Working

Post by McInfo »

There could be whitespace outside the PHP tags in the posted file or in connection.php.
User avatar
jballou
Forum Newbie
Posts: 15
Joined: Mon Mar 08, 2004 2:34 pm
Location: San Francisco, CA

Re: Simple Code Not Working

Post by jballou »

@McInfo - you are exactly right. Thanks so much!
Post Reply