2010-03-27 80 views
0

我完全难住了。这里是我的PHP(笨)代码:mysql查询没有从应用程序内部正确运行

function mod() 
{ 
    $uid = $this->session->userdata('uid'); 
    $pid = $this->input->post('pid'); 
    if ($this->_verify($uid,$pid)) 
    { 
     $name = $this->input->post('name'); 
     $price = $this->input->post('price'); 
     $curr = $this->input->post('curr'); 
     $url = $this->input->post('url'); 

     $query = $this->db->query("UPDATE items SET 
         name=".$this->db->escape($name).", 
         price=".$this->db->escape($price).", 
         currency=".$this->db->escape($curr),", 
         url=".$this->db->escape($url)." 
         WHERE pid=".$this->db->escape($pid)." LIMIT 1"); 
    } 
    header('location: '.$this->session->userdata('current')); 

} 

这段代码的目的是修改在“项目”表中的一行的属性(名称,价格,币种,URL)(priary关键是pid) 。但是,由于某种原因,允许此函数运行一次后,将修改表中所有条目的名称,价格,货币和网址,而不管它们的pid和我在查询结尾处添加的LIMIT 1事物。就好像查询的最后一行被完全忽略。

仿佛这不是很奇怪,我换成“$query = $this->db->query(”与“echo”看到SQL查询正在运行,并且它多输出的查询像我期望:

UPDATE items 
    SET name = 'newname', 
     price = 'newprice', 
     currency = 'newcurrency', 
     url = 'newurl' 
WHERE pid = '10' 
LIMIT 1 

复制 - 将其存入MySQL窗口的行为与我想要的完全一样:它使用选定的pid修改该行。

这是怎么回事?

+0

我投入了限制1,看看它是否会有所作为。它继续修改所有行,尽管它让我更困惑。 ( – Mala 2010-03-27 23:27:12

回答

0

现在我感到很蠢:它只需要用不同的字体来看我的代码。我的代码有

currency=".$this->db->escape($curr),", 

,而不是

currency=".$this->db->escape($curr).", 

呼应使得它工作得很好,因为很明显,你可以给回声个以上的字符串,用逗号隔开,并连接它们

哭声我在这上面花了几个小时

0

我知道你回答了你自己的问题,但是让我把它添加到一堆:你在这类查询中不使用CodeIgniter AT ALL - 如果您按照预期使用CI,则不会出现该错字。您的查询应该是这样的(除其他事项外):

$query = $this->db->update('items', 
          array('name' => $this->input->post('name'), 
           'price' => $this->input->post('price'), 
           'curr' => $this->input->post('curr')), 
          array('id' => $this->input->post('id')), 
          1); 

通过组装手工查询字符串,你撤消什么CI为你做。只有当你使用一些复杂的JOIN语句时,你应该在CI中编写自己的SQL,即使这样,你也想使用PHP函数sprintf来确保你不会引入错别字。

+0

我对CI有点新,但是谢谢你的提示!我会从现在开始使用这个 – Mala 2010-03-28 23:07:56

+0

这是为什么这会被低估?我明确表示我在其实使用CodeIgniter,所以它是非常相关的。 – Mala 2011-04-18 05:47:05

+0

有仇敌,哟! – 2011-04-27 00:05:12