Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 10th, 2010, 02:42 PM
Authorized User
 
Join Date: Jan 2010
Posts: 13
Thanks: 2
Thanked 1 Time in 1 Post
Default Ch. 6 - admin.php - HELP

Just a question that I hope someone has an easy answer to. The programs all run perfectly - almost. On my drop down lists all the info is duplicated. Each actor and director and movie type is in the list twice! Can't see any errors but it seems obvious to me that my code is calling for the same info twice. HELP!!!
Reply With Quote
  #2 (permalink)  
Old February 10th, 2010, 03:38 PM
Authorized User
Points: 105, Level: 2
Points: 105, Level: 2 Points: 105, Level: 2 Points: 105, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Location: Wolfeboro, New Hampshire / Boston
Posts: 25
Thanks: 1
Thanked 4 Times in 4 Posts
Default

Jeez, You got thru chapter 5 pretty quick, you've almost caught up to me. Could you post the code? I'm betting it's either in your loop, or in your SQL statement. If I remember correctly, that program keeps growing throughout the chapter, so if you could also mention which page. Sorry, if I'm wrong about that, I don't have it in front of me right now.

Another possibility is how you are getting each row out of the result set. Are you using mysql_fetch_assoc() ?
Reply With Quote
  #3 (permalink)  
Old February 10th, 2010, 04:51 PM
Authorized User
 
Join Date: Jan 2010
Posts: 13
Thanks: 2
Thanked 1 Time in 1 Post
Default

Are you using mysql_fetch_assoc() ? Yep....
You got thru chapter 5 pretty quick....seems like it is going very slowly.
Anyway, here is the code.....see what you can see.....THANKS
This is the admin txt file (also have it saved as .php) After that is movie.php
[CODE<?php
$db = mysql_connect('localhost', 'bp6am', 'bp6ampass') or
die ('Unable to connect. Check your connection parameters.');
mysql_select_db('moviesite', $db) or die(mysql_error($db));
?>
<html>
<head>
<title>Movie database</title>
<style type="text/css">
th { background-color: #999;}
.odd_row { background_color: #EEE; }
.even_row { background_color: #FFF; }
</style>
</head>
<body>
<table style="width:100%;">
<tr>
<th colspan="2">Movies <a href="movie.php?action=add">[ADD]</a></th>
</tr>
<?php
$query = 'SELECT * FROM movie';
$result = mysql_query($query, $db) or die (mysql_error($db));

$odd = true;
while ($row = mysql_fetch_assoc($result)) {
echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
$odd = !$odd;
echo '<td style="width:75%;">';
echo $row['movie_name'];
echo '</td><td>';
echo ' <a href="movie.php?action=edit&id=' . $row['movie_id'] . '"> [EDIT]</a>';
echo ' <a href="delete.php?type=movie&id=' . $row['movie_id'] . '"> [DELETE]</a>';
echo '</td></tr>';
}
?>
<tr>
<th colspan="2">People <a href="people.php?action=add"> [ADD]</a></th>
</tr>
<?php
$query = 'SELECT * FROM people';
$result = mysql_query($query, $db) or die (mysql_error($db));
$odd = true;
while ($row = mysql_fetch_assoc($result)) {
echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
$odd = !$odd;
echo '<td style="width: 25%;">';
echo $row['people_fullname'];
echo '</td><td>';
echo ' <a href="people.php?action=edit&id=' . $row['people_id'] . '"> [EDIT]</a>';
echo ' <a href="delete.php?type=people&id=' . $row['people_id'] . '"> [DELETE]</a>';
echo '</td></tr>';
}
?>
</table>
</body>
</html>
[/code]

Code:
<?php
$db = mysql_connect('localhost', 'bp6am', 'bp6ampass') or
    die ('Unable to connect. Check your connection parameters.');
mysql_select_db('moviesite', $db) or die(mysql_error($db));
?>
<html>
 <head>
  <title>Add Movie</title>
 </head>
  <body>
   <form action="commit.php?action=add&type=movie" method="post">
    <table>
     <tr>
      <td>Movie Name</td>
      <td><input type="text" name="movie_name"/></td>
     </tr><tr>
      <td>Movie Type</td>
      <td><select name="movie_type">
<?php
//select movie type information
$query = 'SELECT
       movietype_id, movietype_label
     FROM
       movietype
     ORDER BY
       movietype_label';
$result = mysql_query($query, $db) or die(mysql_error($db));

//populate the select options with the results
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $value) {
       echo '<option value="' . $row['movietype_id'] . '">';
       echo $row['movietype_label'] . '</option>';
   }
}
?>
      </select></td>
     </tr><tr>
      <td>Movie Year</td>
      <td><select name="movie_year">
<?php
// populate the select options with years
for ($yr = date("Y"); $yr >= 1970; $yr--) {
     echo '<option value="' . $yr . '">' . $yr . '</option>';
}
?>
     </select></td>
    </tr><tr>
     <td>Lead Actor</td>
     <td><select name="movie_leadactor">
<?php
//select actor records
$query = 'SELECT
       people_id, people_fullname
    FROM
       people
    WHERE
       people_isactor = 1
    ORDER BY
       people_fullname';
$result = mysql_query($query, $db) or die(mysql_error($db));

//populate the select options with the results
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $value) {
        echo '<option value="' . $row['people_id'] . '">';
        echo $row['people_fullname'] . '</option>';
   }
}
?>
      </select></td>
     </tr><tr>
     <td>Director></td>
     <td><select name="movie_director">
