Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 2nd, 2012, 08:06 PM
Registered User
 
Join Date: Dec 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Ch. 6 commit.php

Ok so this is the code from the book on Ch. 6 for the first commit.php,

Quote:
<?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>Commit</title>
</head>
<body>
<?php
switch ($_GET['action']) {
case 'add':
switch ($_GET['type']) {
case 'movie':
$query = 'INSERT INTO
movie
(movie_name, movie_year, movie_type, movie_leadactor,
movie_director)
VALUES
("' . $_POST['movie_name'] . '",
' . $_POST['movie_year'] . ',
' . $_POST['movie_type'] . ',
' . $_POST['movie_leadactor'] . ',
' . $_POST['movie_director'] . ')';

break;
}
break;
}

if (isset($query)) {
$result = mysql_query($query, $db) or die(mysql_error($db));
}
?>
<p>Done!</p>
</body>
</html>
I've been getting an error and I think there's something wrong with the VALUES part of the query, can somebody tell me whether there is something wrong with this code and how to correct it?
 
Old January 15th, 2012, 02:35 AM
Registered User
 
Join Date: Dec 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Can anybody help? Let me add some more details about what my question is and be more specific.
Code:
VALUES
("' . $_POST['movie_name'] . '",
' . $_POST['movie_year'] . ',
' . $_POST['movie_type'] . ',
' . $_POST['movie_leadactor'] . ',
' . $_POST['movie_director'] . ')';
I suspect that there's something wrong with the particular code above. My question is why is the first $_POST value encapsulated with both double-quotes and single quotes but the rest aren't? Also why does each $_POST have a period before and after it, isn't that used to concatenate something in PHP?
 
Old January 15th, 2012, 10:00 AM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

Try the following, I've altered it slightly so that the query is wrapped in " and any text value being stored in the db is wrapped in ' I am unable to test it as I haven't got the db setup etc and the book is at work.

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>Commit</title>
</head>
<body>
<?php
switch ($_GET['action'])
{
    case 
'add':
        switch (
$_GET['type'])
        {
            case 
'movie':
                
$query    =    "INSERT INTO movie (movie_name, movie_year, movie_type, movie_leadactor, movie_director)
                            VALUES ('" 
$_POST['movie_name'] . "',
                                    '" 
$_POST['movie_year'] . "',
                                    '" 
$_POST['movie_type'] . "',
                                    '" 
$_POST['movie_leadactor'] . "',
                                    '" 
$_POST['movie_director'] . "')";
            break;
        }
    break;
}

if (isset(
$query))
{
    
$result mysql_query($query$db) or die(mysql_error($db));
}
?>
<p>Done!</p>
</body>
</html>
And yes the period . is used to add some text to the $query string thus once complete you should get something like the following in $query;
Code:
INSERT INTO movie (movie_name, movie_year, movie_type, movie_leadactor, movie_director)
VALUES ('Dreamscape', '1988', 'Horror', 'Dennis Quaid', 'Joseph Ruben')

Last edited by UseLess; January 15th, 2012 at 10:11 AM..
 
Old January 15th, 2012, 04:48 PM
Registered User
 
Join Date: Dec 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by UseLess View Post
Greetings,

Try the following, I've altered it slightly so that the query is wrapped in " and any text value being stored in the db is wrapped in ' I am unable to test it as I haven't got the db setup etc and the book is at work.

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>Commit</title>
</head>
<body>
<?php
switch ($_GET['action'])
{
    case 
'add':
        switch (
$_GET['type'])
        {
            case 
'movie':
                
$query    =    "INSERT INTO movie (movie_name, movie_year, movie_type, movie_leadactor, movie_director)
                            VALUES ('" 
$_POST['movie_name'] . "',
                                    '" 
$_POST['movie_year'] . "',
                                    '" 
$_POST['movie_type'] . "',
                                    '" 
$_POST['movie_leadactor'] . "',
                                    '" 
$_POST['movie_director'] . "')";
            break;
        }
    break;
}

if (isset(
$query))
{
    
$result mysql_query($query$db) or die(mysql_error($db));
}
?>
<p>Done!</p>
</body>
</html>
And yes the period . is used to add some text to the $query string thus once complete you should get something like the following in $query;
Code:
INSERT INTO movie (movie_name, movie_year, movie_type, movie_leadactor, movie_director)
VALUES ('Dreamscape', '1988', 'Horror', 'Dennis Quaid', 'Joseph Ruben')
Thanks, it worked!
 
Old February 5th, 2012, 04:06 PM
Registered User
 
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello all,

I have a problem also in ch 6 but in movie.php. I'm on page 157 i writed the code but i get each record of the Movie type,Year,Lead Actor,Director
two times.For example if a record for movie type is Action i get in list Action Action. Why ? Here is the code:

Code:
<?php
$db = mysql_connect('localhost','','') 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 method="post" action="commit.php?action=add&type=movie">
  <table width="408" height="142" border="1">
    <tr>
      <td width="147">Movie Name</td>
      <td width="182">
      <input type="text" name="textfield" id="moviename"></td>
    </tr>
    <tr>
      <td>Movie Type</td>
      <td>
        <select name="movie_type">
	  <?php
	  $query = 'select movietype_id, movietype_label 
	  from movietype
	  ORDER BY 
	  movietype_label';
	  $result=mysql_query($query, $db) or die(mysql_error($db));

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
		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
		$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));
		  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
		$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>
	
  </table> <input type="submit" name="submit" value="Add"/>
</form>
</body>
</html>

Last edited by Stamatis; February 5th, 2012 at 04:11 PM..
 
Old February 5th, 2012, 04:54 PM
Friend of Wrox
 
Join Date: May 2011
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

That's because you have a foreach inside the while loop, and you're not even using the foreach so remove it so it's like this:
Code:
while ($row = mysql_fetch_assoc($result))
{ 
	echo '<option value="' . $row['movietype_id'] . '">' . $row['movietype_label'] . '</option>';
}
 
Old February 5th, 2012, 04:57 PM
Registered User
 
Join Date: Jan 2012
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

its ok now.
perhaps it was a wrong of the book while the code was the same of the book

thank you





Similar Threads
Thread Thread Starter Forum Replies Last Post
Commit.PHP and Delete.PHP in WROX's book GiFos Beginning PHP 0 January 30th, 2007 10:51 PM
chapter 7 commit.php ratxamala BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 2 November 2nd, 2005 09:24 AM
ch 6 commit.php problem updating movie table statusquo BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 May 2nd, 2005 10:02 PM
Error: movie.php & commit.php on p182-186, ch6 willburke BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 October 12th, 2004 02:48 PM
ch 7 page 208 commit.php pipdickenz BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 1 September 8th, 2004 12:08 PM





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