Reply
mysql query help
Old 04-11-2007, 09:06 PM mysql query help
digitalfusion's Avatar
Skilled Talker

Posts: 64
Name: Martin
Location: Fife, Scotland
Ok i've been working on my website for a while now and just started to add a client management system to it which i'm building myself.

At the moment i'm about half way through building it, but have got to a problem when trying to display current projects.

I have created the following mysql queries to select and display the information i need displayed buti'm either getting mysql_fetch_array is an invalid myqsql resource or when i added the or die(""); part i get my mesage and the second querie seems to die aswell.

This is the code:
PHP Code:
<?php 
     
include('functions.php');
     
dbconnect();
     
$complete="complete";
     
$uncomplete="In Progress";
     
      
// query database for completed projects
$uncomplete=mysql_query("SELECT * FROM projects WHERE projectstatus=$uncomplete") or die ("I am not currently working on any projects!");
while (
$row2 mysql_fetch_array($uncompleteMYSQL_ASSOC)) {

// display projects found by query
echo "<table width='100%'><tr><td class='sideheader'><strong>" .$row2['projectname']."</strong></td></tr>";
echo 
"<tr><td class='sidecontent'><strong>Project Description: </strong>".$row2['projetcdesc']."<br />";
echo 
"<strong>Project URL: </strong><a class='links' href='".$row2['projecturl']."' alt='".$row2['projectname']."'>".$row2['projectname']."</a></td></tr>";
echo 
"</table>";
}
?>
      <h3><strong>Finished Projects       </strong></h3>
<?php
// query database for completed projects
$completed=mysql_query("SELECT * FROM projects WHERE projectstatus=$complete") or die ("There are no Completed Projects at this time!");
while (
$row1 mysql_fetch_array($completedMYSQL_ASSOC)) {

// display projects found by query
echo "<table width='100%'><tr><td class='sideheader'><strong>" .$row1['projectname']."</strong></td></tr>";
echo 
"<tr><td class='sidecontent'><strong>Project Description: </strong>".$row1['projetcdesc']."<br />";
echo 
"<strong>Project URL: </strong><a class='links' href='".$row1['projecturl']."' alt='".$row1['projectname']."'>".$row1['projectname']."</a></td></tr>";
echo 
"</table>";
}
?>
Any help would be greatly appreciated

Regards,
Martin
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
When You Register, These Ads Go Away!
     
Old 04-11-2007, 09:36 PM Re: mysql query help
vivekar's Avatar
Webmaster Talker

Posts: 531
when you are using strings in SQL, you should either use a single quote' or double quote ".

try like this
Code:
"SELECT * FROM projects WHERE projectstatus='" . $uncomplete . "'"
Caution: using the same variable for a string and a SQL query will make debugging very hard and code will become difficult to manage in the long run.

Best practice:
Code:
$incomplete = 'In Progress';

$sql =  "SELECT * FROM projects WHERE projectstatus='" . $incomplete . "'"
Hope that helps.
vivekar is offline
Reply With Quote
View Public Profile Visit vivekar's homepage!
 
Old 04-12-2007, 09:35 AM Re: mysql query help
digitalfusion's Avatar
Skilled Talker

Posts: 64
Name: Martin
Location: Fife, Scotland
Quote:
Originally Posted by vivekar View Post
when you are using strings in SQL, you should either use a single quote' or double quote ".

try like this
Code:
"SELECT * FROM projects WHERE projectstatus='" . $uncomplete . "'"
Caution: using the same variable for a string and a SQL query will make debugging very hard and code will become difficult to manage in the long run.

Best practice:
Code:
$incomplete = 'In Progress';

$sql =  "SELECT * FROM projects WHERE projectstatus='" . $incomplete . "'"
Hope that helps.
Thank you very much, tried your suggestion and it now works like a charm.

Regards,
Martin
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-12-2007, 10:58 AM Re: mysql query help
digitalfusion's Avatar
Skilled Talker

Posts: 64
Name: Martin
Location: Fife, Scotland
Ok i've hit another probem this time in a script i'm building to update the projects in the database.

