Issue with SQL statement

Hi I have a form with a checkbox where people can select student’s names that they wish to see their profiles for. The code looks like this:

[code]

<?php do { ?>
<p>
  <input name="UID[]" type="checkbox" id="UID" value="<?php echo $row_applicants['UID']; ?>" />
  <label for="UID"></label>
  <?php echo $row_applicants['FNAME']; ?> <?php echo $row_applicants['LNAME']; ?> </p>
<?php } while ($row_applicants = mysql_fetch_assoc($applicants)); ?>

[/code]

Once they submit the form, the array is sent to another page which will show all the profiles selected. Here is the code for that page:

[code]

<?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $ID = $_POST['ID']; } mysql_select_db($database_connect, $connect); $idList = implode(',', $_POST['UID']); $sql = 'SELECT *, scholarships.NAME as scholarshipname FROM scholarships, gc_codes, users, activities, coll_exp, emply, finaid, fin_app, stu_app2008, stu_special WHERE gc_codes.GCID = users.GC stu_special.ID = $ID and scholarships.ID = stu_special.ID and UID in (' . $idList . ')'; $res = mysql_query($sql); $varlist = array(); while ($row = mysql_fetch_assoc($res)) { $varlist[$row['UID']] = $row; } ?> td img {display: block;} td img {margin: 0;} .break { page-break-before: always; } Cambridge Rindge & Latin School | 2012 Scholarship Standard Application <?php foreach ($varlist as $UID => $row) do { ?>
<?php echo $row_your_scholarships['FNAME']; ?> <?php echo $row_your_scholarships['LNAME']; ?> | <?php echo $_GET['NAME']; ?>

2012 Scholarship Standard Application
          <tr>
            <td colspan="7" valign="top"><table width="98%" border="0" align="center" cellpadding="5" cellspacing="2">
              <tr>
                <td width="200%" colspan="2" valign="top"><fieldset id="el11"><legend><strong>Section 1: Applicant Information</strong></legend>
                  <table width="100%" border="0" cellspacing="1" cellpadding="6">
                    <tr bgcolor="#cccccc">
                      <td width="33%" valign="top"><strong>Name: </strong><?php echo $row['FNAME']; ?>&nbsp;<?php echo $row['LNAME']; ?></strong></font></strong></td>
                      <td valign="top"><strong>Gender: </strong><?php echo $row['GENDER']; ?></strong></font></strong></td>
                      <td valign="top"><strong>SID: </strong><?php echo $row['SID']; ?></strong></font></strong></td>
                      </tr>
                    <tr>
                      <td colspan="2" valign="top" bgcolor="#FFFFFF"><strong>Address: </strong><?php echo $row['ADDRESS']; ?></strong></font></strong></td>
                      <td valign="top" bgcolor="#FFFFFF"><strong>Homeroom: </strong><?php echo $row['HR']; ?></strong></font></strong></td>
                      </tr>
                    <tr bgcolor="#cccccc">
                      <td valign="top"><strong>Telephone: </strong><?php echo $row['TEL']; ?></strong></font></strong></td>
                      <td valign="top"><strong>LC: </strong><?php echo $row['LC']; ?></strong></font></strong></td>
                      <td valign="top"><strong>GC: </strong><?php echo $row['NAME']; ?></strong></font></strong></td>
                      </tr>
                    <tr>
                      <td valign="top" bgcolor="#FFFFFF"><strong>Email: </strong><?php echo $row['UNAME']; ?></strong></font></strong></td>
                      <td colspan="2" valign="top" bgcolor="#FFFFFF"><strong>Elementary School:</strong> <?php echo $row['ELEM']; ?></td>
                      </tr>
                    </table></fieldset></td>
                </tr>
              <tr>
                <td colspan="2" valign="top"><fieldset id="el11">
                  <legend><strong>Section 2: Parent / Guardian Information</strong></legend><table width="100%" border="0" cellspacing="1" cellpadding="6">
                    <tr>
                      <td colspan="2" valign="top"><strong>PARENT / GUARDIAN #1</strong></td>
                      <td colspan="2" valign="top" bgcolor="#cccccc"><strong>PARENT / GUARDIAN #2</strong></td>
                      </tr>
                    <tr>
                      <td width="19%" valign="top"><strong>Name: </strong></td>
                      <td width="30%" valign="top"><label for="Name"><?php echo $row['PARENT_A']; ?></label></td>
                      <td width="19%" valign="top" bgcolor="#cccccc"><strong>Name: </strong></td>
                      <td width="30%" valign="top" bgcolor="#cccccc"><?php echo $row['PARENT_B']; ?></td>
                      </tr>
                    <tr>
                      <td valign="top"><strong>Address:</strong></td>
                      <td valign="top"><?php echo $row['PAR_A_ADD']; ?></td>
                      <td valign="top" bgcolor="#cccccc"><strong>Address:</strong></td>
                      <td valign="top" bgcolor="#cccccc"><?php echo $row['PAR_B_ADD']; ?></td>
                      </tr>
                    <tr>
                      <td valign="top"><strong>Telephone:</strong></td>
                      <td valign="top"><?php echo $row['PAR_A_TEL']; ?></td>
                      <td valign="top" bgcolor="#cccccc"><strong>Telephone:</strong></td>
                      <td valign="top" bgcolor="#cccccc"><?php echo $row['PAR_B_TEL']; ?></td>
                      </tr>
                    <tr>
                      <td valign="top"><strong>Relationship to Applicant:</strong></td>
                      <td valign="top"><?php echo $row['PAR_A_REL']; ?></td>
                      <td valign="top" bgcolor="#cccccc"><strong>Relationship to Applicant:</strong></td>
                      <td valign="top" bgcolor="#cccccc"><?php echo $row['PAR_B_REL']; ?></td>
                      </tr>
                    <tr>
                      <td valign="top"><strong>Occupation:</strong></td>
                      <td valign="top"><?php echo $row['PAR_A_OCC']; ?></td>
                      <td valign="top" bgcolor="#cccccc"><strong>Occupation:</strong></td>
                      <td valign="top" bgcolor="#cccccc"><?php echo $row['PAR_B_OCC']; ?></td>
                      </tr>
                    </table>
                  </fieldset></td>
                </tr>
              <tr>
                ......
                     [/code]

The UID field that is used is an integer that is unique to each student. There is only one profile per student. There error message that I get when viewing the results page is:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Scholarship\admin\printable_version2_test3.php on line 87

Which tells me there is an error in my sql statement. I am not sure though what it is. Thanks in advance for all your help/

Without knowing what your database schema looks like, I would proceed as follows:

Output the $sql variable, check what the query is and then execute this directly against your database. Does this return the results you expect?

Yes, the SQL select part of the statement is fine. I think the error is in this part:

$res = mysql_query($sql); $varlist = array(); while ($row = mysql_fetch_assoc($res)) { $varlist[$row['UID']] = $row;

In the body of the page I also have this code to make it loop through all the UIDs so it shows everyone’s profiles:

<body><?php foreach ($varlist as $UID => $row) do { ?>

Thanks again.

You do not need a do() in your foreach loop.

[php]
foreach ($varlist as $UID => $row) {
}
[/php]

Is all you need

Thanks so much. I will remove it. I am still having the same issue as before. Do you see any errors in the following code:

$ID = $_POST['ID']; } mysql_select_db($database_connect, $connect); $idList = implode(',', $_POST['UID']); $sql = 'SELECT *, scholarships.NAME as scholarshipname FROM scholarships, gc_codes, users, activities, coll_exp, emply, finaid, fin_app, stu_app2008, stu_special WHERE gc_codes.GCID = users.GC stu_special.ID = $ID and scholarships.ID = stu_special.ID and UID in (' . $idList . ')'; $res = mysql_query($sql); $varlist = array(); while ($row = mysql_fetch_assoc($res)) { $varlist[$row['UID']] = $row;

If mysql_query is returning a boolean value there is an error in your query. Try adding some error reporting to this line.

[php]$res = mysql_query($sql)or die(mysql_error());[/php]

I just did what you said and it said this:

Warning: mysql_pconnect(): MySQL server has gone away in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Connections\connect.php on line 9 Column ‘UID’ in where clause is ambiguous

More than one of the tables that you are selecting from has a column UID - you will therefore need to prefix this field in your where clause with table.

Thanks so much for your help. I have added the prefix for the field and now have the following code:

$ID = $_POST['ID']; } mysql_select_db($database_connect, $connect); $idList = implode(',', $_POST['UID']); $sql = "SELECT *, scholarships.NAME as scholarshipname FROM scholarships, gc_codes, users, activities, coll_exp, employ, finaid, fin_app, stu_app2008, stu_special WHERE gc_codes.GCID = users.GC and stu_special.ID = $ID and scholarships.ID = stu_special.ID and users.UID in (' . $idList . ') and stu_app2008.UID in (' . $idList . ')"; $res = mysql_query($sql) or die(mysql_error()); $varlist = array(); while ($row = mysql_fetch_assoc($res)) { $varlist[$row['UID']] = $row;

I don’t get any error messages now but, I do get a blank screen which seems to tell me that there is no records found. Hmmm. If I explain the database setup, maybe you can let me know where I went wrong in the sql statement.

So, there are several tables in the database, one for activities, one for employment, on for financial aid, etc. A student may or may not have a record in one of these tables depending on if they worked or had financial aid, etc. There is also a table called users and stu_app2008 and everyone will have 1 record in both of these tables. So, when they submit the form, it passes the UID for the student to the next page and then based on the UID, it needs to display the students profile from the users and stu_app2008 tables and also any data from the other tables if the data exists. UID is a field in all of the tables. All of the fields from each table will be displayed in the profile if the data exists. I did try a join sql statement and that didn’t work either. Here was the code for that statement:

SELECT *, scholarships.NAME as scholarshipname FROM scholarships, gc_codes, users  INNER JOIN stu_app2008 ON users.UID = stu_app2008.UID  INNER JOIN activities ON users.UID = activities.UID INNER JOIN coll_exp ON users.UID = coll_exp.UID  INNER JOIN employ ON users.UID = employ.UID INNER JOIN fin_app ON users.UID = fin_app.UID  INNER JOIN finaid ON users.UID = finaid.UID  INNER JOIN stu_special ON users.UID = stu_special.UID WHERE gc_codes.GCID = users.GC and scholarships.ID = stu_special.ID and users.UID in (' . $idList . ')'

Thanks again for solving my mystery. :slight_smile:

I am still having issues. Ugh!

I think the SQL statement is ok but, maybe the problem relies in the following code:

Here is the SQL statement:

[code]$ID = $_POST[‘ID’];
}
mysql_select_db($database_connect, $connect);
$idList = implode(’,’, $_POST[‘UID’]);
$sql = “SELECT *, scholarships.NAME as scholarshipname FROM scholarships, users INNER JOIN stu_app2008 ON users.UID = stu_app2008.UID LEFT JOIN activities ON users.UID = activities.UID LEFT JOIN coll_exp ON users.UID = coll_exp.UID LEFT JOIN employ ON users.UID = employ.UID LEFT JOIN fin_app ON users.UID = fin_app.UID LEFT JOIN finaid ON users.UID = finaid.UID LEFT JOIN stu_special ON users.UID = stu_special.UID WHERE scholarships.ID = $ID and users.UID in (’ . $idList . ')”;
$res = mysql_query($sql) or die(mysql_error());
$varlist = array();
while ($row = mysql_fetch_assoc($res)) {
$varlist[$row[‘UID’]] = $row;

}[/code]

Here is the code around the information that i want it to display for each record:

[code]<?php
foreach ($varlist as $UID => $row)
do { ?>

<?php } while ($row = mysql_fetch_assoc($res)); ?> [/code]

Any help would be so appreciated!!

Sponsor our Newsletter | Privacy Policy | Terms of Service