PHP Syntax Errors....Help, Please!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PatheticAtPhp
Forum Newbie
Posts: 4
Joined: Wed Sep 12, 2007 12:03 pm

PHP Syntax Errors....Help, Please!

Post by PatheticAtPhp »

Hello everyone,

I am new here, and just signed up today. I need help figuring out an assignment I have for school. Here is what I was supposed to do:

1. Create a Web page using PHP to connect to the MySQL database and showing all the records within an HTML table.
2. Next, create a Web page that shows one record from the same database (using the primary key as an identifier).
3. Finally, create a link from the catalog page to the detail page that passes the primary key to the detail page for each record as needed.

In our lecture material, there was a tutorial for this, but it was done very badly and did not work for anyone in the class who tried it out. Don't ask me why because I am new to this, and I couldn't figure it out. Most of the other folks in my class had some php experience, but I had no experience coming into this class so I am basically lost. I managed to create a static catalog page based on the assignment code, as well as the page that has one record only on it. The lecture tutorial didn't call for a page including every record, though, nor one for the one record only page, but I created them anyway. Here they are......I'm first including the URL and then the PHP behind the page: Catalog page: http://vaf533.aisites.com/Catalog.php

Here is the code I used to create this particular page:

Code: Select all

<?php require_once('Connections/classes_assignment.php'); ?>
<?php
$maxRows_Recordsetclasses = 10;
$pageNum_Recordsetclasses = 0;
if (isset($_GET['pageNum_Recordsetclasses'])) {
  $pageNum_Recordsetclasses = $_GET['pageNum_Recordsetclasses'];
}
$startRow_Recordsetclasses = $pageNum_Recordsetclasses * $maxRows_Recordsetclasses;

mysql_select_db($database_classes_assignment, $classes_assignment);
$query_Recordsetclasses = "SELECT classes.classid, classes.catalogid, classes.classtitle, classes.instructor, classes.`day`, classes.`time`, classes.description FROM classes";
$query_limit_Recordsetclasses = sprintf("%s LIMIT %d, %d", $query_Recordsetclasses, $startRow_Recordsetclasses, $maxRows_Recordsetclasses);
$Recordsetclasses = mysql_query($query_limit_Recordsetclasses, $classes_assignment) or die(mysql_error());
$row_Recordsetclasses = mysql_fetch_assoc($Recordsetclasses);

if (isset($_GET['totalRows_Recordsetclasses'])) {
  $totalRows_Recordsetclasses = $_GET['totalRows_Recordsetclasses'];
} else {
  $all_Recordsetclasses = mysql_query($query_Recordsetclasses);
  $totalRows_Recordsetclasses = mysql_num_rows($all_Recordsetclasses);
}
$totalPages_Recordsetclasses = ceil($totalRows_Recordsetclasses/$maxRows_Recordsetclasses)-1;
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Catalog Page</title>
</head>

