【问题标题】:Wordpress wpdb->query problems with importing dataWordpress wpdb->导入数据的查询问题
【发布时间】:2014-11-24 12:50:29
【问题描述】:

我下面的代码行应该使用(在这种情况下)预定义的值来更新 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 表的屏幕截图。

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

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

如果我用 echo 替换 wpdb-> 查询,代码将正确执行并运行 echo 没有任何问题。

【问题讨论】:

  • 我现在正在讨论它是否会在日期中的“-”处中断。它会在 1994-06-04 破折号处中断吗?
  • 不,日期格式正确。您的专栏是healthy 日期还是常规varchar?您能否提供所有列的确切列类型?
  • 健康列是一个日期字段
  • 在$wpdb-&gt;query....之前和之后发布您的代码
  • 为什么不只是一个$wpdb-&gt;query( $wpdb-&gt;query( .. ) );而不是$wpdb-&gt;query( .. );?

标签: mysql database phpmyadmin wordpress


【解决方案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";
}
?>

【讨论】:

  • 非常感谢您为我的问题写下所有这些。恐怕它没有帮助。代码现在在我的页面上同时显示我的两个按钮,但它仍然没有更新我的数据库值。
  • 分别输入if (count($row2)&gt;0)和if (count($row1)&gt;0)。
  • Internal Server Error (500) 有很多不同的含义,可能与数据库查询本身无关。
  • 再一次,我真的很感谢你所有的时间,但恐怕它仍然不起作用。 Internet 服务器错误仅在我添加 wpdb 查询时显示。如果我用回显替换查询并保留所有其他代码(if 语句等),它仍然运行。
【解决方案2】:

如果您尝试直接访问这些文件,即http://example.com/wp-content/themes/stk/profile.php 和http://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

【讨论】:

  • 还要确保您已登录,否则 $user_ID 将为 0。
猜你喜欢
  • 2012-08-14
  • 2011-06-16
  • 2014-09-06
  • 2011-10-28
  • 1970-01-01
  • 2018-01-27
  • 2014-02-23
  • 1970-01-01
  • 2020-12-18
相关资源
最近更新 更多