Jump to content

Php code not working


Recommended Posts

What's wrong with this code? There are values that match in the tables for each and if I only search by the date, it works fine. It's probably very obvious but I can't seem to pinpoint it.

 

elseif 

($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email', 'Appointment'))) {   
$thisyear = date('Y-m-d');     
$todotype = $_GET['type'];
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; 
}

Link to comment
Share on other sites

Thanks. Yes it is echo out, I just didn't put that part in. The error I'm getting is "supplied argument is not a valid MySQL result resource" I've checked all then table, row names and values and all checks out...

Can I see that code?  The mysql_error() part.

Link to comment
Share on other sites

as MAQ said, try this for testing purposes.

 

 

 

elseif (($_GET['reminder'] == 'thisyear') && (isset($_GET['type'])) && (in_array($_GET['type'], array('Call', 'Email', 'Appointment')))) {   
$thisyear = date('Y-m-d');     
$todotype = $_GET['type'];
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; 
$result = mysql_query($query) or die(mysql_error());

    print mysql_num_rows($query);
}

 

If successful you should get past that query and see the number of rows found.

Link to comment
Share on other sites

Yeah, why would that be, if I change it to the following it works fine:

 

($_GET['reminder'] == 'thisyear') {   
$thisyear = date('Y-m-d');     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) ORDER BY contacttodo.reminder ASC"; 

 

 

But when I do the full code it doesn't, here that is:

 

($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) {   
$thisyear = date('Y-m-d');     
$todotype = $_GET['type'];
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; 
}

Link to comment
Share on other sites

Then one of these conditions is not true.

