UPDATE不使用数组

时间:2015-11-11 03:47:09

标签: php mysqli

我试图将存储在数组中的信息更新到数据库中。我似乎无法让它发挥作用。任何帮助将不胜感激。感谢。

在配置类中:

function update_cfg(array $upd)
{
    if(is_array($upd))
    {
        $sql = array();
        foreach($upd as $column => $info)
        {
            if(isset($info) && $column != 'update_cfg')
            {
              $sql[] = "`" . $column . "`='" . $info . "'";
            }
        }
        if(is_array($sql))
        {
            $result = $this->Sys->db->query("UPDATE `dj_settings` SET " . implode(', ', $sql));
            if($result)
            {
                unset($sql);
                return TRUE;
            }
            else
            {
                unset($sql);
                return FALSE;
            }
        }
    }
}

脚本:

<?php
global $Sys;

if(isset($_POST['update_cfg']))
{
    unset($_POST['update_cfg']);
    $update = $_POST;
    unset($_POST);
    if($Sys->Config->update_cfg($update))
    {
      $Sys->Template->setAlerts('The website has been successfully updated!');
    }
    else
    {
      $Sys->Template->setAlerts('The website has not been updated.', 'error');
    }
    unset($update);
}

?>
<div id="webCfg">
<h2>Website Configuration</h2>
  <?php

    global $Sys;
    global $handle;

    $alerts = $Sys->Template->getAlerts();
    if ($alerts != '') { echo '<div><ul class="alerts">' . $alerts . '</ul></div>'; }
    asort($handle);
    if($handle['status'] == 1)
    {
        $enable = 'checked="checked"';
    }
    else
    {
        $disable = 'checked="checked"';
    }
    foreach ($handle as $key => $val) 
    {
      $label = str_replace('_', ' ', $key);
      if(strlen($val) <= 50 && !is_numeric($val))
      {
        $input[$key] = '<tr><td class="label"><label for="' . $key . '">' . ucwords($label) . '</label></td><td class="content"><input type="text" id="' . $key . '" name="' . $key . '" value="' . $val . '"/></td></tr>';
      }
      elseif(strlen($val) >= 51 && !is_numeric($val))
      {
        $input[$key] = '<tr><td class="label"><label for="' . $key . '">' . ucwords($label) . '</label></td><td class="content"><textarea id="' . $key . '" name="' . $key . '">' . ucfirst($val) . '</textarea></td></tr>';
      }
      elseif(is_numeric($val))
      {
        $input[$key] = '<tr><td class="label"><label for="' . $key . '">Website Status</label></td><td class="content"><input type="radio" id="' . $key . '" name="' . $key . '" value="1" ' . $enable . '> Enable  <input type="radio" id="' . $key . '" name="' . $key . '" value="0" ' . $disable . '> Disable </td></tr>';
      }
    }
  ?>
  <form action="" method="post">
  <table cellpadding="1" cellspacing="0" id="webCfgTbl">
    <?php
      foreach (array_keys($input) as $key) 
      {
        echo '<div class="row">' . $input[$key] . '</div>';
      }
    ?>
    <tr><td class="label"><label for="submit"></label></td><td class="content"><div class="row submitrow"><input type="submit" id="submit" name="update_cfg" class="submit" value="Update Settings" /></div></td></tr>
  </table>
  </form>     
</div>

?>

页面加载数据库中的信息。当我更改值并单击“更新设置”时,$Sys->Config->update_cfg()每次都会返回FALSE。我对此感到困惑,并希望得到任何帮助。感谢。

修改

当我在foreach()循环之后在update_cfg()函数中回显查询时,我得到了这个:

UPDATE `dj_settings` SET `status`='0', `disabled_msg`=':: Test Message for Disabled Website ::', `email_auto_response`='Thank you so much for your email. I will respond back as soon as I am able. Usually within 30 minutes. Please feel free to look at the pictures and videos on our website, and the FAQ and Pricing tab is a great resource also. I will respond personally to any questions you may have shortly. Have a great day! Some tips on picking the right DJ: For most hiring a DJ is a first time experience. Picking the right DJ can be daunting and sometimes overwhelming task. Here are some good tips to follow to help you through your process. 1. Make sure they have their LLC or INC, and have a business license for your area. 2. Make sure they are insured. 3. Make sure that you sign some kind of a contract throughout the booking process. 4. Check reviews on reputable websites. ie.. thumbtack.com , weddingwire.com, or yellowpages.com. 5. If the price is too good to be true, it is. This is a big one, because you definitely get what you pay for when you hire a DJ. 6. Sit down with a couple of DJ's and talk them, generally your own intuition will get you far. If you follow those steps, you're sure to get a good DJ. No matter who it is, with my company or with a competitor. Your event will go off smoothly 99% of the time. Marshall Bracewell-Owner Dynamic DJ Company, LLC 803-807-1243 http://www.dynamicdjcompany.com', `web_url`='http://www.dynamicdjcompany.com', `web_email`='marshall@dynamicdjcompany.com'

1 个答案:

答案 0 :(得分:0)

嗯......这是一个开始我猜...至少更新正在发挥作用。首先尝试$info = $this->Sys->db->real_escape_string($info);然后将其放入"" . $column . "='" . $info . "'";