php-general Digest 13 Jan 2008 18:09:10 -0000 Issue 5233

Topics (messages 267164 through 267183):

Re: Any way to use header() or another function to force user to "top level"
        267164 by: Jim Lucas
        267165 by: Anup Shukla

Re: Guestbook
        267166 by: Børge Holen

Re: session_start problems with FireFox on Mac
        267167 by: Jochem Maas
        267171 by: Europus
        267174 by: Jochem Maas
        267176 by: Europus
        267178 by: Jochem Maas

Re: uh oh, I defined a resoruce
        267168 by: Jochem Maas
        267169 by: Naz Gassiep
        267172 by: David Giragosian
        267175 by: Nathan Nobbe
        267177 by: David Giragosian
        267179 by: Nathan Nobbe
        267181 by: Sancar Saran

Problem with sessions.
        267170 by: Balasubramanyam A
        267173 by: Jochem Maas

var_dump() results
        267180 by: Europus
        267182 by: T.Lensselink
        267183 by: T.Lensselink

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
Chuck wrote:
That is exactly what I am using now but the location I am redirecting
to is loading within the <div> tags and at the top level of the
browser.

-CC
On 1/12/08, Jim Lucas <[EMAIL PROTECTED]> wrote:
Chuck wrote:
I have some code doing some checks that sit inside div tags using href elements:
...
    <div class="pArea">
        <a class="panel" href="code.php?xxxx=yyyy" target="pframe1" >Panel1</a>

Wait, looking at your link again I see that you are directing the loading of this page request to a frame call "pframe1". If you are trying to redirect the iframe with the header() call, then no, their is no way to do what you are trying to do with the header() function. You will need to do this client side with javascript. something like this would do the job.

window.parent.location = '/';

I think that is the syntax.  If not, it is a good start.

...

In code.php, if various conditions aren't met, this script will do a
bunch of house cleaning, logging, then redirect using
header("Location: /some_url").

My problem is that /some_url comes up inside the div area, instead of
causing the browser to load /some_url as if accessed directly.  Im
just starting to dabble with PHP so I'm sure there is another way of
doing this or maybe an argument to header() itself.

I'm looking for the same behavior as the HTML snippet <a href="/"
target="_top">here</a>

Once I make the call to header(), I no longer care about any state
information, session variables, or anything. Basically I am booting
the user out of the application and back to the login/splash page. All
information I need to retain has already been logged to various
mechanisms prior to calling header() which is immediately followed by
exit();

Thanks for any help,
CC

Sounds like header() is what you are looking for then.

The one thing that you have to make sure is that you have not sent any
data to the browser.  Use this:

<?php

#
#Do whatever you want here.  Just don't output any data to the browser.
#

header('Location: http://www.example.com/');
exit;
# Always follow a header/location redirect with the "exit;" command.

?>

I have found that it is best to include the entire domain when using the
header/location redirect method.





--- End Message ---
--- Begin Message ---
Chuck wrote:
That is exactly what I am using now but the location I am redirecting
to is loading within the <div> tags and at the top level of the
browser.


Are you using AJAX to load the page?

--
Regards,
Anup Shukla

--- End Message ---
--- Begin Message ---
On Sunday 13 January 2008 01:32:53 Andrés Robinet wrote:
> Hi Guys,
>
> Anybody knows of a free and decent PHP Multilanguage guestbook (or at least
> supporting German and English)? I don't care if it is db-driven or uses
> flat files.
> I know how to write one, but it's for a website we didn't develop (but will
> now host) and I'm feeling a bit lazy... maybe someone has a hint.
> I don't care of how it looks, I can customize that, just want to write as
> less code as possible and avoid any security headaches as well (yeah... the
> good old lazy days :)).

jup feeling lazy alright. Good thing google works


>
> Thanks in advance,
>
> Rob
>
> ndrés Robinet | Lead Developer | BESTPLACE CORPORATION
> 5100 Bayview Drive 206, Royal Lauderdale Landings, Fort Lauderdale, FL
> 33308
>
> | TEL 954-607-4207 | FAX 954-337-2695
>
> Email: [EMAIL PROTECTED]  | MSN Chat: [EMAIL PROTECTED]  |  SKYPE:
> bestplace |  Web: http://www.bestplace.biz | Web: http://www.seo-diy.com



-- 
---
Børge Holen
http://www.arivene.net

