/    Sign up×
Community /Pin to ProfileBookmark

header (Location)

I’m having a slight problem with my script and redirecting it back to the form.

The script connects to the MySQL 5.0 database (i know this works), collects and “sanatizes” a bunch of $_POST variables (i know the data is passed and collected), writes a valid INSERT statement, and mysql_query($insert)’s the data to the database (I know it does that, too).

The header ( “Location: ); doesn’t want to send it back to the form.

[code=php]
$insert = “INSERT INTO `table1` ( `eventID` , `currentEvent` , `eventTitle` , `eventDate` , `eventLocation` , `eventTime` , `eventDetails` , `dinnerLocation` , `dinnerTime` , `dinnerDetails` , `eventRSVP` , `notes` )
VALUES (
”, ‘0’, ‘$eventTitle’, ‘$eventDate’, ‘$eventLocation’, ‘$eventTime’, ‘$eventDetails’, ‘$dinnerLocation’, ‘$dinnerTime’, ‘$dinnerDetails’, ‘$eventRSVP’, ‘$eventFile’
)”;
$update=mysql_query($insert);

if(!$update) {
if(!mysql_num_rows($update)==1) {
echo “<p>update failed</p>”;
die(mysql_error());
}
else {
header ( “Location: http://www.aicwisconsin.com/dev/addevent.php”);
}
}
[/code]

This (used to) be almost a carbon-copy of another script that does work, which is why I’m extra confused. I’ve been staring at this too long, that my novice brain is throwing up the proverbial white flag.

full [cleaned] code below:

[code=php]
// database connection
// Open a connection to the MySQL database server
$link = mysql_connect(‘host’,’user’,’pass’);
if (!$link)
exit(‘Connection to MySQL server failed!’);

//Select database
$db=mysql_select_db(“database”);
if(!$db) {
die(“Unable to select database”);
}

//Sanitize the value received from login field to prevent SQL Injection
if(!get_magic_quotes_gpc()) {
$eventTitle=mysql_real_escape_string($_POST[‘eventTitle’]);
}else {
$eventTitle=$_POST[‘eventTitle’];
}

// etc for all remaining variables.

$postback = $_POST[‘postback’];

$insert = “INSERT INTO `table1` ( `eventID` , `currentEvent` , `eventTitle` , `eventDate` , `eventLocation` , `eventTime` , `eventDetails` , `dinnerLocation` , `dinnerTime` , `dinnerDetails` , `eventRSVP` , `notes` )
VALUES (
”, ‘0’, ‘$eventTitle’, ‘$eventDate’, ‘$eventLocation’, ‘$eventTime’, ‘$eventDetails’, ‘$dinnerLocation’, ‘$dinnerTime’, ‘$dinnerDetails’, ‘$eventRSVP’, ‘$eventFile’
)”;

$update=mysql_query($insert);

if(!$update) {
if(!mysql_num_rows($update)==1) {
echo “<p>update failed</p>”;

die(mysql_error());
}
else {
header ( “Location: http://www.aicwisconsin.com/dev/addevent.php”);
}
}
else {
die(mysql_error());
}

[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@sstalderJul 21.2008 — Do you receive an error or what happens? If you do not get an error turn on error_reporting() so you can receive the error.
Copy linkTweet thisAlerts:
@rootJul 21.2008 — You have: $update=mysql_query($insert);

It is best to: $update=mysql_query($insert,$link);

Also... Try reading more here: http://us2.php.net/header & here: http://us3.php.net/mysql_query
Copy linkTweet thisAlerts:
@malarzJul 21.2008 — It is good to do die() or exit() right after header(' Location: ...'); otherwise script keeps going
Copy linkTweet thisAlerts:
@SyCoJul 22.2008 — echo something before the header so you can be sure you have entered the part of logic you think you have.

Next test
[code=php]header ( "Location: http://www.aicwisconsin.com/dev/addevent.php");[/code]
as the only line in a test script and run it to be sure your address/syntax is correct.

You could submit to the same page and not have to locate anywhere.
Copy linkTweet thisAlerts:
@OctoberWindauthorJul 22.2008 — I've come to determine that the logic I was using to determine if the mysql_query() was unsuccessful was either wrong, backwards, or just "illogical".

By removing the testing of variables, it works like it should, inserting the data and redirecting back to the form.

Now, I just need to rebuild the logic to test it. In doing so, I think I wrote my mysql_real_escape_strings wrong, so it wasn't escaping properly.

Thanks to . and malarz for the tips and making me take another look at my own "recycled" code. I guess hand-coding a CMS, while still polishing up on a new language is a little more complex than I thought.
×

Success!

Help @OctoberWind spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...