Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

If Recordset not empty redirect

New Here ,
Jul 19, 2011 Jul 19, 2011

I am trying to find a code that will work for a redirect.  I want to check a recordset and if there is information there, I need to redirect them to a different page.  I have tried this code

<?php

if($totalRows_basic_info > 0) {

header ("Location:http://localhost/nameupdate.php");}

?>

This code is not working, can anyone help me with a recordset not empty redirect statement?

TOPICS
Server side applications
2.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jul 19, 2011 Jul 19, 2011

I learned it out of an David Powers Book.

You have to change the example to your needs.

Best regards

<?php if (!empty($row_rsActueel['kortetekst'])) { ?>

<?php echo nl2br($row_rsActueel['kortetekst']); ?>

<?php } ?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 19, 2011 Jul 19, 2011

You might want to try changing the > to ==

or you could

<?php

if(empty($totalRows_basic_info)) {

header ("Location:http://localhost/nameupdate.php");}

?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2011 Jul 19, 2011

Gary would that be if the recordset is empty?  I need to redirect if the recordset is not empty.  If they have already entered information once, I don't want them to enter again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 19, 2011 Jul 19, 2011

If you want to ask if the recordset is empty, you use

if(empty())

If you want to use if the recordset is NOT emptly, you use

if(!empty())

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2011 Jul 19, 2011

Gary thanks for your quick reply.  I inserted the code and it is redirecting it to the new nameupdate form, however, when I emptied out the table I want it to stay on the nameform page and it is not, it is going to the nameupdate form.  It is as if it is not checking the database to see if there is something there, or do I need to add an else statement to make it stay there?  If so how would I go about adding an else statement, I have played around with it before witht he other code and did not get any results.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 19, 2011 Jul 19, 2011

I think you would need to post all of your code for someone to give you a good answer. I'm sure someone would be able to offer a solution once they see all the code.

Gary

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2011 Jul 19, 2011

If the recordset is empty I want them to stay on this page (nameform).  If the record set is not empty I want them to go to nameupdate.

Currently the recordset has something in it and it is being redirected to the correct page, but when I empty the table out, it is still being redirected to the nameupdate page instead of staying on the nameform page.  Here is my code, I have edited it because the code was to long.  This is the code till you get to the HTML.  .  I hope someone can help.  Thanks in advance.

