954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP dynamic web site using form database?

Hello fellow code heads!
I have issues (or so my wife says) but mine stem from the wonderful world of PHP. My web design knowledge is largely based on XHTML and that's where I left the scene. I was recently asked to delve back into the world of web design (something I haven't done in more than a decade and a half) but it was requested that the resulting code was in PHP...and so my learning odyssey began. But I digress...
I was asked to include a form for this site in question. This form is to (as far as I have been able to figure out on my own), on submission, write to a database. In turn, another page on the site would update from this database to display values from the form fields in a specific order. The form has other dynamic qualities such as drop-down selection menus that update in succession: Select Country→Select State/Province/Region(based on country selected)→Select City (based on State/Province/Region selected)...you get the picture, but that is yet another query.
I know this may sound like a fairly simple task for some but you will remember I am a squeaky clean virgin to PHP and while I am pretty quick on the uptake and I'm learning PHP at a phenominal rate, I just can't figure out how to properly code this so it works properly and the code is clean (I'm also a neat-freak). I desperately need help with the coding, syntax, stress reduction...
Any help given would be greatly appreciated, if only pointing me in the right direction to learn how to do this. THANKS IN ADVANCE!

P.S. I'm using PHP and mysql in Dreamweaver CS5 (if you're going to get back in, get back in big). Limited javascript knowledge and no AJAX knowledge to speak of.

HedoBum
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Hello fellow code heads! I have issues (or so my wife says) but mine stem from the wonderful world of PHP. My web design knowledge is largely based on XHTML and that's where I left the scene. I was recently asked to delve back into the world of web design (something I haven't done in more than a decade and a half) but it was requested that the resulting code was in PHP...and so my learning odyssey began. But I digress... I was asked to include a form for this site in question. This form is to (as far as I have been able to figure out on my own), on submission, write to a database. In turn, another page on the site would update from this database to display values from the form fields in a specific order. The form has other dynamic qualities such as drop-down selection menus that update in succession: Select Country→Select State/Province/Region(based on country selected)→Select City (based on State/Province/Region selected)...you get the picture, but that is yet another query. I know this may sound like a fairly simple task for some but you will remember I am a squeaky clean virgin to PHP and while I am pretty quick on the uptake and I'm learning PHP at a phenominal rate, I just can't figure out how to properly code this so it works properly and the code is clean (I'm also a neat-freak). I desperately need help with the coding, syntax, stress reduction... Any help given would be greatly appreciated, if only pointing me in the right direction to learn how to do this. THANKS IN ADVANCE!

P.S. I'm using PHP and mysql in Dreamweaver CS5 (if you're going to get back in, get back in big). Limited javascript knowledge and no AJAX knowledge to speak of.

:) hi there hedubom!

there are lots of good and best programmers here in daniweb.
i hope this one helps.

<html>
<body>
<!-- sample body -->

<?php
	// sample connection string for your database access
	
	$hostname = "localhost";
	$database = "sample_country_database";	
	$username = "root";
	$password = "samplepassword";

	mysql_connect($hostname,$username,$password);
	mysql_select_db($database) or die("Unable to select database.");		
