Помощ с коригирането на грешка

pLe0mAx

Registered
Купил съм си двете книги "Програмиране и уеб дизайн с PHP5, Apache, MySQL" стигнах до известна част от книгата и ми дава грешка. Ако може малко помощ.

самата грешка е :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '='1' ORDER BY review_date DESC' at line 1

ето и кода на movie_details.php

PHP:
<?php
$link = mysql_connect("localhost","root","password")
or die (mysql_error());

mysql_select_db("moviesite")
or die (mysql_error());

// Function to get the director's name from the people's table

function get_director() {
    global $movie_director;
    global $director;
    
$query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'";
$results_d=mysql_query($query_d)
    or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;

}

//Function to get the lead actor's name from the people table
function get_leadactor()    {
global $movie_leadactor;
global $leadactor;

$query_a= "Select people_fullname FROM people WHERE people_id='$movie_leadactor'";
$result_a=mysql_query($query_a)
or die (mysql_error());
$row_a=mysql_fetch_array($result_a);
extract($row_a);
$leadactor = $people_fullname;
}

/* $query = "SELECT * FROM movie" .
        " WHERE movie_id='" . $_GET['movie_id'] . "'";
*/

$movie_query = "SELECT * FROM movie" .
        " WHERE movie_id='" . $_GET['movie_id'] . "'";
        
//$result = mysql_query($query)
$movie_result = mysql_query($movie_query,$link)
or die (mysql_error());

$movie_table_headings=<<<EOD
<tr>
<th>Movie Title</th>
<th>Year of Release</th>
<th>Movie Director</th>
<th>Movie Lead Actor</th>
<th>Movie Running Time</th>
<th>Movie Health</th>
</tr>
EOD;
function generate_ratings($review_rating)    {
    $movie_rating = '';
    for ($i=0;$i<$review_rating;$i++)    {
        $movie_rating .= "<img src=\"star.gif\"> ";
        }
    return $movie_rating;
    }

// echo $_GET['movie_id'];
$review_query = "SELECT * FROM reviews" .
    "WHERE review_movie_id='" . $_GET['movie_id'] . "' " .
	"ORDER BY review_date DESC"; 
	
$review_result = mysql_query($review_query,$link)
or die (mysql_error());


//while ($row = mysql_fetch_array($result))    {
while ($row = mysql_fetch_array($movie_result))    {
    $movie_name =$row['movie_name'];
    $movie_director = $row['movie_director'];
    $movie_leadactor =$row['movie_leadactor'];
    $movie_year= $row['movie_year'];
    $movie_running_time = $row['movie_running_time']."mins";
    $movie_takings=$row['movie_takings'];
    $movie_cost =$row['movie_cost'];
    }

//get director's name from people table
get_director();

//get lead actor's name from people table
get_leadactor();


/*Function to calculate if a movie made a profit, loss or broken even */


function calculate_differences($takings,$costs) {
    $difference = $takings - $costs;

if ($difference < 0)    {
    $difference = substr ($difference, 1);
    $font_color = 'red';
    $profit_or_loss = "S" . $difference. "m";
}   else if ($difference >0)    {
    $font_color = 'green';
    $profit_or_loss = "$" . $difference. "m";
}    else {
    $font_color = 'blue';
    $profit_or_loss= "Broke even";
}

return "<font color=\"$font_color\">" . $profit_or_loss. "</font>";
}
$movie_health = calculate_differences($movie_takings,$movie_cost);
$page_start=<<<EOD
<html>
<head>
<title> Details and Reviews for: $movie_name</title>
</head>
<body>
EOD;
if ($review_flag)    {
    $movie_details .=<<<EOD
<table width="95%" border="0" cellspacing="2"
    cellpadding="20" align="center">
    $review_table_headings
    $review_details
    </table>
EOD;
}



$movie_details =<<<EOD
<table width ="70%" border="0" cellspacing="2"
    cellpadding="2" align="center">
<tr>
    <th colspan="6"><u><h2>$movie_name: Details</h2></u></th>
</tr>
//$movie_table_headings
$review_table_headings=<<<EOD
<tr>
<th> Date of Review</th>
<th> Review Title</th>
<th> Reviewer Name</th>
<th> Movie Review Comments</th>
<th> Rating</th>
</tr>

<tr>
    <td width="33%" align="center">$movie_name</td>
    <td align="center">$movie_year</td>
    <td align="center">$director</td>
    <td align="center">$leadactor</td>
    <td align="center">$movie_running_time</td>
    <td align="center">$movie_health</td>
