2012-05-02 278 views
0

我有一個函數從$ _POST函數接收數組,然後使用索引和包含在索引中的值來創建SQL。我的問題是,我可以讓函數正確地回顯SQL,但我無法創建一個變量。我的作用是低於使用變量創建SQL

function createcontactsArray($sql,Array $contactsArray){ 
     //array has already been cleaned from sql injections 

     //delete null variables and the value of the submit button   
     foreach ($contactsArray as $key => $value) { 

      if($value == ""||$value=="continue") { 
       unset($contactsArray[$key]); 
      } 

     } 

     echo "INSERT INTO users("; 
     //create list of tables to use in the database 
     foreach ($contactsArray as $key => $value) { 

      if ($value == end($contactsArray))    { 
       echo $key; 
      } else    { 
       echo $key.","; 
      } 

     } 
     echo ') VALUES ('; 

     //create list of tables to use in the database 
     //$newcontactsArray = array_values($contactsArray); 
     foreach ($contactsArray as $key => $value) { 

      if ($value == end($contactsArray))    { 
       echo '"'.$value.'"'; 
      } else    { 
       echo '"'.$value.'"'.","; 
      } 

     } 

     echo ');'; 

}

如果您運行此腳本,並把它傳遞例如$contacts = array("name"=>"Peter griffin","town"=>"Quahogn");關聯數組它將輸出以下INSERT INTO users (name,contacts) VALUES ("Peter griffin","Quahog")。不過,我想要的功能創建一個SQL像$sql = INSERT INTO users (name,contacts) VALUES ("Peter griffin","Quahog"),以便輸出我只是說echo $sql;謝謝。

回答

0
function createcontactsArray($sql,Array $contactsArray){ 
     //array has already been cleaned from sql injections 

     //delete null variables and the value of the submit button   
     foreach ($contactsArray as $key => $value) { 

      if($value == ""||$value=="continue") { 
       unset($contactsArray[$key]); 
      } 

     } 

     $sql = "INSERT INTO users("; 
     //create list of tables to use in the database 
     foreach ($contactsArray as $key => $value) { 

      if ($value == end($contactsArray))    { 
       $sql .= $key; 
      } else    { 
       $sql .= $key.","; 
      } 

     } 
     $sql .= ') VALUES ('; 

     //create list of tables to use in the database 
     //$newcontactsArray = array_values($contactsArray); 
     foreach ($contactsArray as $key => $value) { 

      if ($value == end($contactsArray))    { 
       $sql .= '"'.$value.'"'; 
      } else    { 
       $sql .= '"'.$value.'"'.","; 
      } 

     } 

     $sql .= ');'; 

     return $sql; 
+0

@ PLB謝謝。這實際上起作用。你是個救世主。 – sammyukavi

+0

不客氣。在這種情況下,你應該接受這個答案。 – Leri

1

只是不回聲所有的部分,但收集他們在一個字符串變量。因此,而不是:

echo 'Text'; 
echo $variable; 

不喜歡

$output = 'Text'; 
$output .= $variable; 

東西在函數結束返回的輸出與

return $output; 

注意.=串接與新的前值。

0

這裏是正確的方法。 安全和清潔

function dbSet($fields,$source=array()) { 
    global $mysqli; 
    if (!$source) $source = &$_POST; 
    $set=''; 
    foreach ($fields as $field) { 
    if (isset($source[$field])) { 
     $set.="`$field`='".mysqli_real_escape_string($mysqli,$source[$field])."', "; 
    } 
    } 
    return substr($set, 0, -2); 
} 

像這樣使用

$query = "UPDATE $table SET ".dbSet(array("name","contacts")); 

注意,你應該總是硬編碼允許的字段名,而不是從$ _POST讓他們,或網站會在幾秒鐘之內被砍死。

用mysql這個函數可以用於INSERT或UPDATE查詢。

+0

Wooohooo ....男人..你是我的常識。它的工作非常棒。 – sammyukavi

+0

它也是非常安全的。您應該始終對字段名進行硬編碼,不要從POST中獲取它們,否則網站將在幾秒鐘內被黑客入侵。如果它是類的一部分,請使用$ this-> conn而不是$ mysqli –

+0

返回substr($ set,0,-2);'' – sammyukavi

0
function createcontactsArray($sql,Array $contactsArray){ 
     //array has already been cleaned from sql injections 
     $sql = ''; 
     //delete null variables and the value of the submit button   
     foreach ($contactsArray as $key => $value) { 

      if($value == ""||$value=="continue") { 
       unset($contactsArray[$key]); 
      } 

     } 

     $sql .= "INSERT INTO users("; 
     //create list of tables to use in the database 
     foreach ($contactsArray as $key => $value) { 

      if ($value == end($contactsArray))    { 
       $sql .= $key; 
      } else    { 
       $sql .= $key.","; 
      } 

     } 
     $sql .= ') VALUES ('; 

     //create list of tables to use in the database 
     //$newcontactsArray = array_values($contactsArray); 
     foreach ($contactsArray as $key => $value) { 

      if ($value == end($contactsArray))    { 
       $sql .= '"'.$value.'"'; 
      } else    { 
       $sql .= '"'.$value.'"'.","; 
      } 

     } 

     $sql .= ');'; 

     echo $sql; 
+0

OOwwww ....由於懶惰的Internet連接,我得到了否定的投票 –

+0

不用擔心。我用積極的態度拯救了你,讓你成爲零。您的代碼有效 – sammyukavi

+0

謝謝@Ukavi。很高興分享 –