0

My line of code below is supposed to update a NULL value field with (in this case) a pre-defined value. When i execute my wpdb query however the page gives a 500 error.

$wpdb->query( $wpdb->query( "UPDATE ziekbeter SET healthy= '1994-06-04' WHERE person = 5 AND sick IS NOT NULL AND healthy IS NULL") );

Can someone take a look at the line of code and possibly tell me whats wrong?

The code is being executed on a button click.

A screenshot of my wp table is added.

enter image description here

Person ID and the healthy date are going to be dynamic but for now im keeping it static.

enter image description here

profile.php

<?php
$user_ID = get_current_user_id();
echo $user_ID;
global $wpdb;

if ($wpdb->get_results( "SELECT * FROM ziekbeter WHERE person = $user_ID AND healthy IS NULL"))
{
    $row = $wpdb->get_results( "SELECT * FROM ziekbeter WHERE person = $user_ID AND healthy IS NULL");
    {
        ?>
            <form action="<?php bloginfo('url'); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
                    <input type="submit" name="submitBeter" value="Meld mij beter!">
            </form>
        <?php
    } 
}
elseif ($wpdb->get_results("SELECT healthy FROM ziekbeter WHERE person = $user_ID AND healthy IS NOT NULL"))
{
    $row = $wpdb->get_results( "SELECT * FROM ziekbeter WHERE person = $user_ID AND healthy IS NOT NULL");
    {
        ?>
            <form action="<?php bloginfo('url'); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
                    <input type="submit" name="submitZiek" value="Meld mij ziek!">
            </form>
        <?php
    } 
}
else {
        ?>
            <form action="<?php bloginfo('url'); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
                    <input type="submit" name="submitZiek" value="Meld mij ziek!">
            </form>
        <?php
}
?>

ziekbeter.php

if(isset($_POST['submitZiek']))
{
     /* This function will come after i got the submitBeter working */
}
elseif(isset($_POST['submitBeter']))
{
     $wpdb->query( $wpdb->query( "UPDATE ziekbeter SET healthy= '1994-06-04' WHERE person = 5 AND sick IS NOT NULL AND healthy IS NULL") );

     echo "submitBeter wordt uitgevoerd";
}

Should i replace the wpdb-> query with an echo the code will execute properly and run the echo without any problems.

6
  • Im now debating whether or not it might break on the '-' in the date.. Could it break on the 1994-06-04 dashes? Nov 24, 2014 at 12:55
  • No, the date is in proper format. Is your column healthy date or regular varchar? Could you provide the exact column types for all columns? Nov 24, 2014 at 12:58
  • column healthy is a date field Nov 24, 2014 at 13:04
  • Post your code before and after $wpdb->query.... Nov 24, 2014 at 13:11
  • Instead of $wpdb->query( $wpdb->query( .. ) ); why not just one $wpdb->query( .. );? Nov 24, 2014 at 13:41

2 Answers 2

1

Try reworking the logic, something like:

profile.php

<?php
global $wpdb;
$user_ID = get_current_user_id();
echo $user_ID;

//First DB query
$row1 = $wpdb->get_results("SELECT * FROM ziekbeter WHERE person = $user_ID AND healthy IS NULL");
//Second DB query
$row2 = $wpdb->get_results("SELECT healthy FROM ziekbeter WHERE person = $user_ID AND healthy IS NOT NULL");

 // I.e. greater than zero draw the HTML form
 if (count($row1)>0) {

 ?>
  <form action="<?php esc_url(bloginfo('url')); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
     <input type="submit" name="submitBeter" value="Meld mij beter!">
  </form>
 <?php
    } 

  // Second DB query draw different HTML form
  if (count($row2)>0)   {
  ?>
  <form action="<?php esc_url(bloginfo('url')); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
     <input type="submit" name="submitZiek" value="Meld mij ziek!">
  </form>
<?php

}

// Draw third HTML form otherwise
if (!$row1 || !$row2) {
?>
<form action="<?php esc_url(bloginfo('url')); ?>/wp-content/themes/stk/ziekbeter.php" method="post">
   <input type="submit" name="submitZiek" value="Meld mij ziek!">
 </form>
<?php
}
?>

Now, the other file, do not forget to globalize the $wpdb variable:

ziekbeter.php (EDITED):

<?php
global $wpdb;
if(isset($_POST['submitZiek']))  {
     /* This function will come after i got the submitBeter working */
}




if(isset($_POST['submitBeter']))  {
     $result = $wpdb->query( "UPDATE ziekbeter SET healthy= '1994-06-04' WHERE person = 5 AND sick IS NOT NULL AND healthy IS NULL");
    /*Or, use the native WordPress function:
    $result = $wpdb->update( $table, $data, $where, $format = null, $where_format = null ); */     
     if ($result) echo "submitBeter wordt uitgevoerd";
}
?>
5
  • Thanks a lot for writing all this for my question. I'm afraid it doesnt help though. The code now shows both my buttons at the same time on my page but it also still doesnt update my database values. Nov 24, 2014 at 13:38
  • Put if (count($row2)>0) and if (count($row1)>0) respectively. Nov 24, 2014 at 13:44
  • Internal Server Error (500) can means a lot of different things, it may not be related to the DB query itself. Nov 24, 2014 at 13:53
  • Once again i really apreciate all your time but im afraid it still doesnt work. The internetal server error only shows when i add the wpdb query. If i replace the query with an echo and leave all the other code (if statements etc) it still runs. Nov 24, 2014 at 13:54
  • Let us continue this discussion in chat. Nov 24, 2014 at 13:56
0

If you're trying to access those files directly, i.e. http://example.com/wp-content/themes/stk/profile.php and http://example.com/wp-content/themes/stk/ziekbeter.php, then you need to include WordPress:

<?php
require_once '../../../wp-load.php';

// rest of profile.php


<?php
require_once '../../../wp-load.php';

// rest of ziekbeter.php
1
  • Also make sure you're logged in or $user_ID will be 0. Nov 24, 2014 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.