I have three php scripts to do this one to select the project to be updated the other to show the current data and the third to handle the changed data in the form.

But i'm the query seems to die because of an error in teh query i suspect.

The query requires the variable $projectid=$_REQUEST['projectid']; so it can select the correct row to update in the database. but i tried commenting out the query and just echoing the variable and it seems to not print so maybe there is an error in it being posted from the second script to the third script. I've tried putting in a hidden input field with the projectid in it and it still doesn;t seem to pick it up.

The scripts are as follows:

Script 1 (updateproject.php):
PHP Code:
<h3>Project Management </h3>
  <fieldset><legend>Project Lookup</legend><form action="uproject.php" method="post" enctype="multipart/form-data" name="findproject" id="findproject">
    <label><strong>Project Name:
    </strong>
    <select name="projectid" id="projectname">
    <?php
    
require('functions.php');
   
dbconnect();
   
   
$query"SELECT * FROM projects ORDER BY projectname ASC";
   
$result mysql_query($query);
   while(
$row=mysql_fetch_array($result)) {
   echo 
"<option value=\"{$row['projectid']}\">{$row['projectname']}</option>\n";
   }
    
?>
    </select>
    </label>
    <p>
      <input name="findproject" type="submit" id="findproject" value="Find Project" />
    </p>
  </form></fieldset>
Script 2 (uproject.php):
PHP Code:
<h3>Project Management </h3>
  <?php
 
require('functions.php');
dbconnect();

// get variables
$projectname=$_REQUEST['projectname'];
$projectid=$_REQUEST['projectid'];
  
  
// Delete client from database using form data
$sql "SELECT * FROM `projects` WHERE projects.projectid=$projectid;";
$result=mysql_query($sql);
if(
$result==0){
    echo 
"<p align='center'>There has been a problem!</p><br />";
} else {
while (
$row=mysql_fetch_array($resultMYSQL_ASSOC)) {
    echo 
"<strong>Project ID: </strong>" .$row['projectid']. "<br />"
    echo 
"<input name='projectid' type='hidden' value='".$row['projectid']."' />";
    echo 
"<form action='editproject.php' method='post' enctype='multipart/form-data' name='editproject'><br /><br />";
    echo 
"<strong>Project Name: </strong><input name='projectname' type='text' value='".$row['projectname']."' /><br /><br />";
    echo 
"<strong>Project URL: </strong><input name='projecturl' type='text' value='".$row['projecturl']."' /><br /><br />";
    echo 
"<strong>Project Status: </strong><input name='projectstatus' type='text' value='".$row['projectstatus']."' /><br /><br />";
    echo 
"<strong>Project Decription: </strong><textarea name='projectdesc' cols='40' rows='10' >".$row['projectdesc']."</textarea><br /><br />";
    echo 
"<input name='editproject' type='submit' id='editproject' value='Update Project'  />";
    echo 
"</form>";
    }
}

  
?>
Script 3 (editproject.php):
PHP Code:
<h3>Project Management </h3>
  <?php
 
require('functions.php');
dbconnect();

// get variables
$projectname=$_REQUEST['projectname'];
$projectid=$_REQUEST['projectid'];
$projectstatus=$_REQUEST['projectstatus'];
$projectdesc=$_REQUEST['projectdesc'];
$projecturl=$_REQUEST['projecturl'];
 
  
// Delete client from database using form data
$sql "UPDATE `projects` SET projectid=Null, projectname='".$projectname."', projectstatus='".$projectstatus."', projectdesc='".$projectdesc."', projecturl='".$projecturl."' WHERE projects.projectid=$projectid;";
$result=mysql_query($sql);
if(
$result==0){
    echo 
"<p align='center'>There has been a problem!</p><br />";
} else {
    echo 
"The project details have been successfully updated!";
}

  
?>
Any suggestions would be greatly appreciated

Regards,
Martin
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-12-2007, 11:44 AM Re: mysql query help
iulyfigm's Avatar
Junior Talker

Posts: 4
I don't understand why did you wrote here ...projects.projectid=$projectid;"; meaning $projectid; with ';'