</tr>
</table>
<br>
<br>
EOD;
while($review_row = mysql_fetch_array($review_results))    {
    $review_flag=1;
    $review_title[] = $review_row['review_name'];
    $reviewer_name[] =ucwords($review_row['review_reviwer_name']);
    $review[]=$review_row['review_comment'];
    $review_date[] = $review_row['review_date'];
    $review_rating[] = generate_ratings($review_row['review_rating']);
    }

$i=0;
$review_details = '';
while ($i<sizeof($review))    {
    $review_details .=<<<EOD
    <tr>
        <td width="15%" valign="top" align="center">$review_daye[$i]</td>
        <td width="15%" valign="top">review_title[$i]</td>
        <td width="10%" valign="top">reviewer_name[$i]</td>
        <td width="50%" valign="top">reviwer[$i]</td>
        <td width="10%" valign="top" align="center">$review_rating[$i]</td>
        </tr>
EOD;
$i++;
}

$page_end =<<<EOD
</body>
</html>
EOD;
$detailed_movie_info =<<<EOD
    $page_start
    $movie_details
    $page_end
EOD;
echo $detailed_movie_info;
mysql_close();



?>

В гугъл търсих и попаднах на стара подобна тема, но решение на проблема не бе показано.
 
PHP:
<?php
$link = mysql_connect("localhost","root","password")
or die (mysql_error());

mysql_select_db("moviesite")
or die (mysql_error());

// Function to get the director's name from the people's table

function get_director() {
    global $movie_director;
    global $director;
    
$query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'";
$results_d=mysql_query($query_d)
    or die(mysql_error());
$row_d = mysql_fetch_array($results_d);
extract($row_d);
$director = $people_fullname;

}

//Function to get the lead actor's name from the people table
function get_leadactor()    {
global $movie_leadactor;
global $leadactor;

$query_a= "Select people_fullname FROM people WHERE people_id='$movie_leadactor'";
$result_a=mysql_query($query_a)
or die (mysql_error());
$row_a=mysql_fetch_array($result_a);
extract($row_a);
$leadactor = $people_fullname;
}

/* $query = "SELECT * FROM movie" .
        " WHERE movie_id='" . $_GET['movie_id'] . "'";
*/

$movie_query = "SELECT * FROM movie" .
        " WHERE movie_id='" . $_GET['movie_id'] . "'";
        
//$result = mysql_query($query)
$movie_result = mysql_query($movie_query,$link)
or die (mysql_error());

$movie_table_headings=<<<EOD
<tr>
<th>Movie Title</th>
<th>Year of Release</th>
<th>Movie Director</th>
<th>Movie Lead Actor</th>
<th>Movie Running Time</th>
<th>Movie Health</th>
</tr>
EOD;
function generate_ratings($review_rating)    {
    $movie_rating = '';
    for ($i=0;$i<$review_rating;$i++)    {
        $movie_rating .= "<img src=\"star.gif\"> ";
        }
    return $movie_rating;
    }

// echo $_GET['movie_id'];
$review_query = "SELECT * FROM reviews WHERE review_movie_id='" . mysql_real_escape_string($_GET['movie_id']) . "' ORDER BY review_date DESC"; 
        
$review_result = mysql_query($review_query,$link)
or die (mysql_error());


//while ($row = mysql_fetch_array($result))    {
while ($row = mysql_fetch_array($movie_result))    {
    $movie_name =$row['movie_name'];
    $movie_director = $row['movie_director'];
    $movie_leadactor =$row['movie_leadactor'];
    $movie_year= $row['movie_year'];
    $movie_running_time = $row['movie_running_time']."mins";
    $movie_takings=$row['movie_takings'];
    $movie_cost =$row['movie_cost'];
    }

//get director's name from people table
get_director();

//get lead actor's name from people table
get_leadactor();


/*Function to calculate if a movie made a profit, loss or broken even */


function calculate_differences($takings,$costs) {
    $difference = $takings - $costs;

if ($difference < 0)    {
    $difference = substr ($difference, 1);
    $font_color = 'red';
    $profit_or_loss = "S" . $difference. "m";
}   else if ($difference >0)    {
    $font_color = 'green';
    $profit_or_loss = "$" . $difference. "m";
}    else {
    $font_color = 'blue';
    $profit_or_loss= "Broke even";
}

return "<font color=\"$font_color\">" . $profit_or_loss. "</font>";
}
$movie_health = calculate_differences($movie_takings,$movie_cost);
$page_start=<<<EOD
<html>
<head>
<title> Details and Reviews for: $movie_name</title>
</head>
<body>
EOD;
if ($review_flag)    {
    $movie_details .=<<<EOD
<table width="95%" border="0" cellspacing="2"
    cellpadding="20" align="center">
    $review_table_headings
    $review_details
    </table>
EOD;
}



