PHP - How to update data if no data is available or give an error

Asked By Curious To learn on 20-Mar-12 02:34 AM
Hi I have a query program where a user logs a query and when they log their query they get a query id and their username and query get logged at well as the date and time. I also have another form which updates the query when it is attended too or when it is closed which updates the rest of the feilds in that row. So now what im trying to do is that if the query is already been attended too it must give an error if another person trys to attend the same query bt if it hasnt been attended to then it must update the remaining feilds
$select = ("SELECT Count(*)as numrows FROM table WHERE approved=1 and Attendby='".$attend."'and queryId='".$id."'" );
$row = mysql_fetch_assoc($select);//mysql_query($select);
 if($row['numrows']> 0)
 {
   echo"query already attended";
  }
else
{
  $call = intval($_POST[queryId]);
$sql="UPDATE tableSET CustomerDetails='$details',Department='$dept',AttendDate='$NewDate',AttendTime='$NewTime',CallType='$type ',Attendby='$attend ',QueryStatus='$status' Where QueryId= $id";
//header('Location:page.html');
}
this is the code i have and it seems to not works
Jitendra Faye replied to Curious To learn on 20-Mar-12 02:36 AM
For this, Before Updating record first check that data is available or not.

if data is not available then update command will generate error.

Curious To learn replied to Jitendra Faye on 20-Mar-12 02:41 AM
yes im doing the check before i update but it seems to update anyway even if there is data in that field already, oh and i want it to give an error if there is data in the feilds its trying to update please help me with some code examples because im still trying to find my feet around php
Somesh Yadav replied to Curious To learn on 20-Mar-12 02:48 AM
hi refer to the below link

http://php.net/manual/en/function.mysql-error.php

I hope it helps you.
Curious To learn replied to Somesh Yadav on 20-Mar-12 02:58 AM
Hi Somesh its not what im really looking for but thanx for the help
RayMoonDay replied to Curious To learn on 04-Apr-12 06:10 AM
Your select count(*) query is checking with the Attendby='".$attend."'. So basically it is checking if the query is attended by who ever is mentioned in $attend variable. Rather, if you change that query to check if the Attendby is not NULL, the it will fetch results

$select = ("SELECT Count(*)as numrows FROM table WHERE approved=1 and Attendby <> NULL and queryId='".$id."'" ); 
Curious To learn replied to RayMoonDay on 04-Apr-12 09:35 AM
Thanks RayMoonDay but i have already solved it