<?php

//select director records
$query = 'SELECT
       people_id, people_fullname
     FROM
       people
     WHERE
       people_isdirector = 1
     ORDER BY
       people_fullname';
$result = mysql_query($query, $db) or die(mysql_error($db));

//populate the select options with the results
while ($row = mysql_fetch_assoc($result)) {
    foreach ($row as $value) {
        echo '<option value="' . $row['people_id'] . '">';
        echo $row['people_fullname'] . '</option>';
   }
}
?>
      </select></td>
     </tr><tr>
      <td colspan="2" style="text-align: center;">
       <input type="submit" name="submit" value="Add" />
      </td>
     </tr>
    </table>
   </form>
  </body>
</html>
Reply With Quote
  #4 (permalink)  
Old February 10th, 2010, 10:50 PM
Authorized User
Points: 105, Level: 2
Points: 105, Level: 2 Points: 105, Level: 2 Points: 105, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Location: Wolfeboro, New Hampshire / Boston
Posts: 25
Thanks: 1
Thanked 4 Times in 4 Posts
Default

Just came home and gave it a quick look, can't find anything. I think I'll have a few beers and try again. Just another thought: have you tried going in to MySql at the command prompt and executing "select * from people;"? They may be in there twice. Could have happened if you ran the insert scripts twice, especially if the first part inserted ok. But errored out at a later stage. If you fixed it and ran it again, the people inserted the first time would have been entered again.

You can also try this:
Code:
MySql> select count(*) from people;
That will give you the total rows from the table and see if it makes sense.
Reply With Quote
  #5 (permalink)  
Old February 10th, 2010, 11:25 PM
Authorized User
Points: 105, Level: 2
Points: 105, Level: 2 Points: 105, Level: 2 Points: 105, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Location: Wolfeboro, New Hampshire / Boston
Posts: 25
Thanks: 1
Thanked 4 Times in 4 Posts
Default

OK. After a few beers, I found it. You did it right. The BOOK is wrong.

Get rid of the foreach loops inside the while loops and everything is fine. Those even look weird, cause they never even use the $value in the loop. I downloaded your code and ran it with the same result. I deleted the foreach loops and the problem was fixed. Those loops don't even make logical sense, I'll bet somebody has already complained and entered an errata.

The reason I never ran into this was that I tend to take a different approach. Instead of typing in the code as is, I look at the resulting screen and see if I can write something that does it properly, only looking back at the book code when I get stuck ( AND I ALWAYS DO:):)). If it helps, here's what I had come up with for movie.php . Definitely some interesting differences. BTW, note the \n's and \t's, those help the HTML source come out neat when you hit "view source" in the browser. Makes it a LOT easier to diagnose HTML generated in loops problems. Here is my version:

Code:
<?php
$db = mysql_connect('localhost', 'bp6am', 'bp6ampass') or
	die("Unable to connect to database.");
	
mysql_select_db('moviesite', $db) or die("DB_USE: " . mysql_error($db));

//echo "</br></br>DEBUG:</br><pre>\$_POST</br>"; print_r($_POST); echo "\$_GET</br>"; print_r($_GET); echo "</pre>";

if ($_GET['action'] == "edit") {
	$query = "select * from movie where movie_id = " . $_GET['id'];
	$result = mysql_query($query, $db) or die("SELECTMOVIE: ". mysql_error($db));
	$row = mysql_fetch_assoc($result);
	extract($row);
	
//echo "</br></br>DEBUG:</br><pre>\$row</br>"; print_r($row); echo "</pre>";
} else {  //action must be "add"
	$movie_name =  "";
	$movie_type = 0;
	$movie_year = date('Y');
	$movie_leadactor = 0;
	$movie_director = 0;
}
?>
<html>
<head>
<title>
	<?php echo ucfirst($_GET['action']) . " Movie"; ?>
</title>
<body>
<form action="commit.php?action=<?php echo $_GET['action']; ?>&type=movie" method="post">
<table>
	<tr>
		<td>Movie Name</td>
		<td><input type="text" name="movie_name" 
			value="<?php echo $movie_name; ?>"/>
	</tr>
	<tr>
		<td>Movie Type</td>
		<td>
<?php
$query = <<< SELECTTYPE
select
	movietype_id, movietype_label
from 
	movietype
order by
	movietype_label
SELECTTYPE;

$result = mysql_query($query, $db) or die("SELECT: " . mysql_error($db));
echo "\n\t<select name=\"movie_type\">";
while($row = mysql_fetch_assoc($result)) {
	echo "\n\t\t<option value=" . $row['movietype_id'];
	if ($row['movietype_id'] == $movie_type) {
			echo " selected=\"selected\" ";
	}
	echo ">" .  $row['movietype_label'] . "</option>";
} // while		
echo "\n\t</select>";
?>
		</td>
	</tr>
	<tr>
		<td>Movie Year</td>
		<td>
<?php
echo "\n\t<select name=\"movie_year\">";
$year = 2012;
while ($year >= 1935) {
	echo "\n\t\t<option value=\"$year\"";
	if ($year  == $movie_year) {
		echo " selected=\"selected\" ";
	}
	echo ">$year</option>";
	$year--;
}
echo "\n\t</select>";

?>
		</td>

	</tr>
	<tr>
		<td>Lead Actor</td>
		<td>
<?php
$query = <<< SELECTACTORS
select
	people_fullname,
	people_id
from
	people
where
	people_isactor = 1
SELECTACTORS;

$result = mysql_query($query, $db) or die("SELECTACTOR: " . mysql_error($db));
echo "\n\t<select name=\"movie_leadactor\">";
while($row = mysql_fetch_assoc($result)) {
	echo "\n\t\t<option value=" . $row['people_id'];
	if ( $row['people_id'] == $movie_leadactor ) {
		echo " selected=\"selected\" ";
	}
	echo ">" .  $row['people_fullname'] . "</option>";
} // while		
echo "\n\t</select>";
?>
		</td>
	</tr>
	<tr>
		<td>Movie Director</td>
		<td>
<?php
$query = <<< SELECTDIRECTORS
select
	people_fullname,
	people_id
from
	people
where
	people_isdirector = 1
SELECTDIRECTORS;

$result = mysql_query($query, $db) or die("SELECTDIRECTOR: " . mysql_error($db));
echo "\n\t<select name=\"movie_director\">";
while($row = mysql_fetch_assoc($result)) {
	echo "\n\t\t<option value=" . $row['people_id'];
	if ( $row['people_id'] == $movie_director ) {
		echo " selected=\"selected\" ";
	}
	echo ">" .  $row['people_fullname'] . "</option>";
} // while		
echo "\n\t</select>";
?>
		</td>
	</tr>