$movie_details =<<<EOD
<table width ="70%" border="0" cellspacing="2"
    cellpadding="2" align="center">
<tr>
    <th colspan="6"><u><h2>$movie_name: Details</h2></u></th>
</tr>
//$movie_table_headings
$review_table_headings=<<<EOD
<tr>
<th> Date of Review</th>
<th> Review Title</th>
<th> Reviewer Name</th>
<th> Movie Review Comments</th>
<th> Rating</th>
</tr>

<tr>
    <td width="33%" align="center">$movie_name</td>
    <td align="center">$movie_year</td>
    <td align="center">$director</td>
    <td align="center">$leadactor</td>
    <td align="center">$movie_running_time</td>
    <td align="center">$movie_health</td>
</tr>
</table>
<br>
<br>
EOD;
while($review_row = mysql_fetch_array($review_results))    {
    $review_flag=1;
    $review_title[] = $review_row['review_name'];
    $reviewer_name[] =ucwords($review_row['review_reviwer_name']);
    $review[]=$review_row['review_comment'];
    $review_date[] = $review_row['review_date'];
    $review_rating[] = generate_ratings($review_row['review_rating']);
    }

$i=0;
$review_details = '';
while ($i<sizeof($review))    {
    $review_details .=<<<EOD
    <tr>
        <td width="15%" valign="top" align="center">$review_daye[$i]</td>
        <td width="15%" valign="top">review_title[$i]</td>
        <td width="10%" valign="top">reviewer_name[$i]</td>
        <td width="50%" valign="top">reviwer[$i]</td>
        <td width="10%" valign="top" align="center">$review_rating[$i]</td>
        </tr>
EOD;
$i++;
}

$page_end =<<<EOD
</body>
</html>
EOD;
$detailed_movie_info =<<<EOD
    $page_start
    $movie_details
    $page_end
EOD;
echo $detailed_movie_info;
mysql_close();



?>

Научи се да си ескайпваш данните, защото ще те хакнат :)
 
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\AppServe\www\movie_details.php on line 158
// =<<


В момента съм в период на обучение..също бих те помолил ако можеш да оцветиш с конкретен цвят промените. Целта ми е да разбера и самата грешка, а не просто да го взема готово. Мерси :)))
 
Здравей,
Грешката ти идва от това, че се опитваш да завъртиш mysql_fetch_array на заявка, която не връща коректен резултат.
Т.е имаш грешка в заявката, която е на ред 66.
Сложи едно: echo $review_query;
под този ред и виж какво ще ти върне. Това което ти го върне го пусни в phpmyadmin-а и виж там какво ще ти покаже.
 
изкарва ми това SELECT * FROM reviews WHERE review_movie_id='1' ORDER BY review_date DESC

и след това в phpmyadmin си ми изкарва самата таблица с данните за review_movie_id=1
 
да, тук умишлено съм написал password.
Както казах бях търсил в гугъл и там се коментираше именно за тази завка query_review, ама уви..

ето тук - обаче не е от разстоянията..
 
Дай структурата на таблиците да пробвам локално, че така на пръв поглед нищо не виждам :)
 
createreviews.php


PHP:
<?php
$connect=mysql_connect("localhost","root","password")
	or die ("Hey loser, check your server connection");
mysql_select_db("moviesite");

$reviews = "CREATE TABLE reviews(
	review_movie_id int(11) NOT NULL,
	review_date date NOT NULL,
	review_name varchar(255) NOT NULL,
	review_reviewer_name varchar(255) NOT NULL,
	review_comment varchar(255) NOT NULL,
	review_rating int(11) NOT NULL default 0,
	KEY (review_movie_id)
	)";
	
$results = mysql_query($reviews)
	or die(mysql_error());
	
$insert = "INSERT INTO reviews 
	(review_movie_id, review_date, review_name, review_reviewer_name, review_comment, review_rating)
	VALUES
	('1','2003-08-02','This movie rocks!','John Doe','I thought this was a great nivue even though my girlfriend made me seeit against my will.','4'),
	('1','2003-08-01','An okay movie','Billy Bob','This was an okay movie. T like Eraserhead better.','2'),
	('1','2003-08-10','Woo hoo!','Peppermint Patty','Whis I\'d have seen it sooner!','5'),
	('2','2003-08-01','My favourite movie!','Marvin Marian','I didn\'t wear my flair to the movie but I loved it anyway.','5'),
	('3','2003-08-01','An awesome time','George B.','I liked this movie, even though i thought it was and informational video for our travel agent.','3')";
	