<?php require_once('../Connections/emergency.php'); ?>
<?php
if(empty($totalRows_basic_info)) {
header ("Location: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);
?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 19, 2011 Jul 19, 2011

I'm fairly confused.

The script that you have posted, am I to assume it is on nameupdate.php?   And you are redirecting the visitor to back the same page if information is not submitted or present in the DB?

If that is the case, that is the long way around the barn.  Perhaps you can clear up the confusion by walking us through the process or your logic.

Gary

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 19, 2011 Jul 19, 2011

Gary,

The script posted is on the initial data entry page, it is on nameform. This table should be empty when they see this form.   If this table is not empty, I want them to go to the nameupdate form.  I only want one user.  This is going on a USB stick with information about only that person on the stick, so I want them entering their information only once.  The only way I thought that I could accomplish this is to let them see the initial data entry page only one time.  I am new to programming so if my thought process is incorrect, how would you go about it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 20, 2011 Jul 20, 2011

My initial confusion to your last post was that I thought you were taking the person back to the same form again if something was empty, which would have been a weird way to do that...

So lets forget my questions as to how your going to store the data on a memory stick and I will assume the MS will have the form and the ability to connect to the database.

To determine if this person exists in your database you will need to have some unique identifier.  If this is for employment or dealing with employees you may be able to use their social security number (although there may be legal issues involved with that that I am unaware of).

If you do not have a unique ID, then you run the danger of corrupting your data (2 Tom Smiths that live in Houston....go figure).

So lets assume you have this hurdle behind you. So if there are two different forms to be completed, one being for a new entry and a second for an update, you can do it on one file, or two.  Off the top of my head I am not seeing a big  advantage/disadvantage to either way.

So he basic logic would be:

1 Does this person exist?

a. No, show this form, then process this form

b. Yes, show the next form, process that form.

So to put a little finer point to it, it might look like this:

<?php require_once('../Connections/emergency.php'); ?>
<?php
if(empty($totalRows_basic_info)) {

?>

<form id="new">...</form>

<?php

}else{
?>

<form id="notnew">...</form>

<?php

}

?>

For new would have an INSERT command and form notnew would have an UPDATE.

If there is just one form then you would simply run an INSERT query if the table is empty and run an UPDATE queary if it is not.

That would look something like this

<?php require_once('../Connections/emergency.php'); ?>
<?php
if(empty($totalRows_basic_info)) {

$query1="INSERT"

}else{

$query2="UPDATE"

}

?>

Someone else may have a different / better suggestion, but that is how I would approach it.

HTH

Gary

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 20, 2011 Jul 20, 2011

I still can not get it to work.  I can get the database to echo just fine, but when I do anything to check to see if the recordset is empty, I get either the same page, or 500 error, or a 404 error, just depending on which suggestion that I have used in this post.

I tried the suggestion of just using one form, but it gave me a 500 error.

Could this possibly be the portable server that I am using and not the coding?

I am currently using Litepxp.  I tried server2go and dreamweaver will absolutely not connect.  Server2go also does not like header location statements.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 20, 2011 Jul 20, 2011

This is a new problem?

Are all of the files loaded to your local / portable server?  Is the connection to the local / portable server? I am not familiar with Litepxp, is this a server that is contained on the memory stick?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 21, 2011 Jul 21, 2011

The server is contained on the USB stick.  I don't think that it is a new problem, It is just that I can't get the scripts to work, and thought that maybe it could be a server problem.  As far as anything else I have asked the server to do, it has preformed well.  I am working off of an older version, I had downloaded the new version and it freezes the browser.  So went back to the old version which has worked very well.

As far as the script goes if I use >0 it will stay on the same page.  If I use ==1 then it will redirect to the other page.  When using ==1 it does not matter if the table is empty or not, it just redirects to the other page.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 21, 2011 Jul 21, 2011

Also if I use if(empty($totalRows_basic_info)) { alone it stays on the same page.  If I add anything else to the script like the else statements thats when I get the error page or nothing at all depending one which one I use.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 21, 2011 Jul 21, 2011

If the script is working with the operator >, ( and I have my doubts that it is) then I suggest your try

>= 1

Here is a list of operators that might help you/

==is equal to5==8 returns false
!=is not equal5!=8 returns true
<>is not equal5<>8 returns true
>is greater than5>8 returns false
<is less than5<8 returns true
>=is greater than or equal to5>=8 returns false
<=is less than or equal to

See if that helps

Gary

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 24, 2011 Jul 24, 2011

I am still having problems with this redirect script.  I had someone ask me about suggest

<?php

$totalRows_basic_info = xxxx;

if (trim($totalRows_basic_info) <> '')

if(empty($totalRows_basic_info))

header ("Location:http://localhost/nameupdate.php");}

It still did not work.  I played around with Gary's suggestions about the == ,=>, =< and so on and did not get the results that I wanted.  I did discover however, depending on which one I use It stays on nameform or will move to nameupdate.  So I got to thinking if it moves to nameupdate, should an else statement be added to say do nothing if $totalRows_basic_info>0 go to nameupdate, else do nothing.

Have any suggestions on how to write this.  I tried

<?php

if(empty($totalRows_basic_info))

header ("Location:http://localhost/nameupdate.php");

else {

}

?>

at the end of my statment and that did not work.  Am I on the right track with my thinking?

Do I need to add

$totalRows_basic_info=xxxx;

to my command.  When I add it right now it does nothing to the results.

Thanks for any help

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 25, 2011 Jul 25, 2011

In this script, the closing bracket would need to be at the bottom of the page, like so

<?php

if(empty($totalRows_basic_info))

header ("Location:http://localhost/nameupdate.php");

else {

?>

[all of your code for the form]

<?php } ?>

</body>

<.html?

The way you displayed it then nothing happens if the condition is not met.

So does everything else work on the page?

Gary

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 26, 2011 Jul 26, 2011
LATEST

I decided to try another portable server just to see what kind of results that I get.  This new usb server does not like the header statement.  Does anyone have any ideas besides PHP to check the recordset and then redirect.  I have read that javascript can redirect, but can't find any tutorials or examples on how to write an if statement to check a database.

Can Java script do this?  I have not written any javascript before and really have no idea on how to do it, as I decided to tackle learning only one thing at a time and right now it is php.   I need something that will check to see if $totalRows_basic_info is empty and if it is go to nameupdate.php.  If it is not empty then stay on nameform and complete the form1.

I have been working on this problem for over 3 weeks now and am very frustrated that I can't get anything to work.  That is why I think that it may be a server problem.  (I tried the usb stick on other computers and it would not come up on three out of 5 computers, so I do think that the old server had some quirks.  Now the new server will not take header statements, so I thought maybe  javascript might be the answer.  I have been looking online for about 3 days for tutorials or examples in javascript on how to do this but have come up empty handed.

Any help is greatly appreciated.  Thanks everyone for taking the time to try and help me solve this problem especially Gary.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines