2010-03-27 77 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