$insert_result=mysql_query($insert)
	or die(mysql_error());
	
?>


createmovie.php

PHP:
<?php 
$connect=mysql_connect("localhost","root","password") 
	or die ("Hey loser, check your server connection.");
$create=mysql_query("CREATE DATABASE IF NOT EXISTS moviesite")
	or die(mysql_error());
	
mysql_select_db("moviesite");

$movie = "CREATE TABLE movie (
movie_id int(11) NOT NULL auto_increment,
movie_name varchar(255) NOT NULL,
movie_type tinyint(2) NOT NULL default 0,
movie_year int(4) NOT NULL default 0,
movie_leadactor int(11) NOT NULL default 0,
movie_director int(11) NOT NULL default 0,
PRIMARY KEY (movie_id),
KEY movie_type (movie_type,movie_year)
)";

$results = mysql_query($movie)
	or die(mysql_error());
	
$movietype = "CREATE TABLE movietype (
movietype_id int(11) NOT NULL auto_increment,
movietype_label varchar(100) NOT NULL,
PRIMARY KEY (movietype_id)
)";

$results = mysql_query($movietype)
	or die(mysql_error());
	
$people = "CREATE TABLE people (
people_id int(11) NOT NULL auto_increment,
people_fullname varchar(255) NOT NULL,
people_isactor tinyint(1) NOT NULL default 0,
people_isdirector tinyint(1) NOT NULL default 0,
PRIMARY KEY (people_id)
)";

$results = mysql_query($people)
	or die(mysql_error());
	
echo "Movie Database successfully created!";

?>


moviedata.php

PHP:
<?php
$connect = mysql_connect("localhost","root","password")
	or die("Hey loser, check your server connection.");
	
mysql_select_db("moviesite");

$insert = "INSERT INTO movie (movie_id, movie_name, movie_type, ".
			"movie_year, movie_leadactor, movie_director)".
			"VALUES (1, 'Bruce Almighty', 5, 2003, 1, 2),".
			"(2, 'Office Space', 5, 1999, 5, 6),".
			"(3, 'Grand Canyon', 2, 1991, 4, 3)";
$results = mysql_query($insert)
	or die(mysql_error());
	
$type = "INSERT INTO movietype (movietype_id, movietype_label)".
			"VALUES ('1', 'Sci Fi'),".
			"('2', 'Drama'),".
			"('3', 'Adventure'),".
			"('4', 'War'),".
			"('5', 'Comedy'),".
			"('6', 'Horror'),".
			"('7', 'Action'),".
			"('8', 'Kids')";
$results = mysql_query($type)
	or die(mysql_error());
	
$people = "INSERT INTO people (people_id, people_fullname,".
			"people_isactor, people_isdirector)".
			"VALUES (1, 'Jim Carrey', 1, 0),".
			"(2, 'Tom Shadyac', 0, 1),".
			"(3, 'Lawrence Kasdan', 0, 1),".
			"(4, 'Kevin Kline', 1, 0 ),".
			"(5, 'Ron Livingston', 1, 0),".
			"(6, 'Mike Judge', 0, 1)";
			
$results = mysql_query($people)
	or die(mysql_error());
	
echo "Data inserted successfully!";
?>
 
Изтрий файловете от dox-a, че си оставил паролите в тях :)
Проблемите са няколко. Не знам дали са допуснати от теб или от книгата, но има из всички mysql_query-та едно $link накрая. Не трябва да го има. Махни го и ще се оправят. Освен това този ред, който дъни грешка:
while($review_row = mysql_fetch_array($review_results)) {
Грешката е от това s. По-нагоре в кода е $review_result, а не $review_results. Освен това имаш едни extract-ти, които не ти трябват в случая.
Това на пръв поглед ми се набива на очи от 5 мин разглеждане на кода.
Успех :)
 
мхм, грешката се махна..по инерция явно съм сложил S. Обаче явно нещо куца със заявката за ревютата, тъй като не иска да попълни таблицата с информация
 
Дебъгваш по същия начин -> print на заявката, която инсертва и после ръчно в phpmyadmin-а :)
 

Горе