By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,319 Members | 2,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,319 IT Pros & Developers. It's quick & easy.

Help. php reading before it should

100+
P: 142
Hi,

My script lets you choose if you want to view table 1 or table 2 in mysql after you selected 1 or 2 from the drop down menu and press submit.

The problem is if you run the script for the very first time it gives an error at the query. cause no one has told the code where to go and look.

How can I make it so it does not run the script before you click submit or have it select and display the first table until your select?

Here my script I have marked the section that is giving the problem.. with *********my text*******************

Thanks


[PHP]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>view</title>
</head>

<body>
<?PHP

$self = $_SERVER['PHP_SELF'];
$table = $_POST['table'];


?>
<form action="<?php echo( $self ); ?>" method="post">
<select name="table">
<option value="my_table">1</option>
<option value="my_table2">2</option>
</select>

<label></label>
<br>
<input type="submit" value="submit"></form>


<?php

If( $self)

{
$conn=@mysql_connect( "localhost", "design", "superdesign")
or die( "Err:Conn");

$rs= @mysql_select_db ( "gptesting_co_za_-_italchamdir", $conn )
or die ( "Err:Db");

# Create the query ************ How do I select a row? I tried stuff and codes but
#cannot get it to work*************What happen here is because $table is not
#specified the very first time you come to the page it gives an error..I just need to #somehow do like a "else if select first_name from a ************

$sql="select first_name from $table";
# this is what i tried $sql="select first_name from $table" else if ("select first_name from $table");

#execute

$rs = mysql_query( $sql, $conn );

#write data

While( $row = mysql_fetch_array( $rs ) )
{
echo( "ID: " . $row["first_name"] );
echo(" - First Name: " . $row["first_name"] . "<br>") ;
}
}
?>
</body>
</html>
[/PHP]
Apr 14 '07 #1
Share this Question
Share on Google+
6 Replies


Motoma
Expert 2.5K+
P: 3,235
The first thing you want to do in your script is see if this is the first time the script has been called or the second. You can do this by checking your $_POST variable to see if your form has been submitted.
You can then use a conditional statement to perform the correct actions (show the form, or show the results).
Apr 14 '07 #2

100+
P: 142
Hi, I cannot get the thing to work...

If I get the thing not to display at the opening of the page it does not diplay after I called the query.

What I don't understand is the script says "if ($self)" ->. Why does it then show the table query at the start of the page.

I tried a few things now but could not get it to work. Can someone please help me or show me an example or start the code for me as I really don't know what to do an search the net, books and on the php site.


This is the working code..I need to get it to run the script after you submited the form.

Please look at location:
# Create the query ************ I want the query to show "from a" the first time you open the page*****************************

[PHP]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>view</title>
</head>

<body>

<form action="<?php echo( $self ); ?>" method="post">
<select name="table">
<option value="my_table">1</option>
<option value="my_table2">2</option>
</select>

<label></label>
<br>
<input type="submit" value="submit"></form>


<?php
$submit = $_POST['submit'];
$self = $_SERVER['PHP_SELF'];
$table = $_POST['table'];



if ($self)

{
$conn=@mysql_connect( "localhost", "design", "superdesign")
or die( "Err:Conn");

$rs= @mysql_select_db ( "gptesting_co_za_-_italchamdir", $conn )
or die ( "Err:Db");

# Create the query ************ I want the query to show "from a" the first time you open the page*****************************

$sql="select first_name from $table";

#execute

$rs = mysql_query( $sql, $conn );

#write data

While( $row = mysql_fetch_array( $rs ) )
{
echo( "ID: " . $row["first_name"] );
echo(" - First Name: " . $row["first_name"] . "<br>") ;
}
}
?>
</body>
</html>

[/PHP]

Kind Regards
Louwrens
Apr 17 '07 #3

code green
Expert 100+
P: 1,726
The variable $self is declared in this line
Expand|Select|Wrap|Line Numbers
  1. $self = $_SERVER['PHP_SELF'];
The following is a meaningless test. What are you tring to achieve with this.
[PHP]If( $self) [/PHP] Motoma gave you the answer but you have ignored him
[PHP]if(isset($_POST['submit']))[/PHP]
Apr 17 '07 #4

100+
P: 142
Oh...sorry I am still new to PHP. I thought I meant I must put the whole php script in an if statement. That why I did not understand it cause it was already in a if...

Thanks - Let me try it...
Apr 17 '07 #5

100+
P: 142
Is this how it should be, because it aint working.

[PHP]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>view</title>
</head>

<body>

<form action="<?php echo( $self ); ?>" method="post">
<select name="table">
<option value="my_table">1</option>
<option value="my_table2">2</option>
</select>

<label></label>
<br>
<input type="submit" value="submit"></form>


<?php
$submit = $_POST['submit'];
$self = $_SERVER['PHP_SELF'];
$table = $_POST['table'];



if(isset($_POST['submit']))

{
$conn=@mysql_connect( "localhost", "design", "superdesign")
or die( "Err:Conn");

$rs= @mysql_select_db ( "gptesting_co_za_-_italchamdir", $conn )
or die ( "Err:Db");

# Create the query

$sql="select first_name from $table";

#execute

$rs = mysql_query( $sql, $conn );

#write data

While( $row = mysql_fetch_array( $rs ) )
{
echo( "ID: " . $row["first_name"] );
echo(" - First Name: " . $row["first_name"] . "<br>") ;
}
}
?>
</body>
</html>

[/PHP]
Apr 17 '07 #6

100+
P: 142
Thanks guys got it working!
Apr 19 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.