($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) { 

 

Do a var_dump($_GET); and post the output.

Link to comment
Share on other sites

Thank you everyone for your help and patience with me.

 

If successful you should get past that query and see the number of rows found.

 

I used the code below, what you gave me and I had made a mistake when I input the code, what happend is it printed out 1 line with no information in it from the DB.

 

elseif ($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email', 'Appointment'))) {   
$thisyear = date('Y-m-d');     
$todotype = $_GET['type'];
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; 

$result = mysql_query($query) or die(mysql_error());    
print mysql_num_rows($query);}

 

alright post your full code YouSeriouslyNeedHelp.

 

Here is my full code:

    <?php
    $ID=$_GET['ID'];
    require("xxx.php");
    
if
($_GET['reminder'] == '') {
$today = date('Y-m-d', strtotime('today'));     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder = '$today' ORDER BY contacttodo.reminder ASC"; 
}

elseif
($_GET['reminder'] == 'lastweek') {
$lastweek = date('Y-m-d', strtotime('last sunday'));
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$lastweek' - INTERVAL 7 DAY AND '$lastweek' ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'thisweek') {     
$thisweek = date('Y-m-d', strtotime('last sunday'));     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$thisweek' AND '$thisweek' + INTERVAL 7 DAY ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'nextweek') {     
$nextweek = date('Y-m-d', strtotime('next sunday'));     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$nextweek' AND '$nextweek' + INTERVAL 7 DAY ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'thismonth') {     
$thismonth = date('Y-m-d');     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND MONTH(reminder) = MONTH(CURDATE())ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'thisquarter') {     
$thisquarter = date('Y-m-d');     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND QUARTER(reminder) = QUARTER(CURDATE())ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) {   
$thisyear = date('Y-m-d');     
$todotype = $_GET['type'];
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; 
}

    $result=mysql_query($query) or die(mysql_error()) ;
    while ($row = mysql_fetch_array($result)) {
    ?>
   <tbody>
    <tr> 
    <td width=""> <input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td>
    <td width=""><a href="/backend/contacts/editcontact.php?ID=<?php echo $row['ID']; ?>"><strong><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></strong></a></td>
    <td width=""><?php echo $row['phonecell']; ?> </td>
    <td width=""><a href="<?php echo $row['email']; ?>"><?php echo $row['email']; ?></a> </td>
    <td width=""><?php echo $row['contactstatus']; ?> </td>
    <td width=""><?php echo $row['contacttype']; ?> </td>
    <td width=""><?php echo $row['type']; ?> </td>
    <td><? echo date("F d, Y", strtotime($row['reminder'])); ?></td>
    <td><a href="/backend/contact/notes.php?ID=<? echo $row['tasknotes']; ?>">Notes</a></td>
    <td><a href="/backend/call-center/callcenter.php?ID=<? echo $row['ID']; ?>">Call</a></td>
    <td><a href="mailto:<? echo $row['email']; ?>">Email</a></td>
    <td><a href="/backend/contacts/editcontact.php?ID=<? echo $row['ID']; ?>">Edit</a></td>
    <td><a href="/backend/contacts/deletesuccess.php?ID=<? echo $row['ID']; ?>">Delete</a></td>
   </tr>
   </tbody>
   <?php  
    
    mysql_close();
    ?>

Link to comment
Share on other sites

Np, Alright you need to properly nest your if's and elseif's conditions, since it prints out 1 it means that the query is valid and there is a row found.

 

Try this

    <?php
    $ID=$_GET['ID'];
    require("xxx.php");
    
if
($_GET['reminder'] == '') {
$today = date('Y-m-d', strtotime('today'));     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder = '$today' ORDER BY contacttodo.reminder ASC"; 
}

elseif
($_GET['reminder'] == 'lastweek') {
$lastweek = date('Y-m-d', strtotime('last sunday'));
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$lastweek' - INTERVAL 7 DAY AND '$lastweek' ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'thisweek') {     
$thisweek = date('Y-m-d', strtotime('last sunday'));     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$thisweek' AND '$thisweek' + INTERVAL 7 DAY ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'nextweek') {     
$nextweek = date('Y-m-d', strtotime('next sunday'));     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND contacttodo.reminder BETWEEN '$nextweek' AND '$nextweek' + INTERVAL 7 DAY ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'thismonth') {     
$thismonth = date('Y-m-d');     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND MONTH(reminder) = MONTH(CURDATE())ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'thisquarter') {     
$thisquarter = date('Y-m-d');     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND QUARTER(reminder) = QUARTER(CURDATE())ORDER BY contacttodo.reminder ASC"; 
} 

elseif 

($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) {   
$thisyear = date('Y-m-d');     
$todotype = $_GET['type'];
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; 
}else{

print "No conditions Met";

}

    $result=mysql_query($query) or die(mysql_error()) ;
    while ($row = mysql_fetch_array($result)) {
    ?>
   <tbody>
    <tr> 
    <td width=""> <input type="checkbox" name="" id="" value="<?php echo $row['ID']; ?>"></td>
    <td width=""><a href="/backend/contacts/editcontact.php?ID=<?php echo $row['ID']; ?>"><strong><?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?></strong></a></td>
    <td width=""><?php echo $row['phonecell']; ?> </td>
    <td width=""><a href="<?php echo $row['email']; ?>"><?php echo $row['email']; ?></a> </td>
    <td width=""><?php echo $row['contactstatus']; ?> </td>
    <td width=""><?php echo $row['contacttype']; ?> </td>
    <td width=""><?php echo $row['type']; ?> </td>
    <td><? echo date("F d, Y", strtotime($row['reminder'])); ?></td>
    <td><a href="/backend/contact/notes.php?ID=<? echo $row['tasknotes']; ?>">Notes</a></td>
    <td><a href="/backend/call-center/callcenter.php?ID=<? echo $row['ID']; ?>">Call</a></td>
    <td><a href="mailto:<? echo $row['email']; ?>">Email</a></td>
    <td><a href="/backend/contacts/editcontact.php?ID=<? echo $row['ID']; ?>">Edit</a></td>
    <td><a href="/backend/contacts/deletesuccess.php?ID=<? echo $row['ID']; ?>">Delete</a></td>
   </tr>
   </tbody>
   <?php  
    
    mysql_close();
    ?>

Link to comment
Share on other sites

I would also suggest an 'else' statement in case you don't match anything.  Otherwise, you're going to run into the problem you have now.

 

Thanks, I took it out in an effort to figure this out. Although, my next question will be how to write an else statement that says, if nothing matches, print nothing...but first thing is I need to figure out why this query isn't working for "this year"

Link to comment
Share on other sites

Np, Alright you need to properly nest your if's and elseif's conditions, since it prints out 1 it means that the query is valid and there is a row found.

 

Okay...well that just drives me nuts because after I put in your code, I get the message "No conditions Met" however, in my database I have

 

in table contacts

 

ID=1

firstname=John

lastname=Smith

 

in table contacttodo

contacts id = 1

reminder = 08/29/2011

type = Call

 

and it is saying that I have no conditions that met...doesn't make any sense...

Link to comment
Share on other sites

Please read what you wrote here.  You added these 2 conditions, if the elseif is not executing then it's b/c of one or both of these.

isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))

 

Now, var_dump($_GET); so we can see what the value of 'type' is and figure out what part is not evaluating to true.

 

 

Yeah, why would that be, if I change it to the following it works fine:

 

($_GET['reminder'] == 'thisyear') {   
$thisyear = date('Y-m-d');     
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) ORDER BY contacttodo.reminder ASC"; 

 

 

But when I do the full code it doesn't, here that is:

 

($_GET['reminder'] == 'thisyear' && isset($_GET['type']) && in_array($_GET['type'], array('Call', 'Email'))) {   
$thisyear = date('Y-m-d');     
$todotype = $_GET['type'];
$query = "SELECT * FROM contacttodo,contacts WHERE contacttodo.contacts_id = contacts.ID AND YEAR(reminder) = YEAR(CURDATE()) AND contacttodo.type = '$todotype' ORDER BY contacttodo.reminder ASC"; 
}

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.