<tr>
	<td colspan="2" style="text-align: center;">
	<?php
	if ($_GET['action'] == "edit") {
		echo '<input type="submit" name="submit" value="Save" />';
		echo "<input type=\"hidden\" name=\"movie_id\" value=\"$movie_id\" />";
	} else {
		echo '<input type="submit" name="submit" value="Add" />';
	}
	?>
	</td>
</tr>
</table>
</form>
<?php 
echo "<pre>"; echo '$_POST '; echo print_r($_POST); echo "</pre>";
echo "<pre>"; echo '$_GET '; echo print_r($_GET); echo "</pre>";
?>
</body>
</html>
Reply With Quote
  #6 (permalink)  
Old February 10th, 2010, 11:28 PM
Authorized User
Points: 105, Level: 2
Points: 105, Level: 2 Points: 105, Level: 2 Points: 105, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Location: Wolfeboro, New Hampshire / Boston
Posts: 25
Thanks: 1
Thanked 4 Times in 4 Posts
Default

BTW. I was right. Check out drupalmeister's June 4th post with p 157 in the thread name. He found the same problem.
Reply With Quote
The Following User Says Thank You to BozHogan For This Useful Post:
drgnhiker (February 11th, 2010)
  #7 (permalink)  
Old February 11th, 2010, 01:53 PM
Authorized User
 
Join Date: Jan 2010
Posts: 13
Thanks: 2
Thanked 1 Time in 1 Post
Default Thanks

Interesting code.....because I am just learning all this stuff some of it makes no sense to me so I went off on a research spree to find answers....could not find a reference for your \t......can you explain that one?
I don't try too many things on my own yet but am always willing to learn new ways to do the same thing, specially if it = quicker/easier.
Thanks again for all your help......there may be questions coming.
Reply With Quote
  #8 (permalink)  
Old February 12th, 2010, 11:43 PM
Authorized User
Points: 105, Level: 2
Points: 105, Level: 2 Points: 105, Level: 2 Points: 105, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Location: Wolfeboro, New Hampshire / Boston
Posts: 25
Thanks: 1
Thanked 4 Times in 4 Posts
Default \n and \t 's in echo statements

No Problem. I usually look at the end screen and then try and fight my way through the code, cause in the end, I really feel like I've learned a lot more.

\n is a newline
\t is a tab

It won't have any effect on what shows up in the browser, because browsers ignore white space in the HTML. Viewing source however, is another story. They show up there. Making it easier to read and debug, especially when that darned loop isn't doing the right thing, and mine never seem to on the first try. Usually viewing source points me right at the bug in my PHP. It just helps if it's formatted nicely. That's all the \n's and \t's do.
Reply With Quote
  #9 (permalink)  
Old February 13th, 2010, 12:22 AM
Authorized User
Points: 105, Level: 2
Points: 105, Level: 2 Points: 105, Level: 2 Points: 105, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2010
Location: Wolfeboro, New Hampshire / Boston
Posts: 25
Thanks: 1
Thanked 4 Times in 4 Posts
Default More questions

BTW, glad to see your researching this stuff. In the professional world these days, it's perhaps become even less important to know how to do something, than it is to be able to (and know where to) go to find out how to do it. Let me know if you find any really good resources. So far I'm pretty much just relying on www.php.net. And www.w3schools.com is pretty good for all kinds of non PHP stuff like HTML, Javascript, CSS, XML, etc. I think the 2nd one is the official site of the W3C, but not 100% sure of that.

And keep the questions coming. I love this stuff and it keeps me on my toes. I'm getting old and repetition helps things sink into this old brain, so keep 'em coming. It helps me too.

Who knows? Maybe we'll get tagged as a "HOT THREAD", what ever that means.

Regards,
Boz
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP 5, Wrox, CH 7 navigator.php Jon_ BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 August 24th, 2008 01:13 PM
Beg. PHP 5 > Ch. 11 - fetch_field.php crater BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 January 2nd, 2007 12:20 PM
Forum admin.php LCMatt PHP How-To 0 March 18th, 2006 06:27 PM
Ch. 1: PHP Installation Troubles: php3 vs php manjito BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 4 June 12th, 2003 03:59 PM



All times are GMT -4. The time now is 02:25 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.