?>

	<!-- select field and query -->
	
	<form method="post">	
		
		<select name="thecountryname" width = "200">
			<option value=''> Country Names  </option>

			<!-- sample country names display -->

			<?php
				$myquery=mysql_query("SELECT DISTINCT sample_country_name
																	from sample_country_table
																	");

				if(mysql_fetch_object($myquery) == null)
				{
					echo "<option value=''> No Record Found. </option>";
				}

				else
				{	
					
					$myquery=mysql_query("SELECT DISTINCT sample_country_name
																	from sample_country_table
																	");	

					while($countryname=mysql_fetch_object($myquery))
					{
						echo "<option value='$countryname->sample_country_name'>".$countryname->sample_country_name."</option>";
					}
				} 
			?>
		
		</select>

		<input type="submit" name="view" value="  View  ">
	</form>
	
<?php
	if($_POST['view'])
	{
		$counname = $_POST['thecountryname'];

		if($counname == null)
		{
			echo "<script type='text/javascript'>window.alert('Please check your input.')</script>";
			//echo "<META http-equiv='refresh' content='0; URL=index.php'>";
		}

		else if($counname != null)
		{

			$myquery=mysql_query("SELECT  * from sample_coutry_table");

			if(mysql_fetch_object($myquery) == null)
			{
				echo "<center>No Record Found.</center>"; // ---> this is if the sample_country_table have no values
			}
	
			else
			{
				$myquery=mysql_query("SELECT  DISTINCT sample_coutry_name
																from sample_country_table
																where sample_coutry_name = '$counname'
																");
			
				while($x=mysql_fetch_object($myquery))
				{
					echo " <center> ";
					echo " <font color = 'red'>Coutry Name : </font>".$x->sample_coutry_name;
				}
	
				echo " 
					
					<center>
					<table class = sample align=center width=1350px height = 50>
					<tr bgcolor = '#82CAFA'>
					<td width='50' align = 'center'> - </center></td>
					<td width='250' align = 'center'>COUTRY NAME</center></td>
					<td width='50'><center>COUNTRY DETAIL 1</center></td> <!-- sample details from here -->
					<td width='50'><center>COUNTRY DETAIL 2</center></td>
					<td width='50'><center>COUNTRY DETAIL 3</center></td>
					</tr><tr></tr>";
			
				$myquery=mysql_query("SELECT  sample_country_name, detail1, detail2, detail3
															from sample_coutry_table
															where sample_country_name like '$counname'
															order by sample_country_name");
	
				$check=0;
				while($y=mysql_fetch_object($myquery))
				{
				
					$check++;
			
					//---- country name ----
					echo "<tr>";
					echo "<td align = right width='50'>$check</td>";
					echo "<td width='250' align = left>".$y->sample_country_name."</td>";
					
					if ($y->detail1 == 0  || $y->detail1 == null) // ---> sample details and column names from here
					{
						echo "<td width='50' align = center><font color = red>"." - "."</font></td>"; 
					}
					else
					{
						echo "<td width='50' align = center>".$y->detail1."</td>";
					}
					if ($y->detail2 == 0  || $y->detail2 == null)
					{
						echo "<td width='50' align = center>"." - "."</td>"; 
					}
					else
					{
						echo "<td width='50' align = center>".$y->detail2."</td>";
					}
					if ($y->detail3 == 0  || $y->detail3== null)
					{
						echo "<td width='50' align = center>"." - "."</td>"; 
					}
					else
					{
						echo "<td width='50' align = center>".$y->detail3."</td>";
					}
			
					echo "</tr>";
				}
		
				echo "<tr></tr><tr></tr>";
				echo "</table></center>";
	
				echo 	" ";
			}
			
		}

		else
		{
			echo "<center>No Record Found.</center>";
		}
	}

	else
	{
		echo "<center>No Record Found.</center>";
	}
	
?>

</body>
</html>


happy day ahead! ~_^

ﻼim
Junior Poster
171 posts since Aug 2010
Reputation Points: 10
Solved Threads: 13
 

WoW...thank you very much ﻼim!
I didn't expect to see a reply so quickly with a page full of code to boot,...and commented no less.
I should have been checking more diligently but I thought I would be automatically emailed when someone answered. I should have checked my settings.
Thank you again ﻼim and I'll try this code to see how it works.

I'll have to get my hands on a proper database to use with it...it's surprising how difficult it is to find a simple sql database with just Country/State/Province/Region in one place. But that's another query.

Thank you once again.

HedoBum
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

*sigh*...I am such a newb
I can't get this code to work for me. I have a database for this but I don't think I'm getting the syntax right or something. I have the database set up...I get a connection with DW but I don't think I implemented it into this code properly or something because in LiveView I just get a blank page with "Unable to select database" at the top. Maybe there needed to be commenting geared for Über-newb because I can't for the life of me figure where I went wrong. Someone figure this out for a database called country.php?

edit: ...and a database table of countries?

HedoBum
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Karibu Daniweb (Welcome to Daniweb)
Since your odyssey is just starting and taking account of your background, PHP code won't make much sense if any at all. I advice you do crash course with PHP and MySQL . you have two "colleges" to learn that. Either Tizag or w3School . After that, you will see how easy that task is becoming :)

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

*sigh*...I am such a newb I can't get this code to work for me. I have a database for this but I don't think I'm getting the syntax right or something. I have the database set up...I get a connection with DW but I don't think I implemented it into this code properly or something because in LiveView I just get a blank page with "Unable to select database" at the top. Maybe there needed to be commenting geared for Über-newb because I can't for the life of me figure where I went wrong. Someone figure this out for a database called country.php?

edit: ...and a database table of countries?

yeah. i'll suggest that you take the former's advice. ~_^

:) at the start it may seem complicated but later as you go on with practice and trials you'll see, it's not that difficult.

ﻼim
Junior Poster
171 posts since Aug 2010
Reputation Points: 10
Solved Threads: 13
 

I use Uniform Server as a development platform for all my web design. It's light weight and copies how a real server would run. It's perfect for development... and it comes with PMA (PHPMyAdmin).

[Links]
http://www.uniformserver.com/

Nyight
Junior Poster
100 posts since Aug 2010
Reputation Points: 23
Solved Threads: 21
 
Karibu Daniweb (Welcome to Daniweb) Since your odyssey is just starting and taking account of your background, PHP code won't make much sense if any at all. I advice you do crash course with PHP and MySQL . you have two "colleges" to learn that. Either Tizag or w3School . After that, you will see how easy that task is becoming :)

Good advice...I'm using W3Schools now. W3Schools is invaluable for information on pretty much anything web related and I reference it regularly (usually).
I am an impatient type and I want to know how to do thingsnow. Needless to say, I learn on an exponential curve.
Thanks for the replies all.

HedoBum
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

If you face specific problem in your learning just file a thread. I think you should close this for now, if you think "all is over" about this thread ;)

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

ﻼim...I think I understand your code now...well in a cursory way. I just went through it again and now that I have a bit better than a "tourists vocabulary" of PHP, it looks clearer to me. I'm not quite fluent yet but I'm getting there.
Thanks again for the help.

@evstevemd: Thanks for the heads-up...I failed to remember this forum was on the open help system.

HedoBum
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

ﻼim...I think I understand your code now...well in a cursory way. I just went through it again and now that I have a bit better than a "tourists vocabulary" of PHP, it looks clearer to me. I'm not quite fluent yet but I'm getting there. Thanks again for the help.

@evstevemd: Thanks for the heads-up...I failed to remember this forum was on the open help system.

no problem.
:) good thing for you.

you'll learn more as you go on.
good luck.

ﻼim
Junior Poster
171 posts since Aug 2010
Reputation Points: 10
Solved Threads: 13
 

no problem. :) good thing for you.

you'll learn more as you go on. good luck.

Stay seated folks! I've re-assessed my problem. Learnedloads about PHP and mySQL and worked out a post to database script based on my limited knowledge and the able skills of someone who wrote a pretty darn good tutorial.

Problem is...the script isn't working for me and I can't seem to get any help on the issue but I'll be posting a new HELP ME! HELP ME! thread shortly that will scream newbie. I just have to try a few more things before I employ the help of bigger beans here.
Stay tuned!

HedoBum
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
Sbut I'll be posting a new HELP ME! HELP ME! thread shortly that will scream newbie.

Don't do that as we might get annoyed by your big voice...;) Now seriously, when you post, post meaningful title, instead of help me, or urgent use descriptive words like error message or "Cannot connect to MySQL database"

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

Oh PLEASE! did you really think I was going to post a thread with a title of "HELP ME! HELP ME!"?
C'mon...I may be a newb but I'm not a stupid newb. :D

HedoBum
Newbie Poster
15 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
Oh PLEASE! did you really think I was going to post a thread with a title of "HELP ME! HELP ME!"? C'mon...I may be a newb but I'm not a stupid newb. :D


:D

evstevemd
Senior Poster
3,713 posts since Jun 2007
Reputation Points: 462
Solved Threads: 392
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You