--- End Message ---
--- Begin Message ---
Terry Calie schreef:

...


To illustrate my php goes like this:

index.php
=========
<?php
code
code
require "index2.php"
code
?>

index2.php
========
<?php
code
code
code
?>
<whitespace character>

...

to avoid this in future never add the closing php bracket to the end of
the php file (unless you explicitly want to output something after the
code in question - which is almost never the case), it is not required.

e.g.

--------- >8 info.php ----------------
<?php

phpinfo();
--------- >8 info.php ----------------

--- End Message ---
--- Begin Message ---
Jochem Maas wrote:

?>
<whitespace character>
...
to avoid this in future never add the closing php bracket to the end of
the php file (unless you explicitly want to output something after the
code in question - which is almost never the case), it is not required.

e.g.
--------- >8 info.php ----------------
<?php

phpinfo();
--------- >8 info.php ----------------

I didn't know that. Does the underlying engine in PHP provide the
otherwise missing tag? Does the described problem only happen with
FF on Mac or does it affect other browsers on other OSes?

Ulex

--- End Message ---
--- Begin Message ---
Europus schreef:
Jochem Maas wrote:

?>
<whitespace character>
...
to avoid this in future never add the closing php bracket to the end of
the php file (unless you explicitly want to output something after the
code in question - which is almost never the case), it is not required.

e.g.
--------- >8 info.php ----------------
<?php

phpinfo();
--------- >8 info.php ----------------

I didn't know that. Does the underlying engine in PHP provide the
otherwise missing tag?

the tag is not missing as such - the tag denotes the end of string that
php parses - it's quite logical to assume that the end of a file means the same
thing (especially when you consider that any file you include/require
must specify '<?php' to 'awaken' the php parser)

Does the described problem only happen with
FF on Mac or does it affect other browsers on other OSes?

I have never seen this problem effect just one browser on on platform -
afaic it's a general problem that effects all browsers - you can't send
out HTTP headers after HTTP content ... now it maybe that some browsers
these days see fit to ignore white space before the headers (or maybe
just ignore a "\n") but I doubt it ... honestly I can't see how php
would even send the headers after the "\n" had been output ... either
you error logging level is too low, your not reading the error log or you
have error suppression on the header() function (.e.g @header()). all
of these would give you the incorrect notion that the header function
was working, all of these are bad practice.

you also mentioned you changed the code when trying to test it on another
machine - don't do that, test one thing at a time.

anyway problem solved, preventative knowledge aquired, onto the next hurdle :-)


Ulex


--- End Message ---
--- Begin Message ---
 Jochem Maas wrote:

you also mentioned you changed the code when trying to test it on another
machine - don't do that, test one thing at a time.

I'm not the original poster, that wasn't me.

anyway problem solved, preventative knowledge aquired, onto the next hurdle :-)

I'm having some trouble with a for() loop and echoing array results
to screen to see if I'm getting the data I'm after, I may post about
it later.

Ulex

--- End Message ---
--- Begin Message ---
Europus schreef:
 Jochem Maas wrote:

you also mentioned you changed the code when trying to test it on another
machine - don't do that, test one thing at a time.

I'm not the original poster, that wasn't me.

anyway problem solved, preventative knowledge aquired, onto the next hurdle :-)

I'm having some trouble with a for() loop and echoing array results
to screen to see if I'm getting the data I'm after, I may post about
it later.

use var_dump() rather than echo.


Ulex


--- End Message ---
--- Begin Message ---
Sancar Saran schreef:
Well,

ADODB and TYPO3 are successfull oss procjecs which uses PHP and they utilizes globals at large.

windows is a successful OS but most people would agree it's built on a 
foundation
of cruft.

success != built with good code != globals are good practice.


Regards.

Sancar


--- End Message ---
--- Begin Message ---

While "global variables are good" in quotes did not return any results
from Google, "global variables are evil" in quotes only returned a result
set of 4:

Results *1* - *4* of *4* for *" 
global<http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=Gorx1ErPpjplcNDVa0qb6g&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNFszs4YfNg7ok3-m08IgyrhO5EAsQ>
variable<http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=vCOY79teeScIcrxuvb6k3g&q=http://www.answers.com/variable%26r%3D67&usg=AFQjCNEzQBZwfNnJ4pr1pnYOXYttJJNkvg>are
evil<http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=eh8-cXxXIwGuHRJhXG2REQ&q=http://www.answers.com/evil%26r%3D67&usg=AFQjCNHcgNJwcVKoYcSB_Ks5OMr4LnAXWw>
"*
**
Hardly an overwhelming argument by using search results as a measure.

