Jump to content

[SOLVED] Redirect following SQL action?


msaz87

Recommended Posts

Just wanted to know if it were possible to run an SQL process like updating or dumping a table and then have a header command redirect the page? Or if not... what else could be used to substitute an auto-redirect?

 

Thanks for the help

 

mysql_query("UPDATE ibf_members SET skin='{$_POST['skin']}' WHERE id={$ibforums->member['id']}");
header("Location: index.php?act=UserCP&CODE=MOD");

 

use something like that..

Link to comment
Share on other sites

Yes it is possible, just use the code Gayner gave you.  Headers will work as long as there is no output displayed before them, so as long as there is not an error in your query then it should all go according to plan :)

 

Just wanted to know if it were possible to run an SQL process like updating or dumping a table and then have a header command redirect the page? Or if not... what else could be used to substitute an auto-redirect?

 

Thanks for the help

 

mysql_query("UPDATE ibf_members SET skin='{$_POST['skin']}' WHERE id={$ibforums->member['id']}");
header("Location: index.php?act=UserCP&CODE=MOD");

 

use something like that..

 

I'm getting a header error, but the top of my code is pulling variables, a few if statements, then:

 

<? include('../includes/db_connect.php'); ?>

<? include('../includes/header.php'); ?>

<?php

$delete			= $_REQUEST['delete'];

$date_mo		= $_REQUEST['league_date_mo'];
$date_day		= $_REQUEST['league_date_day'];
$date_year		= $_REQUEST['league_date_year'];

if($date_mo <10) $date_mo = "0".$date_mo;
if($date_day <10) $date_day = "0".$date_day;

$league			= $_REQUEST['league'];
$date			= $date_year."-".$date_mo."-".$date_day;
$week			= $_REQUEST['week'];
$location		= $_REQUEST['park'];
$notes			= $_REQUEST['notes'];

?>

<?php

if ($delete == "on") { 

?>

<?php

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 
//echo "Schedule ID: $schedule_id, Field: $fld, Time: $better_gametime, Team: $team, Value: $value<br />"; // IF YOU NEED TO SEE THE OUTPUT, UNCOMMENT THIS
   
mysql_query("	DELETE FROM schedules 
		WHERE schedule_id = '$schedule_id'") 
		or die(mysql_error());    

header("Location: index.php");
}

echo("The ".$date." schedule has been deleted.");

?>

 

There's some echo junk beneath it that was a placeholder while I was looking for a redirect solution, but if, like you said, the header command is above it, it should work?

 

Not sure what I'm doing wrong... thanks for your help.

Link to comment
Share on other sites

I tried turning the URL into a full one and still no dice... the complete page code:

 

<? include('../includes/db_connect.php'); ?>

<? include('../includes/header.php'); ?>

<?php

$delete			= $_REQUEST['delete'];

$date_mo		= $_REQUEST['league_date_mo'];
$date_day		= $_REQUEST['league_date_day'];
$date_year		= $_REQUEST['league_date_year'];

if($date_mo <10) $date_mo = "0".$date_mo;
if($date_day <10) $date_day = "0".$date_day;

$league			= $_REQUEST['league'];
$date			= $date_year."-".$date_mo."-".$date_day;
$week			= $_REQUEST['week'];
$location		= $_REQUEST['park'];
$notes			= $_REQUEST['notes'];

?>

<?php

if ($delete == "on") { 

?>

<?php

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 
//echo "Schedule ID: $schedule_id, Field: $fld, Time: $better_gametime, Team: $team, Value: $value<br />"; 
   
mysql_query("	DELETE FROM schedules 
		WHERE schedule_id = '$schedule_id'") 
		or die(mysql_error());    
}

//echo("The ".$date." schedule has been deleted.");

?>

<?php } else {

foreach($_POST['deletetime'] as $time => $times) {
list($delete_gametime,$t) = explode('|',$time);

$dgt	= strtotime($delete_gametime);
$d_time	= date("Hi",$dgt);

//echo("Times: $times, Delete_GT: $d_time<br/>");

$gt_delete_query = "	DELETE FROM schedules
			WHERE league_date	= '$date'
			AND league		= '$league'
			AND gametime		= '$d_time'";

mysql_query($gt_delete_query) 
or die(mysql_error());  	


}	

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 

//echo "Schedule ID: $schedule_id, Field: $fld, Game Time: $better_gametime, Team: $value, A or B: $team<br />"; 

$time_strip = strtotime($better_gametime);
$time_final	= date("Hi",$time_strip);

mysql_query("	UPDATE schedules SET 
		league_date	= '$date', 
		league		= '$league', 
		park_id		= '$location', 
		week		= '$week', 
		notes		= '$notes', 
		field		= '$fld', 
		gametime	= '$time_final', 
		team		= '$value', 
		a_or_b		= '$team' 
		WHERE schedule_id	= '$schedule_id'") 
		or die(mysql_error());     

		header("Location: http://[url]/index.php");
}

?>

<?php

echo("The ".$date." schedule has been edited.");

?>

<form action="../edit_schedule.php">

<input type="submit" value="return to previous page">

</form>

<?php } ?>

 

I've looked a few times and don't see any output beforehand... am I blind?

 

Thanks for the help...

Link to comment
Share on other sites

I'm not sure what the problem is...have you check the

 

db_connect.php

header.php

 

files to make sure there is no output in them...oh also make sure there are no spaces before the opening tags in both this document and the two you are including

 

 

whoops...nevermind I see it now...

 

<? include('../includes/db_connect.php'); ?>

<? include('../includes/header.php'); ?>

<?php

 

All of those blank spaces count as output...here try this:

 

<?php
include('../includes/db_connect.php');
include('../includes/header.php');

$delete			= $_REQUEST['delete'];

$date_mo		= $_REQUEST['league_date_mo'];
$date_day		= $_REQUEST['league_date_day'];
$date_year		= $_REQUEST['league_date_year'];

if($date_mo <10) $date_mo = "0".$date_mo;
if($date_day <10) $date_day = "0".$date_day;

$league			= $_REQUEST['league'];
$date			= $date_year."-".$date_mo."-".$date_day;
$week			= $_REQUEST['week'];
$location		= $_REQUEST['park'];
$notes			= $_REQUEST['notes'];



if ($delete == "on") { 

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 
//echo "Schedule ID: $schedule_id, Field: $fld, Time: $better_gametime, Team: $team, Value: $value<br />"; 
   
mysql_query("DELETE FROM schedules 
		WHERE schedule_id = '$schedule_id'") 
		or die(mysql_error());    
}

//echo("The ".$date." schedule has been deleted.");

} else {

foreach($_POST['deletetime'] as $time => $times) {
list($delete_gametime,$t) = explode('|',$time);

$dgt	= strtotime($delete_gametime);
$d_time	= date("Hi",$dgt);

//echo("Times: $times, Delete_GT: $d_time<br/>");

$gt_delete_query = "DELETE FROM schedules
			WHERE league_date	= '$date'
			AND league		= '$league'
			AND gametime		= '$d_time'";

mysql_query($gt_delete_query) 
or die(mysql_error());  	


}	

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 

//echo "Schedule ID: $schedule_id, Field: $fld, Game Time: $better_gametime, Team: $value, A or B: $team<br />"; 

$time_strip = strtotime($better_gametime);
$time_final	= date("Hi",$time_strip);

mysql_query("	UPDATE schedules SET 
		league_date	= '$date', 
		league		= '$league', 
		park_id		= '$location', 
		week		= '$week', 
		notes		= '$notes', 
		field		= '$fld', 
		gametime	= '$time_final', 
		team		= '$value', 
		a_or_b		= '$team' 
		WHERE schedule_id	= '$schedule_id'") 
		or die(mysql_error());     

		header("Location: http://[url]/index.php");
}


echo("The ".$date." schedule has been edited.");

?>

<form action="../edit_schedule.php">

<input type="submit" value="return to previous page">

</form>

<?php 
} 
?>

 

 

Link to comment
Share on other sites

You have to check if headers have been sent yet:

 

function redirect( $url ){
    if (! headers_sent( ) ){
    
        header( "Location: ".$url );
        exit( 0 );
    }
    echo "<script language=Javascript>document.location.href='".$url."';</script>";
    exit( 0 );
}

redirect('any url here you want');

Link to comment
Share on other sites

I went back and got rid of the includes, and all the spaces... and still errors...

 

<?php 	

$db_server	= "XXX";
$db_user	= "XXX";
$db_pass	= "XXX";
$db_name	= "XXX";

$schedule_db	= mysql_connect($db_server, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());

//include('../includes/db_connect.php');
//include('../includes/header.php');

$delete			= $_REQUEST['delete'];

$date_mo		= $_REQUEST['league_date_mo'];
$date_day		= $_REQUEST['league_date_day'];
$date_year		= $_REQUEST['league_date_year'];

if($date_mo <10) $date_mo = "0".$date_mo;
if($date_day <10) $date_day = "0".$date_day;

$league			= $_REQUEST['league'];
$date			= $date_year."-".$date_mo."-".$date_day;
$week			= $_REQUEST['week'];
$location		= $_REQUEST['park'];
$notes			= $_REQUEST['notes'];

if ($delete == "on") { 

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 
//echo "Schedule ID: $schedule_id, Field: $fld, Time: $better_gametime, Team: $team, Value: $value<br />"; // IF YOU NEED TO SEE THE OUTPUT, UNCOMMENT THIS
   
mysql_query("	DELETE FROM schedules 
		WHERE schedule_id = '$schedule_id'") 
		or die(mysql_error());    
}

//echo("The ".$date." schedule has been deleted.");

} else {

foreach($_POST['deletetime'] as $time => $times) {
list($delete_gametime,$t) = explode('|',$time);

$dgt	= strtotime($delete_gametime);
$d_time	= date("Hi",$dgt);

//echo("Times: $times, Delete_GT: $d_time<br/>");

$gt_delete_query = "	DELETE FROM schedules
			WHERE league_date	= '$date'
			AND league		= '$league'
			AND gametime		= '$d_time'";

mysql_query($gt_delete_query) 
or die(mysql_error());  	


}	

foreach($_POST['slot'] as $key => $value){
list($schedule_id,$fld,$better_gametime,$team) = explode('|',$key); 

//echo "Schedule ID: $schedule_id, Field: $fld, Game Time: $better_gametime, Team: $value, A or B: $team<br />"; // IF YOU NEED TO SEE THE OUTPUT, UNCOMMENT THIS

$time_strip = strtotime($better_gametime);
$time_final	= date("Hi",$time_strip);

mysql_query("	UPDATE schedules SET 
		league_date	= '$date', 
		league		= '$league', 
		park_id		= '$location', 
		week		= '$week', 
		notes		= '$notes', 
		field		= '$fld', 
		gametime	= '$time_final', 
		team		= '$value', 
		a_or_b		= '$team' 
		WHERE schedule_id	= '$schedule_id'") 
		or die(mysql_error());     

		header("Location: http://[url]/index.php");
}

} ?>

Link to comment
Share on other sites

Hmm... :shrug: just try doing what dreamwest said...replace the header with this:

 

if (!headers_sent()){
  header("Location: http://[url]/index.php");
        exit();
    }else{
    echo "<script language='Javascript'>document.location.href='http://[url]/index.php';</script>";
    exit();
}

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.