<body>
<table border="1" cellpadding="2" cellspacing="2">
  <tr>
    <td>classid</td>
    <td>catalogid</td>
    <td>classtitle</td>
    <td>instructor</td>
    <td>day</td>
    <td>time</td>
    <td>description</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_Recordsetclasses['classid']; ?></td>
      <td><?php echo $row_Recordsetclasses['catalogid']; ?></td>
      <td><?php echo $row_Recordsetclasses['classtitle']; ?></td>
      <td><?php echo $row_Recordsetclasses['instructor']; ?></td>
      <td><?php echo $row_Recordsetclasses['day']; ?></td>
      <td><?php echo $row_Recordsetclasses['time']; ?></td>
      <td><?php echo $row_Recordsetclasses['description']; ?></td>
    </tr>
    <?php } while ($row_Recordsetclasses = mysql_fetch_assoc($Recordsetclasses)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($Recordsetclasses);
?>
Ok, now here is the URL to the static one record page: http://vaf533.aisites.com/Single_Record.php

And here is the PHP code I used for that one:

Code: Select all

<?php require_once('Connections/classes_assignment.php'); ?>
<?php
$maxRows_rsClasses = 10;
$pageNum_rsClasses = 0;
if (isset($_GET['pageNum_rsClasses'])) {
  $pageNum_rsClasses = $_GET['pageNum_rsClasses'];
}
$startRow_rsClasses = $pageNum_rsClasses * $maxRows_rsClasses;

mysql_select_db($database_classes_assignment, $classes_assignment);
$query_rsClasses = "SELECT * FROM classes WHERE classes.classid=4";
$query_limit_rsClasses = sprintf("%s LIMIT %d, %d", $query_rsClasses, $startRow_rsClasses, $maxRows_rsClasses);
$rsClasses = mysql_query($query_limit_rsClasses, $classes_assignment) or die(mysql_error());
$row_rsClasses = mysql_fetch_assoc($rsClasses);

if (isset($_GET['totalRows_rsClasses'])) {
  $totalRows_rsClasses = $_GET['totalRows_rsClasses'];
} else {
  $all_rsClasses = mysql_query($query_rsClasses);
  $totalRows_rsClasses = mysql_num_rows($all_rsClasses);
}
$totalPages_rsClasses = ceil($totalRows_rsClasses/$maxRows_rsClasses)-1;

?>
<html>
<title>Displaying One Record</title>
<style type="text/css">
<!--
.style1 {font-family: Verdana, Arial, Helvetica, sans-serif}
.style2 {color: #009933}
body {
	background-color: #CCFFFF;
}
-->
</style>
<body>
<h3 align="center" class="style1 style2">Displaying One Record from the Database </h3>
<p align="center" class="style1">Show me all the classes where the class ID is #4.</p>
<table border="1" align="center" bordercolor="#006633">
  <tr>
    <td align="center" bgcolor="#99CC99">Class Id</td>
    <td align="center" bgcolor="#99CC99">Course Number</td>
    <td align="center" bgcolor="#99CC99">Title</td>
    <td align="center" bgcolor="#99CC99">Instructor</td>
    <td align="center" bgcolor="#99CC99">Day</td>
    <td align="center" bgcolor="#99CC99">Time</td>
    <td align="center" bgcolor="#99CC99">Description</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_rsClasses['classid']; ?></td>
      <td><?php echo $row_rsClasses['catalogid']; ?></td>
      <td><?php echo $row_rsClasses['classtitle']; ?></td>
      <td><?php echo $row_rsClasses['instructor']; ?></td>
      <td><?php echo $row_rsClasses['day']; ?></td>
      <td><?php echo $row_rsClasses['time']; ?></td>
      <td><?php echo $row_rsClasses['description']; ?></td>
    </tr>
    <?php } while ($row_rsClasses = mysql_fetch_assoc($rsClasses)); ?>
</table>
</body>
</html>

<?php
mysql_free_result($rsClasses);
?>
I really didn't have a huge problem with these two pages, but when I tried to recreate the tutorial from the lecture to produce the "Index page" and the "details page" that connects to it (part 3 of the assignment listed above) I ran into some huge issues. Like I said, the tutorial was VERY BAD and everyone in the class said it did not work. I am going to have to produce a similar code that passes information from a catalog page in my website that I am building for this class, and if I cannot get this tutorial correct then I doubt I will be able to reproduce it in the manner I need to for the project, as it is basically the same concept.

Here is the so called "Index page" that was created using the lecture tutorial: http://vaf533.aisites.com/IndexPage.php You should be able to click on the class name and the description is supposed to pop up on the details page. However, when you click on mine all you get is this: http://vaf533.aisites.com/detail.php?classid=6

Here is the php code behind the "Index page" from the tutorial....I had to fix some of this just to get this to work, so this is NOT the original code from the tutorial....It was quite a headache for me, even though this should be a simple PHP page with repeating regions:

Code: Select all

<?php require_once('Connections/classes_assignment.php'); ?>
<?php
mysql_select_db($database_classes_assignment, $classes_assignment);
$query_rsClasses = "SELECT classid, catalogid, classtitle FROM classes";
$rsClasses = mysql_query($query_rsClasses, $classes_assignment) or die(mysql_error());
$row_rsClasses = mysql_fetch_assoc($rsClasses);
$totalRows_rsClasses = mysql_num_rows($rsClasses);
?>
<table width="70%" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="12%">&nbsp;</td>
<td width="36%">Catalog ID Number</td>
<td width="26%">Class Name</td>
<td width="26%">&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td>&nbsp;</td>
<td><?php echo $row_rsClasses['catalogid'];?></td>
<td><a href="detail.php?classid=<?php echo $row_rsClasses['classid']; ?>"><?php echo $row_rsClasses['classtitle']; ?></a></td>
<td>&nbsp;</td>
</tr>
<?php } while ($row_rsClasses = mysql_fetch_assoc($rsClasses));?>
</table>