David


Nevermind. I forgot the 's'.

Once again:

Personalized Results *1* - *8* of *8* for
*"global<http://www.google.com/url?sa=X&oi=dict&ei=joCGR9SjMp2wgATIv4HWBg&sig2=RNnAskxl87Jpwe85XDzyvg&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNFkKRqMEPNyoEfdOeatXZo5_51d5Q>
variables<http://www.google.com/url?sa=X&oi=dict&ei=joCGR9SjMp2wgATIv4HWBg&sig2=b_YKTOej3C0R3RD0QrZUwA&q=http://www.answers.com/variables%26r%3D67&usg=AFQjCNGXzEwYW9a5HoADskUH1aTT5OIvsg>are
good<http://www.google.com/url?sa=X&oi=dict&ei=joCGR9SjMp2wgATIv4HWBg&sig2=L-Je5IAp1doAPCeTovvqwA&q=http://www.answers.com/good%26r%3D67&usg=AFQjCNG9Bm-38XQxaR5w7aHA_cDvLT1nSw>
"*. (*0.17* seconds)

Personalized Results *1* - *10* of about *136* for
*"global<http://www.google.com/url?sa=X&oi=dict&ei=xYCGR-TiK4fcgASYpOi2Bg&sig2=ntdSUwxo_P-rplOA3PKNrw&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNE2teAhA638GVYsBxeBxb9z0iYi5A>
variables<http://www.google.com/url?sa=X&oi=dict&ei=xYCGR-TiK4fcgASYpOi2Bg&sig2=zCmKFYtnVoOQlWKrXcLqdw&q=http://www.answers.com/variables%26r%3D67&usg=AFQjCNEEdMRqxKMgkWXftVCWfE4kZr98jg>are
evil<http://www.google.com/url?sa=X&oi=dict&ei=xYCGR-TiK4fcgASYpOi2Bg&sig2=HxVB1-BREdrmjuSHo6E9jQ&q=http://www.answers.com/evil%26r%3D67&usg=AFQjCNErEUXMUFMcXbjo95hRT2t8pzkISg>
"*. (*0.07* seconds

Ok. So those of us who use globals are 17 time more likely to end up in
hell.

I can live with those odds.

David

God, I'm glad you're not in charge of decision-making on any issues that affect me :P
- Naz.

--- End Message ---
--- Begin Message ---
On 1/13/08, Naz Gassiep <[EMAIL PROTECTED]> wrote:
>
>
> >> While "global variables are good" in quotes did not return any results
> >> from Google, "global variables are evil" in quotes only returned a
> result
> >> set of 4:
> >>
> >> Results *1* - *4* of *4* for *" global<
> http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=Gorx1ErPpjplcNDVa0qb6g&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNFszs4YfNg7ok3-m08IgyrhO5EAsQ
> >
> >> variable<
> http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=vCOY79teeScIcrxuvb6k3g&q=http://www.answers.com/variable%26r%3D67&usg=AFQjCNEzQBZwfNnJ4pr1pnYOXYttJJNkvg
> >are
> >> evil<
> http://www.google.com/url?sa=X&oi=dict&ei=-nyGR539MKjIgQT1hKC7Bg&sig2=eh8-cXxXIwGuHRJhXG2REQ&q=http://www.answers.com/evil%26r%3D67&usg=AFQjCNHcgNJwcVKoYcSB_Ks5OMr4LnAXWw
> >
> >> "*
> >> **
> >> Hardly an overwhelming argument by using search results as a measure.
> >>
> >> David
> >>
> >>
> >
> > Nevermind. I forgot the 's'.
> >
> > Once again:
> >
> > Personalized Results *1* - *8* of *8* for
> > *"global<
> http://www.google.com/url?sa=X&oi=dict&ei=joCGR9SjMp2wgATIv4HWBg&sig2=RNnAskxl87Jpwe85XDzyvg&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNFkKRqMEPNyoEfdOeatXZo5_51d5Q
> >
> > variables<
> http://www.google.com/url?sa=X&oi=dict&ei=joCGR9SjMp2wgATIv4HWBg&sig2=b_YKTOej3C0R3RD0QrZUwA&q=http://www.answers.com/variables%26r%3D67&usg=AFQjCNGXzEwYW9a5HoADskUH1aTT5OIvsg
> >are
> > good<
> http://www.google.com/url?sa=X&oi=dict&ei=joCGR9SjMp2wgATIv4HWBg&sig2=L-Je5IAp1doAPCeTovvqwA&q=http://www.answers.com/good%26r%3D67&usg=AFQjCNG9Bm-38XQxaR5w7aHA_cDvLT1nSw
> >
> > "*. (*0.17* seconds)
> >
> > Personalized Results *1* - *10* of about *136* for
> > *"global<
> http://www.google.com/url?sa=X&oi=dict&ei=xYCGR-TiK4fcgASYpOi2Bg&sig2=ntdSUwxo_P-rplOA3PKNrw&q=http://www.answers.com/global%26r%3D67&usg=AFQjCNE2teAhA638GVYsBxeBxb9z0iYi5A
> >
> > variables<
> http://www.google.com/url?sa=X&oi=dict&ei=xYCGR-TiK4fcgASYpOi2Bg&sig2=zCmKFYtnVoOQlWKrXcLqdw&q=http://www.answers.com/variables%26r%3D67&usg=AFQjCNEEdMRqxKMgkWXftVCWfE4kZr98jg
> >are
> > evil<
> http://www.google.com/url?sa=X&oi=dict&ei=xYCGR-TiK4fcgASYpOi2Bg&sig2=HxVB1-BREdrmjuSHo6E9jQ&q=http://www.answers.com/evil%26r%3D67&usg=AFQjCNErEUXMUFMcXbjo95hRT2t8pzkISg
> >
> > "*. (*0.07* seconds
> >
> > Ok. So those of us who use globals are 17 time more likely to end up in
> > hell.
> >
> > I can live with those odds.
> >
> > David
>
> God, I'm glad you're not in charge of decision-making on any issues that
> affect me :P
> - Naz.
>

How kind of you to clarify your position, Naz. ;~)

David

--- End Message ---
--- Begin Message ---
the 2 problems that globals introduce are

   1. namespace collisions
   2. point of origin problem

where a  namespace collision is the inconsistent use of a global
variable within an application
eg.
// one point in the application
$GLOBALS['curUser'] = array('username' => 'bob', 'userId' => 5);

// another point in the application
$GLOBALS['curUser'] = new stdClass();
$GLOBALS['curUser']->username = 'bob';
$GLOBALS['curUser']->userId = 5;

and what im calling the 'point of origin' problem, is the ambiguity
introduced by global variables.  when reading through the code of an
application and you encounter the use of a global variable, where was
that variable set?  if not in the same file, then youll have to start
looking.  you may not even be able to determine where the value is
getting set by looking through the code, that is, you might have to analyze
the runtime behavior of the application.  and with the $GLOBALS array, this
can be cumbersome because there is no way (that i know of) to add implicit
logging to setting of values within.

now, whether you or your team or business or w/e will have to deal with
these
problems depends on what sort of conventions, if any, are used to work with
globals (or whether theyre used at all ;)).

to make them work in a team environment you should have some external
documentation
that establishes clear usage guidelines and outlines the purpose (or
declares 'namespaces')
within the $GLOBALS array.  so you might say "$GLOBALS['user'] is designated
to contain
user data, which will be a record from the users table in our database".  it
will be an array.
at that point if someone uses $GLOBALS['user'] for something else, or
changes its data type
or something, you can say, well youve violated our convention here so youll
have to clean that up.

there is the issue of working with code from other parties and this is where
things
can still get messy.  say that you have your conventions and everything is
hunky-dory; then you
decide to use some third party code, and it uses one of your global
'namespaces'.
so for example you get some third party code and within it, for whatever
reason $GLOBALS['user']
(referenced earlier) is being read or set.  well if you want to use that
code, youll have to refactor
either yours, or theirs.

object oriented development helps with some of these problems, but it is not
by itself a silver bullet.
for example with singleton
http://www.phppatterns.com/doku.php/design/singleton_pattern
problems 1, and 2 above are still relevant.  php is introducing namespaces
soon enough, but prior
to those class name collisions are generally resolved in php by prefixing a
class name
eg.
class User {}
becomes
class MyBiz__User {}

problem 2 though is relevant depending on what the singleton is used for.
suppose it only contains
2 values, a reference to itself and one datum, which is set in the private
constructor (or another private
method invoked as part of the constructor).  this is the most trivial case
and it is not really subject to
the problem.  however, if a singleton were to have getter and setter methods
problem number 2 above
can occur.  the good news here is that since singleton is a class you write,
adding logging is trivial
so you should be able to track down those spurious cases where a value is
set in one portion of the
application and read in another.

i happen to know that facebook uses globals, and i just thought i would
mention it as a 'successful'
yet perhaps 'code not so great' example of a large application using
globals.

if you are to use globals, then establish conventions.
the worst stuff i have ever seen is use of other superglobal arrays for the
same purpose as what the
$GLOBALS array is intended to be.
eg.
// one part of the application
// NOTE: this value did not orginate from the request
$_POST['someVal'] = 5;

// later in the application
$myVar = $_POST['someVal'];

that is just plain bad if you ask me (and they didnt even put the note!).

-nathan

--- End Message ---
--- Begin Message ---
On 1/13/08, Nathan Nobbe <[EMAIL PROTECTED]> wrote:
>
> the 2 problems that globals introduce are
>
>    1. namespace collisions
>    2. point of origin problem
>
> where a  namespace collision is the inconsistent use of a global
> variable within an application
> eg.
> // one point in the application
> $GLOBALS['curUser'] = array('username' => 'bob', 'userId' => 5);
>
> // another point in the application
> $GLOBALS['curUser'] = new stdClass();
> $GLOBALS['curUser']->username = 'bob';
> $GLOBALS['curUser']->userId = 5;
>
> and what im calling the 'point of origin' problem, is the ambiguity
> introduced by global variables.  when reading through the code of an
> application and you encounter the use of a global variable, where was
> that variable set?  if not in the same file, then youll have to start
> looking.  you may not even be able to determine where the value is
> getting set by looking through the code, that is, you might have to
> analyze
> the runtime behavior of the application.  and with the $GLOBALS array,
> this
> can be cumbersome because there is no way (that i know of) to add implicit
> logging to setting of values within.
>
> now, whether you or your team or business or w/e will have to deal with
> these
> problems depends on what sort of conventions, if any, are used to work
> with
> globals (or whether theyre used at all ;)).
>
> to make them work in a team environment you should have some external
> documentation
> that establishes clear usage guidelines and outlines the purpose (or
> declares 'namespaces')
> within the $GLOBALS array.  so you might say "$GLOBALS['user'] is
> designated to contain
> user data, which will be a record from the users table in our database".
> it will be an array.
> at that point if someone uses $GLOBALS['user'] for something else, or
> changes its data type
> or something, you can say, well youve violated our convention here so
> youll have to clean that up.
>
> there is the issue of working with code from other parties and this is
> where things
> can still get messy.  say that you have your conventions and everything is
> hunky-dory; then you
> decide to use some third party code, and it uses one of your global
> 'namespaces'.
> so for example you get some third party code and within it, for whatever
> reason $GLOBALS['user']
> (referenced earlier) is being read or set.  well if you want to use that
> code, youll have to refactor
> either yours, or theirs.
>
> object oriented development helps with some of these problems, but it is
> not by itself a silver bullet.
> for example with singleton
> http://www.phppatterns.com/doku.php/design/singleton_pattern
>
> problems 1, and 2 above are still relevant.  php is introducing namespaces
> soon enough, but prior
> to those class name collisions are generally resolved in php by prefixing
> a class name
> eg.
> class User {}
> becomes
> class MyBiz__User {}
>
> problem 2 though is relevant depending on what the singleton is used for.
> suppose it only contains
> 2 values, a reference to itself and one datum, which is set in the private
> constructor (or another private
> method invoked as part of the constructor).  this is the most trivial case
> and it is not really subject to
> the problem.  however, if a singleton were to have getter and setter
> methods problem number 2 above
> can occur.  the good news here is that since singleton is a class you
> write, adding logging is trivial
> so you should be able to track down those spurious cases where a value is
> set in one portion of the
> application and read in another.
>
> i happen to know that facebook uses globals, and i just thought i would
> mention it as a 'successful'
> yet perhaps 'code not so great' example of a large application using
> globals.
>
> if you are to use globals, then establish conventions.
> the worst stuff i have ever seen is use of other superglobal arrays for
> the same purpose as what the
> $GLOBALS array is intended to be.
> eg.
> // one part of the application
> // NOTE: this value did not orginate from the request
> $_POST['someVal'] = 5;
>
> // later in the application
> $myVar = $_POST['someVal'];
>
> that is just plain bad if you ask me (and they didnt even put the note!).
>
> -nathan
>


