473,779 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help. php reading before it should

142 New Member
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.dt d">
<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_table 2">2</option>
</select>

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


<?php

If( $self)

{
$conn=@mysql_co nnect( "localhost" , "design", "superdesig n")
or die( "Err:Conn") ;

$rs= @mysql_select_d b ( "gptesting_co_z a_-_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_arr ay( $rs ) )
{
echo( "ID: " . $row["first_name "] );
echo(" - First Name: " . $row["first_name "] . "<br>") ;
}
}
?>
</body>
</html>
[/PHP]
Apr 14 '07 #1
6 1449
Motoma
3,237 Recognized Expert Specialist
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
webandwe
142 New Member
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.dt d">
<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_table 2">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_co nnect( "localhost" , "design", "superdesig n")
or die( "Err:Conn") ;

$rs= @mysql_select_d b ( "gptesting_co_z a_-_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_arr ay( $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
1,726 Recognized Expert Top Contributor
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
webandwe
142 New Member
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
webandwe
142 New Member
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.dt d">
<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_table 2">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_co nnect( "localhost" , "design", "superdesig n")
or die( "Err:Conn") ;

$rs= @mysql_select_d b ( "gptesting_co_z a_-_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_arr ay( $rs ) )
{
echo( "ID: " . $row["first_name "] );
echo(" - First Name: " . $row["first_name "] . "<br>") ;
}
}
?>
</body>
</html>

[/PHP]
Apr 17 '07 #6
webandwe
142 New Member
Thanks guys got it working!
Apr 19 '07 #7

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

Similar topics

2
1787
by: Parker.Jim | last post by:
I need to write a program which performs word subsitutions on a text file. The program should input the names of three text files: the source file that will be "edited", a text file that contains the editing instructions, and a file that will contain the result of the editing.
0
5576
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted ******************************************************** For this teeny job, please refer to: http://feeds.reddit.com/feed/8fu/?o=25
22
3278
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of seats as 0 and the flight no. is also repeating. If any can tell why is this please help me. #include<stdio.h> #include<ctype.h> #include<conio.h>
2
2685
by: ali.sobh | last post by:
Hi, i am new in c++ and i need some help with a program i need to do a universal unit converter. this calculator can only do : +, *, /, and -. it should read a string (the equation) and ask you to put the value of that string. i wasn't able to do that, but i could do with characters... for ex : if i write: CelsiusTemp * ConversionFactor + base....it should ask me to input the value of celsiustemp and ...
5
5083
Sl1ver
by: Sl1ver | last post by:
I have this piece of code public bool Write(string KeyName, object Value) { try { // Setting string subKey1 = "SOFTWARE\\QwixAssets"; RegistryKey rk = Registry.LocalMachine; RegistryKey sk1 = rk.CreateSubKey(subKey1);
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10139
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4037
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3632
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.