try with
...projects.projectid='$projectid'";
iulyfigm is offline
Reply With Quote
View Public Profile
 
Old 04-12-2007, 12:57 PM Re: mysql query help
digitalfusion's Avatar
Skilled Talker

Posts: 64
Name: Martin
Location: Fife, Scotland
Quote:
Originally Posted by iulyfigm View Post
I don't understand why did you wrote here ...projects.projectid=$projectid;"; meaning $projectid; with ';'

try with
...projects.projectid='$projectid'";
Thanks for your help but that doesn't seem to be the problem it seems to be the variable that is the problem.

But i don't know what is wrong
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-12-2007, 01:48 PM Re: mysql query help
iulyfigm's Avatar
Junior Talker

Posts: 4
try to put $result=mysql_query($sql)or die("SQL ERROR:".mysql_error());
and if it is a sql error you will see.

Last edited by iulyfigm : 04-12-2007 at 01:51 PM.
iulyfigm is offline
Reply With Quote
View Public Profile
 
Old 04-12-2007, 02:10 PM Re: mysql query help
digitalfusion's Avatar
Skilled Talker

Posts: 64
Name: Martin
Location: Fife, Scotland
Quote:
Originally Posted by iulyfigm View Post
try to put $result=mysql_query($sql)or die("SQL ERROR:".mysql_error());
and if it is a sql error you will see.
Ok i done what you suggested and got the following error message:

SQL ERROR: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 '' at line 1
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-12-2007, 02:19 PM Re: mysql query help
digitalfusion's Avatar
Skilled Talker

Posts: 64
Name: Martin
Location: Fife, Scotland
ok i have fixed the mysql error now so the query works, turns out just needed to change projects.projectid='".$projectid."'"; to that part of the sql statement. But now have an even bigger problem. I'm back to my original assumption that the $projectid variable isn;t being received from teh previous script properly and therefore the query is properly constructed but can't update the row because the projectid is needed and isn't being sent.

Any help with that is greatly appreciated
Regards,
Martin
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-12-2007, 08:58 PM Re: mysql query help
mgraphic's Avatar
Truth Seeker

Posts: 2,308
Name: Keith Marshall
Location: West Hartford, CT
Do an echo on your var to see the value of the string, or you can display all vars by inserting print_r($GLOBALS);

You will then see what vars do and do not have a value.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-12-2007, 09:08 PM Re: mysql query help
digitalfusion's Avatar
Skilled Talker

Posts: 64
Name: Martin
Location: Fife, Scotland
Quote:
Originally Posted by mgraphic View Post
Do an echo on your var to see the value of the string, or you can display all vars by inserting print_r($GLOBALS);

You will then see what vars do and do not have a value.
I tried echoing my $projectid variable already and it's not displaying anything thats why i thought there was a problem with it and not the query.

The thing is i can't see what may be wrong with the variable.

Regards,
Martin
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-12-2007, 09:20 PM Re: mysql query help
mgraphic's Avatar
Truth Seeker

Posts: 2,308
Name: Keith Marshall
Location: West Hartford, CT
If your var is being set in another script file, is that file being included and the var is being set before you get to the point to the db row insertion?
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-12-2007, 09:43 PM Re: mysql query help
digitalfusion's Avatar
Skilled Talker

Posts: 64
Name: Martin
Location: Fife, Scotland
no the var is $projectid=$_REQUEST['projectid'];

it collects data from the form in the other script

Regards,
Martin
__________________
---------------------------------------------------
www.tornadodesigns.co.uk www.teenium.com www.kirkgandss.com www.worldofmuscle.co.uk
digitalfusion is offline
Reply With Quote
View Public Profile Visit digitalfusion's homepage!
 
Old 04-12-2007, 10:16 PM Re: mysql query help
mgraphic's Avatar
Truth Seeker

Posts: 2,308
Name: Keith Marshall
Location: West Hartford, CT
OK - I see in your script sample above when you echo your form html from the php in the previous script, that the input is outside your html form tags. They need to be inside the nest to be considered part of the form data.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 04-13-2007, 09:56 AM