Now here is the php code behind the "details page" that is supposed to display the class description information when you click on the class name:

Code: Select all

<?php require_once('Connections/classes_assignment.php'); ?>
<?php
$colname_rsClassDetail ="1";
if (isset($HTTP_GET_VARS['classid'])) {
$colname_rsClassDetail = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS
['classid'] : addslashes($HTTP_GET_VARS['classid']);
}
mysql_select_db($database_classes_assignment, $classes_assignment);
$query_rsClassDetail = sprintf("SELECT * FROM classes WHERE classid = %s", $colname_ClassDetail);
$query_rsClassDetail = mysql_query($query_rsClassDetail, $classes_assignment) or die(mysql_error());
$row_rsClassDetail = mysql_fetch_assoc($rsClassDetail);
$totalRows_rsClassDetail = mysql_num_rows($rsClassDetail);
?>
<html>

This page came EXACTLY from the lectures, but I just cannot get this to work in conjunction with my Index page, and this is really getting to me. I have been working on this for 3 days and just cannot figure out what the problem is. I asked my professor for help, but she just told me to methodically build the pages over and over until I get it right, which I have done probably fifteen times having to fix the flawed code that the school provided each time, to no avail!!! Any helpful suggestions would be greatly appreciated!!! I only have a week and a half until I have to have my final project in for this class and I am not sure I will be able to get this right if I don't have some help!

Thanks in advance for anyone's help!!!
Last edited by PatheticAtPhp on Thu Sep 13, 2007 7:08 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It says you have an SQL syntax error. Print out $query_rsClassDetail and see what the problem is.
(#10850)
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

First glance ...
$query_rsClassDetail = sprintf("SELECT * FROM classes WHERE classid = %s", $colname_ClassDetail);
Shouldn't that be

Code: Select all

$query_rsClassDetail = sprintf("SELECT * FROM classes WHERE classid = %s", $colname_rsClassDetail);
PatheticAtPhp
Forum Newbie
Posts: 4
Joined: Wed Sep 12, 2007 12:03 pm

You're right, and thanks....BUT:

Post by PatheticAtPhp »

YES, you are right. Thank you! Now that I have fixed that, though, I am getting errors on lines 11 and 12. Here is what the error reads:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /whs2fs1_3us4_2/vaf533aii/domains/vaf533.aisites.com/public_html/detail.php on line 11

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /whs2fs1_3us4_2/vaf533aii/domains/vaf533.aisites.com/public_html/detail.php on line 12


I really wish my school had included an actual PHP class in my curriculum. We have only lightly touched on PHP/SQL in one previous class, and my classes are so quick that I didn't get a good grasp of it from the two week crash course they gave me several months back. Then I had to go straight into another set of classes (5½ weeks) where the pressure to learn new concepts was so great that I didn't have a chance to bone up on the PHP/SQL I barely learned from the other class!!! Now I am expected to build this E-Commerce site using it, and I am lost! There should have been a class dedicated specifically to PHP and SQL syntax, etc.... and in my opinion, it should have been more than 5½ weeks, or at the very least should have been ALL PHP and SQL, and not a hodge podge mix like the one course I took that had PHP/SQL as a section!!! I appreciate your help so much!!!!!

Thanks!!!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Those errors would suggest a syntax error in your query itself.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

Right before ...
$query_rsClassDetail = mysql_query($query_rsClassDetail, $classes_assignment) or die(mysql_error());
... insert the line ...

Code: Select all

echo $query_rsClassDetail;
... as suggested previously by arborint and post back the results.
Post Reply