A very cogent and detailed assessment of the issues, Nathan, and I
appreciate the time and thoughtfulness involved in writing your response.

My stance concerning globals is that, on PHP projects anyway, I work alone,
use longish and very descriptive variable names, and most always only use
globals for static data needed for calculations of some kind. But I
certainly appreciate that in a team environment and when working on very
large projects, the odds of globals usage, especially without documentation
and adherence to a standard, can lead to the problems described above.

In general, I agree with Stut's response delineated earlier in this thread,
that a careful and circumscribed use of globals has a place in PHP
programming.

David

--- End Message ---
--- Begin Message ---
On Jan 13, 2008 12:14 PM, David Giragosian <[EMAIL PROTECTED]> wrote:

> In general, I agree with Stut's response delineated earlier in this
> thread,
> that a careful and circumscribed use of globals has a place in PHP
> programming.


agreed.

-nathan

--- End Message ---
--- Begin Message ---
On Sunday 13 January 2008 16:53:42 Jochem Maas wrote:
> Sancar Saran schreef:
> > Well,
> >
> > ADODB and TYPO3 are  successfull oss procjecs which uses PHP and they
> > utilizes globals at large.
>
> windows is a successful OS but most people would agree it's built on a
> foundation of cruft.
>
> success != built with good code != globals are good practice.
>
> > Regards.
> >
> > Sancar

Hello there, 

After some research, I found source of the "Globals are evil".

This was coming from common c and c++ practice. In this enviroments using 
globals leding to race conditions which was very hard to resolve.

and uh oh, that php thing was using share nothing perspective. No race 
condtions. except variable crashing.

From my point of view. After 8 years of php programming only one time face the 
variable crashing. And that was a local variable called $i.

Of course your millage was vary...

Regards

Sancar.

--- End Message ---
--- Begin Message ---
Could someone help with this code? It is a search and delete program. I'm
executing bunch of code within if-else statement. After searching the
records in else block the code is redirecting to if block. I'm not able
delete the records in else block. How do avoid the control move to if block?


<?php
session_start();
require_once('connection.php');
$_SESSION['voucher_value'] =  $_POST['voucher_to'];
if(!($_SESSION['voucher_value'] == b1) )
{
$tbl_name = "voucher";
$kodaikanal = "Resident Scientist <br /> IIA, Kodaikanal <br /> Observatory
<br /> KODAIKANAL - 624 103";
$hosakote = "Scientist in-charge <br /> CREST Campus <br /> HOSAKOTE - 562
114";
$leh_ladakh = "Engineer in-charge <br /> IAO-HANLE <br />Fort Road.SKARA <br
/>P.O.Box No.100<br />LEH-LADAKH - 194 101 <br /> (J & K)";
$kavalur = "Resident Scientist <br /> Vainu Bappu Observatory <br /> KAVALUR
- 635 701";

$_SESSION['voucher_value'] =  $_POST['voucher_to'];
if($_SESSION['voucher_value'] == k1)
{
$add_val =  $kodaikanal;
}
elseif($_SESSION['voucher_value'] == h1)
{
$add_val =  $hosakote;
}
elseif($_SESSION['voucher_value'] == l1)
{
$add_val =  $leh_ladakh;
}
elseif($_SESSION['voucher_value'] == k2)
{
$add_val =  $kavalur;
}

if ( !(isset ($_POST['delete']) ) ) {
$_SESSION['name_voucher']= $_POST['search_name'];
}
$search_temp = $_SESSION['name_voucher'];


if(isset($_POST['delete'])){
//$checkboxID= $_POST['checkbox'];
$array= $_POST['checkbox'];
foreach ($array as $key=>$value) {
$sql1 = "DELETE FROM $tbl_name WHERE voucher_number='$array[$key]'";
$result1 = mysql_query($sql1);
}


}
$sql="SELECT * FROM $tbl_name where name='$search_temp' &&
address='$add_val'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if ($count == 0)
{
include('header.php');
print "<br />";
print "<br />";
print "Search does not find any such records";
print "<br />";
exit();
include('footer.php');
}
include('ser_mis.php');
}




else
{

$tbl_name = "voucherb";
if ( !(isset ($_POST['delete']) ) ) {
$_SESSION['name_voucher']= $_POST['search_name'];
}
$search_temp = $_SESSION['name_voucher'];


if(isset($_POST['delete'])){
//$checkboxID= $_POST['checkbox'];
$array= $_POST['checkbox'];
foreach ($array as $key=>$value) {
$sql1 = "DELETE FROM $tbl_name WHERE voucher_number='$array[$key]'";
$result1 = mysql_query($sql1);
}
if(!(mysql_affected_rows() == 1))
{
include('header.php');
print "<br />";
print "<br />";
print "Records has not been deleted. Please contact administrator";
include('footer.php');
exit();
}else
{
include('header.php');
print "<br />";
print "<br />";
print "The record(s) has been successfully deleted from database";
print "<br />";
exit();
include('footer.php');
}
}
$sql="SELECT * FROM $tbl_name where name='$search_temp'";// &&
address='$add_val'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

/*if ($count == 0)
{
include('header.php');
print "<br />";
print "<br />";
print "Search does not find any such records";
print "<br />";
exit();
include('footer.php');
}*/
include('ser_misb.php');
}

?>

--- End Message ---
--- Begin Message ---
Balasubramanyam A schreef:
Could someone help with this code? It is a search and delete program. I'm
executing bunch of code within if-else statement. After searching the
records in else block the code is redirecting to if block. I'm not able
delete the records in else block. How do avoid the control move to if block?

1. your problem description is pretty useless - apart form being seemingly
incoherent you also mention an 'if block' and an 'else block' in such a way as
to imply that there is only one of each (there are plenty of each in the code 
provided).

2. your code is not properly indented at all which makes reading it
and trying to figure out what it does (and what it is supposed to do)
too much bother.

3. your doing alsorts of things wrong aside from any flawed logic that
the script apparently contains.

e.g.

1. why set the following session value unconditionally each time?
why not just use the POST value?

$_SESSION['voucher_value'] =  $_POST['voucher_to'];


2. I assume that k1 is not a constant you have defined elsewhere...

if($_SESSION['voucher_value'] == k1)


3. you have big SQL injection holes in the script, also you make blind
assumptions about incoming request data:

$array= $_POST['checkbox'];
foreach ($array as $key=>$value) {
$sql1 = "DELETE FROM $tbl_name WHERE voucher_number='$array[$key]'";



<?php
session_start();
require_once('connection.php');
$_SESSION['voucher_value'] =  $_POST['voucher_to'];
if(!($_SESSION['voucher_value'] == b1) )
{
$tbl_name = "voucher";
$kodaikanal = "Resident Scientist <br /> IIA, Kodaikanal <br /> Observatory
<br /> KODAIKANAL - 624 103";
$hosakote = "Scientist in-charge <br /> CREST Campus <br /> HOSAKOTE - 562
114";
$leh_ladakh = "Engineer in-charge <br /> IAO-HANLE <br />Fort Road.SKARA <br
/>P.O.Box No.100<br />LEH-LADAKH - 194 101 <br /> (J & K)";
$kavalur = "Resident Scientist <br /> Vainu Bappu Observatory <br /> KAVALUR
- 635 701";

$_SESSION['voucher_value'] =  $_POST['voucher_to'];
if($_SESSION['voucher_value'] == k1)
{
$add_val =  $kodaikanal;
}
elseif($_SESSION['voucher_value'] == h1)
{
$add_val =  $hosakote;
}
elseif($_SESSION['voucher_value'] == l1)
{
$add_val =  $leh_ladakh;
}
elseif($_SESSION['voucher_value'] == k2)
{
$add_val =  $kavalur;
}

if ( !(isset ($_POST['delete']) ) ) {
$_SESSION['name_voucher']= $_POST['search_name'];
}
$search_temp = $_SESSION['name_voucher'];


if(isset($_POST['delete'])){
//$checkboxID= $_POST['checkbox'];
$array= $_POST['checkbox'];
foreach ($array as $key=>$value) {
$sql1 = "DELETE FROM $tbl_name WHERE voucher_number='$array[$key]'";
$result1 = mysql_query($sql1);
}


}
$sql="SELECT * FROM $tbl_name where name='$search_temp' &&
address='$add_val'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if ($count == 0)
{
include('header.php');
print "<br />";
print "<br />";
print "Search does not find any such records";
print "<br />";
exit();
include('footer.php');
}
include('ser_mis.php');
}




else
{

$tbl_name = "voucherb";
if ( !(isset ($_POST['delete']) ) ) {
$_SESSION['name_voucher']= $_POST['search_name'];
}
$search_temp = $_SESSION['name_voucher'];


if(isset($_POST['delete'])){
//$checkboxID= $_POST['checkbox'];
$array= $_POST['checkbox'];
foreach ($array as $key=>$value) {
$sql1 = "DELETE FROM $tbl_name WHERE voucher_number='$array[$key]'";
$result1 = mysql_query($sql1);
}
if(!(mysql_affected_rows() == 1))
{
include('header.php');
print "<br />";
print "<br />";
print "Records has not been deleted. Please contact administrator";
include('footer.php');
exit();
}else
{
include('header.php');
print "<br />";
print "<br />";
print "The record(s) has been successfully deleted from database";
print "<br />";
exit();
include('footer.php');
}
}
$sql="SELECT * FROM $tbl_name where name='$search_temp'";// &&
address='$add_val'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

/*if ($count == 0)
{
include('header.php');
print "<br />";
print "<br />";
print "Search does not find any such records";
print "<br />";
exit();
include('footer.php');
}*/
include('ser_misb.php');
}

?>


--- End Message ---
--- Begin Message ---
It's pretty much the same. With var_dump(), values from the first row
of the table are iterated twice, the script is not looping through and
reporting all 2100 rows. Grossly similar behavior was observed with
print_r() and echo: while different data was reported by each, the
loop wouldn't loop. The first row was reported once (or twice) and
that was the end of that. I've tried foreach() also, same-same. It
doesn't loop, it just reports the first row.

Here's my code:

<?php

//connect to db
$link = mysql_pconnect('$host', '$login', '$passwd');
if (!$link) {
    die('Unable to connect : ' . mysql_error());
}
// make $table the current db
$db_selected = mysql_select_db('$table', $link);
if (!$db_selected) {
    die ('Unable to select $table : ' . mysql_error());
}

//get column data
        $sql    = "SELECT column2, column3 FROM $table";
        $result = mysql_query($sql);
        $row = mysql_fetch_row($result);

//loop through to display results
for($i=0; $i < count($row); $i++){
        var_dump($row);
        echo "<br /><br />";
?>

Ulex

--- End Message ---
--- Begin Message ---
Europus wrote:
It's pretty much the same. With var_dump(), values from the first row
of the table are iterated twice, the script is not looping through and
reporting all 2100 rows. Grossly similar behavior was observed with
print_r() and echo: while different data was reported by each, the
loop wouldn't loop. The first row was reported once (or twice) and
that was the end of that. I've tried foreach() also, same-same. It
doesn't loop, it just reports the first row.

It doesn't loop through all rows because you use mysql_fetch_row. Wich does exactly what it's name tells you. It fetches one single row. So instead of looping through the whole result set. You are looping through every cell off the result set. You're probably
better of using mysql_fetch_array().

Here's my code:

<?php

//connect to db
$link = mysql_pconnect('$host', '$login', '$passwd');
if (!$link) {
    die('Unable to connect : ' . mysql_error());
}
// make $table the current db
$db_selected = mysql_select_db('$table', $link);
if (!$db_selected) {
    die ('Unable to select $table : ' . mysql_error());
}

//get column data
    $sql    = "SELECT column2, column3 FROM $table";
    $result = mysql_query($sql);
    $row = mysql_fetch_row($result);

//loop through to display results
for($i=0; $i < count($row); $i++){
    var_dump($row);
    echo "<br /><br />";
?>

Ulex


--- End Message ---
--- Begin Message ---
Europus wrote:
T.Lensselink wrote:

It doesn't loop through all rows because you use mysql_fetch_row. Wich does exactly what it's name tells you. It fetches one single row.

Doh! Yes I'm brand new at PHP, though I've been doing HTML/CSS forever.
That doesn't seem to mean much to a PHP/MySQL installation.
Everybody has to start somewhere :)

So instead of looping through the
whole result set. You are looping through every cell off the result set. You're probably better of using mysql_fetch_array().

I made that change, now it reports the 1st row 4 times and never
gets on to the 2nd or subsequent rows. What the ???
Can you show some code?

Ulex




--- End Message ---

Reply via email to