Having trouble redirecting

I am creating a webpage for someone to enter their name and phone number. If that information is there, I want to redirect them to a different webpage. (this is on a usb stick and I only want one user)

This is the code that I am using, but it doesn’t seem to work:

<?php if($totalRows_basic > 0) header("Location:http://localhost/nameupdate.php"); } ?>

I have placed this at the top of the page before the connection statement, I have had it right below the connection statement, and at the end right before the html, so I don’t think that it is placement. If you can help me with the correct code, where should it be placed?

Thanks for any help.

first of all your code is missing {
[php]<?php
if($totalRows_basic > 0){
header(“Location:http://localhost/nameupdate.php”);
}
?>[/php]

second, maybe error message/whole code or more elaborate decription of what happens when you execute it would help us help you^^

Hi there,

If correcting the issue with the missing { then make sure that the statement is above any HTML (any space at all before the <?php tag or anything that gets echoed or printed before the header() function is called will make the header redirect impossible).

If the above cannot work because you have to output something before the header() redirect is called, then you can try implementing my redirect function which should work in any placement:
[php]/**

  • Redirects to the page specified in $location
  • @param string $location Where to redirect to
  • @param integer $delay Delay in seconds
    /
    function redirect($location,$delay=0)
    {
    if(headers_sent() === true && $delay <= 0)
    {
    header(“Location: “.$location);
    }
    else
    {
    echo $delay == 0 ? ‘’
    : ‘’;
    echo ‘’;
    }
    }
    [/php]

I tried putting in the missing { and it did not work. It stays on the same page.

<?php if($totalRows_basic_info > 0){ header("Location:http://localhost/nameupdate.php");}?>

So then I tried the other code. It did not work either. When that didn’t work, I tried adding my original statement to it, and still no results. It stays on the same page.

<?php if($totalRows_basic_info > 0){ header("Location:http://localhost/nameupdate.php");}?> <?php function redirect($location,$delay=0) { if(headers_sent() === true && $delay <= 0) { header("Location: ".$location); } else { echo $delay == 0 ? '' : ''; echo ''; } } ?>

Can anyone help me solve this problem?

Thanks

I dont know if it’s a good solution or not, but try redirecting with js

[php]echo “<script language=“JavaScript”>”;
echo “self.location.href=‘http://localhost/nameupdate.php’;”;
echo “”;[/php]

put this inside your php code in the spot where redirect should occur and tell if it helped.

The Java script created a 500 internal error. Any other ideas?

First, I’m afraid you haven’t used the code I posted properly, see the integration below:
[php]/**

  • Redirects to the page specified in $location
  • @param string $location Where to redirect to
  • @param integer $delay Delay in seconds
    /
    function redirect($location,$delay=0)
    {
    if(headers_sent() === true && $delay <= 0)
    {
    header(“Location: “.$location);
    }
    else
    {
    echo $delay == 0 ? ‘’
    : ‘’;
    echo ‘’;
    }
    }

if($totalRows_basic_info > 0)
{
redirect(“http://localhost/nameupdate.php”);
}[/php]

If that still doesn’t work, then $totalRows_basic_info is less than or equal to 0…

I copied your code and did a paste, it still did not work. I tried placing it before the connect, then below, at the beginning of the html and then at the end. No luck.

I double checked my basic_info table using phpmyadmin and there is information in that table. There is one row, which there should be.

Any other ideas?

After typing my last reply, I thought about what I typed. There should be just one row, so I used this

<?php if($totalRows_basic = 1){ header("Location:http://localhost/nameupdate.php");} ?> <?php Now it works. Thank you for making me think about what I really wanted and making it work.

Post whole code if you can, it will make it easier to pinpoint the issue

You may want to check it still works with this:
[php]<?php if($totalRows_basic == 1){[/php]

You need == for a comparison, a single = is setting $totalRows_basic to 1 in the if statement - this means it will always return true (unless the setting of the variable fails)

The =1 worked until I rebooted the computer, now it will not work. Can’t understand it.

I replaced your code again and retryed it and it still doesn’t work. I have double checked my phpmyadmin and there is information in basic_info and I can pull it up on another page that is echoing the information

I have copied most of my script I exceeded maxium limits, so I cut down on lists, and here is a copy of the entire page: I hope someone can help.

<?php require_once('../../Connections/emergency.php'); ?> <?php function redirect($location,$delay=0) { if(headers_sent() === true && $delay <= 0) { header("Location: ".$location); } else { echo $delay == 0 ? '' : ''; echo ''; } } if($totalRows_basic_info > 0) { redirect("http://localhost/nameupdate.php"); } ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO basic_info (primary_key, first_name, middle_int, last_name, dobmonth, dobday, dobyear, address1, address2, city, `state`, zip, home_phone, work_phone, cell_phone) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['hiddenField'], "int"), GetSQLValueString($_POST['first_name'], "text"), GetSQLValueString($_POST['middle_int'], "text"), GetSQLValueString($_POST['last_name'], "text"), GetSQLValueString($_POST['dobmonth'], "text"), GetSQLValueString($_POST['dobday'], "int"), GetSQLValueString($_POST['dobyear'], "int"), GetSQLValueString($_POST['address1'], "text"), GetSQLValueString($_POST['address2'], "text"), GetSQLValueString($_POST['city'], "text"), GetSQLValueString($_POST['state'], "text"), GetSQLValueString($_POST['zip'], "int"), GetSQLValueString($_POST['home_phone'], "text"), GetSQLValueString($_POST['work_phone'], "text"), GetSQLValueString($_POST['cell_phone'], "text")); mysql_select_db($database_emergency, $emergency); $Result1 = mysql_query($insertSQL, $emergency) or die(mysql_error()); $insertGoTo = "../namereturn.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } mysql_select_db($database_emergency, $emergency); $query_info = "SELECT * FROM basic_info"; $info = mysql_query($query_info, $emergency) or die(mysql_error()); $row_info = mysql_fetch_assoc($info); $totalRows_info = mysql_num_rows($info); ?> Untitled Document

My Medical Records Setup

 

My Information

Please enter the information for the person who will be using this Medical Record usb stick. You can only enter one person.

 
First Name
Middle Initial
Last Name
Date of Birth Month
January February March Please Choose One

Day
1 2 3 Please Choose One

Year
2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 Please Choose One

Address 1
Address 2
City
State
Zip
Home Phone (xxx-xxx-xxxx)
Work Phone (xxx-xxx-xxxx)
Cell Phone (xxx-xxx-xxxx)
 

 

 

<!-- end #mainContent --></div>

I would like the information contained on this device to be used in case of an emergency when I can not speak for myself. I agree that I have entered all this information. I will not hold anyone liable for the information contained on this device. I understand that if this device is lost or stolen the information could be viewed by an unauthorized person.

<?php mysql_free_result($info); ?>

I also tried

<?php if($totalRows_basic == 1){ header("Location:http://localhost/nameupdate.php");} ?>

And it still does not work. I just don’t understand that I got it to work several times before a reboot and now it won’t and I have rebooted several times just to make sure.

Sorry but sintax of header are correct, but we can’t know value of $totalRows_basic_info, if script doesn’t works it must be <=0.
Try to put [php]echo" VALUE : $totalRows_basic_info";[/php] and you will check this value.
When check that this value are <=0, check where are saved this value.

How is $totalRows_basic_info actually being set? and where?

I honestly think thats where your issue is as its not getting to this point redirect(“http://localhost/nameupdate.php”);

[php]if($totalRows_basic_info > 0)
{
redirect(“http://localhost/nameupdate.php”);
}
else
}
echo (‘totalRows_basic_info isnt working right. This is what its set as ( ‘.$totalRows_basic_info.’ )’);
}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service