Wordpress wpdb->查询导入数据的问题

时间:2014-11-24 12:50:30

标签: mysql database wordpress phpmyadmin wpdb

下面的代码行应该用(在这种情况下)预定义的值更新NULL值字段。当我执行我的wpdb查询但页面给出500错误。

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

有人可以看一下代码行,可能会告诉我什么是错的?

点击按钮时正在执行代码。

添加了我的wp表的屏幕截图。

enter image description here

人员ID和健康日期将是动态的,但现在我保持静态。

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";
}

我应该更换wpdb-&gt;使用echo查询代码将正确执行并运行echo而不会出现任何问题。

2 个答案:

答案 0 :(得分:1)

尝试重新编写逻辑,例如:

<强> 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
}
?>

现在,另一个文件,不要忘记全球化$wpdb变量:

ziekbeter.php (已编辑):

<?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";
}
?>

答案 1 :(得分:0)

如果您尝试直接访问这些文件,例如http://example.com/wp-content/themes/stk/profile.phphttp://example.com/wp-content/themes/stk/ziekbeter.php,则需要包含WordPress:

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

// rest of profile.php


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

// rest of ziekbeter.php
相关问题