Jump to content

[SOLVED] SQL/PHP help


Jammokablammo

Recommended Posts

Hi,  I'm having a bit of trouble with some code, was wondering if anyone could spot something obvious i'm missing?

 

Code:

 

<?php

include_once('connection.php');

$username = $_POST['Username'];
$paswd = $_POST['Password'];
$count = 0;
$SQLID = '';
$SQLTE = '';
$SQLS = '';
$SQLCB = '';
$SQLTC = '';
$SQLP = '';
$blank = '';
$i = 0;

if ($_POST['UnitID'])
{
 $SQLID = "UnitID";
 $count++;
 echo $SQLID;
}

if ($_POST['TimeEntered'])
{
	 if ($count == 0)
 {
 		$SQLTE = "TimeEntered";
 }
 else
 {
 		$SQLTE = ", TimeEntered";
 } 
 $count++;
 echo $SQLTE;
}

if ($_POST['Stage'])
{
	 if ($count == 0)
 {
 		$SQLS = "Stage";
 }
 else
 {
 		$SQLS = ", Stage";
 } 
 $count++;
 echo $SQLS;
}

if ($_POST['CheckedBy'])
{
	 if ($count == 0)
 {
 		$SQLCB = "CheckedBy";
 }
 else
 {
 		$SQLCB = ", CheckedBy";
 } 
 $count++;
 echo $SQLCB;
}

if ($_POST['TimeChecked'])
{
	 if ($count == 0)
 {
 		$SQLTC = "TimeChecked";
 }
 else
 {
 		$SQLTC = ", TimeChecked";
 } 
 $count++;
 echo $SQLTC;
}

if ($_POST['Pass'])
{
	 if ($count == 0)
 {
 		$SQLP = "Pass";
 }
 else
 {
 		$SQLP = ", Pass";
 }
 $count++;
 echo $SQLP;
}

/*if (count == 0)
{
header("Location: reportManager.php");
}*/

$sql = ("SELECT " . $SQLID . $SQLTE . $SQLS . $SQLCB . $SQLTC . $SQLP .
" FROM Stages WHERE UnitID = '$UnitID'");
$data = mysql_query($sql);

for ($i = 0; $i < $count; $i++)
{
      $result = mysql_fetch_array( $data );
      if (strcmp($SQLID,$blank) != 0)
      echo $result['UnitID'];
      if (strcmp($SQLTE,$blank) != 0)
      echo $result['TimeEntered'];
      if (strcmp($SQLS,$blank) != 0)
      echo $result['Stage'];
      if (strcmp($SQLCB,$blank) != 0)
      echo $result['CheckedBy'];
      if (strcmp($SQLTC,$blank) != 0)
      echo $result['TimeChecked'];
      if (strcmp($SQLP,$blank) != 0)
      echo $result['Pass'];
}
?>

 

The if statements are definitely accessed, as if i change the echo $result['blah']; to just echo a string, it works no problem.  So i'm pretty certain that there's a problem with $result, but all the things in the square brackets (teaching myself php so i don't really know what the technical term for anything is i'm afraid!) are the names of the fields in the database.

 

Also, if i uncomment the if(count == 0) part, it always redirects to reportManager.php, even if count is equal to 6!  I really can't get my head around that!

 

Any help would be lovely!

Link to comment
Share on other sites

I'm not really sure what you're asking for. You didn't tell us the problem..

 

The reason when you comment out the if statement it still redirects is because you didn't comment out the redirect. A comment is basically like erasing it. So if you don't have the if statement, it'll always do it.

Link to comment
Share on other sites

  • 2 weeks later...

Sorry folks, i went on holiday the day after i started this thread, and i was wrongly informed that there would be internet there!  Now then...

 

are you mean if($count == 0)

 

Sasa you little genius!  It's always the little things i get wrong!

That's fixed the redirect problem, but i'm still struggling with the data i'm accessing from the database.  The idea is that for every checked checkbox, the sql statement will include that field.  So if the checkbox is unchecked, it will just refer to an empty string.

 

So the sql statement is built with the line:

 

$sql = ("SELECT " . $SQLID . $SQLTE . $SQLS . $SQLCB . $SQLTC . $SQLP .

" FROM Stages WHERE UnitID = '$UnitID'");

 

and i believe the query is then carried out by:

 

$data = mysql_query($sql);

 

Then the for loop:

 

for ($i = 0; $i < $count; $i++)

{

      $result = mysql_fetch_array( $data );

      if (strcmp($SQLID,$blank) != 0)

      echo $result['UnitID'];

 

and so on.  So as far as i can tell, for every checked checkbox, the loop should run through once.  and then the strings are compared, so in this case, if $SQLID isn't an empty string, then $result['UnitID'] should be echoed to the screen, which is where i get the problem.  If i tell it to echo a normal string to screen, this works no problem, but echo $result['UnitID']; doesn't do a thing and i don't know why.  UnitID is definately the name of the field in the database.

 

Anyone got any ideas please?

Link to comment
Share on other sites

Echo your actual query before executing it.  Make sure that the fields you are pulling from the database include "UnitID"... and make sure that is the actual case of it as well.

 

I don't really know what you mean...

 

How do i echo the actual query?  And what do you mean by "make sure that is the actual case of it as well."?

Link to comment
Share on other sites

Before:

 

$data = mysql_query($sql);

 

Do this:

 

echo $sql;

 

When I'm talking about case, the names of your fields in the database are case-sensitive.  So if you named the field "unitID" in the database and you are calling it out of the array with "UnitID", it's not going to find it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.