2009-04-27 11 views
6

मैं इस तरह बाध्यकारी चर का उपयोग करने का प्रयास करें:उपयोग एक bind_param() इनपुट के चर संख्या के साथ वार्स

$stmt = $mysqli->prepare("UPDATE mytable SET myvar1=?, myvar2=... WHERE id = ?")) { 
$stmt->bind_param("ss...", $_POST['myvar1'], $_POST['myvar2']...); 

लेकिन की $ _POST [ '...'] कुछ खाली हो सकता है तो मैं डॉन ' टी उन्हें डीबी में अपडेट करना चाहते हैं।

खाली $ _POST ['...'] के सभी अलग-अलग संयोजनों को ध्यान में रखना व्यावहारिक नहीं है और हालांकि मैं अपनी ज़रूरतों के लिए "अद्यतन mytable SET ..." स्ट्रिंग का निर्माण कर सकता हूं, bind_param() एक है अलग जानवर

मैं एक स्ट्रिंग और उस पर उपयोग eval() के रूप में अपनी कॉल के निर्माण की कोशिश कर सकते हैं लेकिन यह महसूस नहीं करता है सही :(

उत्तर

-2

एक स्ट्रिंग के रूप में यह निर्माण, लेकिन एक सरणी में अपने मूल्यों डाल दिया और करने के लिए है कि पारित bindd_param। (और स्थानापन्न? 'अपने एसक्यूएल स्ट्रिंग में मूल्यों के लिए रों।

$ stmt = $ mysqli-> तैयार ("अद्यतन mytable सेट myvar1 = ?, myvar2 = ... कहां आईडी =?")) { $ stmt-> bind_param ("एस एस ...", $ _POST [ 'myvar1'], $ _POST [ 'myvar2'] ...);

उदाहरण के लिए:

$args = array(); 
$sql = "UPDATE sometable SET "; 
$sep = ""; 
$paramtypes = ""; 
foreach($_POST as $key => $val) { 
    $sql .= $sep.$key." = '?'"; 
    $paramtypes .= "s"; // you'll need to map these based on name 
    array_push($args, $val); 
    $sep = ","; 
} 
$sql .= " WHERE id = ?"; 
array_push($args, $id); 
array_insert($args, $paramtypes, 0); 

$stmt = $mysqli->prepare($sql); 
call_user_func_array(array(&$stmt, 'bindparams'), $array_of_params); 
$stmt->bind_param($args); 
+1

@altCongnito 'array_insert() वास्तव में मौजूद है? –

+0

आपने सरणी – pmrotule

1

यह मामूली अधिक एक सरणी का उपयोग कर अपने बयान के निर्माण के लिए स्पष्ट है:

$params = array(); 
$fragments = array(); 
foreach($_POST as $col => $val) 
{ 
    $fragments[] = "{$col} = ?"; 
    $params[] = $val; 
} 

$sql = sprintf("UPDATE sometable SET %s", implode(", ", $fragments)); 
$stmt = $mysqli->prepare($sql); 
$stmt->bind_param($params); 
+0

+1 के बजाय '$ sql + =' लिखा है क्या उपयोगकर्ता को फ़ील्ड को अपडेट करने के लिए निर्दिष्ट करना सुरक्षित है? मैं बस सोच रहा हूं, अगर वह उपयोगकर्ता तालिका थी, और उपयोगकर्ता किसी भी तरह से पता चला कि वहां एक कॉलम था जो उनके उपयोगकर्ता विशेषाधिकारों को संग्रहीत करता था, तो वे सही कुंजी/मूल्य संयोजन के साथ फ़ॉर्म पोस्ट सबमिट करके आसानी से अपने उपयोगकर्ता विशेषाधिकारों को बढ़ा सकते थे। – cgp

+0

का उपयोग करने के लिए '$ sql = = – Calvin

+2

किसी भी परिस्थिति में उपयोगकर्ता को फ़ील्ड निर्दिष्ट करने के लिए अच्छा नहीं है। कम से कम उसके पास $ valid_fieldnames सरणी होनी चाहिए और जांच करें कि (जारीकर्ता ($ valid_fieldnames [$ col])) foreach के शरीर के रूप में। (मैं ऐरे ('फ़ील्ड' => 1, 'फ़ील्ड 2' => 1, ...) को ऐरे ('फील्ड', 'फील्ड 2', ...) पर जारी करता हूं क्योंकि जारीकर्ता() in_array() से तेज़ है।) – jmucchiello

3

यह है कि मैं क्या mysqli पैरामीटर के एक चर राशि के साथ विवरण तैयार करने के लिए इस्तेमाल करते हैं। यह मैंने लिखा एक वर्ग का हिस्सा है। यह आपके लिए आवश्यकतानुसार अधिक है, लेकिन यह आपको सही दिशा दिखाना चाहिए।

public function __construct($con, $query){ 
    $this->con = $con; 
    $this->query = $query; 
    parent::__construct($con, $query); 
    //We check for errors: 
    if($this->con->error) throw new Exception($this->con->error); 
} 

protected static $allowed = array('d', 'i', 's', 'b'); //allowed types 

protected static function mysqliContentType($value) { 
    if(is_string($value)) $type = 's'; 
    elseif(is_float($value)) $type = 'd'; 
    elseif(is_int($value)) $type = 'i'; 
    else throw new Exception("type of '$value' is not string, int or float"); 
    return $type; 
} 

//This function checks if a given string is an allowed mysqli content type for prepared statement (s, d, b, or i) 
protected static function mysqliAllowedContentType($s){ 
    return in_array($s, self::$allowed); 
} 

public function feed($params){ 
    //These should all be empty in case this gets used multiple times 
    $this->paramArgs = array(); 
    $this->typestring = ''; 
    $this->params = $params; 
    $this->paramArgs[0] = ''; 
    $i = 0; 
    foreach($this->params as $value){ 
     //We check the type: 
     if(is_array($value)){ 
      $temp = array_keys($value); 
      $type = $temp[0]; 
      $this->params[$i] = $value[$type]; 
      if(!self::mysqliAllowedContentType($type)){ 
       $type = self::mysqliContentType($value[$type]); 
      } 
     } 
     else{ 
      $type = self::mysqliContentType($value); 
     } 
     $this->typestring .= $type; 
     //We build the array of values we pass to the bind_params function 
     //We add a refrence to the value of the array to the array we will pass to the call_user_func_array function. Thus say we have the following 
     //$this->params array: 
      //$this->params[0] = 'foo'; 
      //$this->params[1] = 4; 
     //$this->paramArgs will become: 
      //$this->paramArgs[0] = 'si'; //Typestring 
      //$this->paramArgs[1] = &$this->params[0]; 
      //$this->paramArgs[2] = &$this->params[1]. 
     //Thus using call_user_func_array will call $this->bind_param() (which is inherented from the mysqli_stmt class) like this: 
      //$this->bind_param('si', &$this->params[0], &$this->params[1]); 
     $this->paramArgs[] = &$this->params[$i]; 
     $i++; 
    } 
    unset($i); 
    $this->paramArgs[0] = $this->typestring; 
    return call_user_func_array(array(&$this, 'bind_param'), $this->paramArgs); 
} 

आप इस तरह इसका इस्तेमाल:

$prep = new theClassAboveHere($mysqli, $query); 
$prep->feed(array('string', 1, array('b', 'BLOB DATA')); 

वर्ग mysqli_stmt वर्ग का विस्तार करना चाहिए।

मुझे आशा है कि यह आपको सही दिशा में मदद करेगा।
यदि आप नहीं चाहते हैं कि मैं पूरी कक्षा भी पोस्ट कर सकता हूं, इसमें परिवर्तनीय परिणाम बाध्यकारी शामिल हैं।

+1

कृपया पूरी कक्षा पोस्ट करें। – svenkapudija

24

आप call_user_func_array समारोह इस्तेमाल कर सकते हैं एक चर नंबर या तर्क के साथ bind_param विधि कॉल करने के:

$paramNames = array('myvar1', 'myvar2', /* ... */); 
$params = array(); 
foreach ($paramNames as $name) { 
    if (isset($_POST[$name]) && $_POST[$name] != '') { 
     $params[$name] = $_POST[$name]; 
    } 
} 
if (count($params)) { 
    $query = 'UPDATE mytable SET '; 
    foreach ($params as $name => $val) { 
     $query .= $name.'=?,'; 
    } 
    $query = substr($query, 0, -1); 
    $query .= 'WHERE id = ?'; 
    $stmt = $mysqli->prepare($query); 
    $params = array_merge(array(str_repeat('s', count($params))), array_values($params)); 
    call_user_func_array(array(&$stmt, 'bind_param'), $params); 
} 
+1

बस यह उल्लेख करना चाहता था कि PHP 5.3+ में उपर्युक्त कोड काम नहीं करेगा, क्योंकि इसे संदर्भ के रूप में सरणी मानों की आवश्यकता होती है। देखें: http://php.net/manual/de/mysqli-stmt.bind-param.php – Alex2php

+0

इसके बजाय 'bind_values' का उपयोग करें। – Gumbo

+0

मुझे यह MYSQLi में कहां मिल सकता है? मुझे केवल पीडीओ से पता है। – Alex2php

-1

array_insert मौजूद नहीं है, मैं अनुमान लगा रहा हूँ वह कुछ घर बनाया समारोह को संदर्भित करता है, लेकिन मैं कर रहा हूँ यह सुनिश्चित नहीं है कि यह वास्तव में क्या करता है ... शुरुआत में कहीं भी सरणी पर पैरामीटर प्रकार डालें, मुझे लगता है कि मान 0 पास हो गया है, लेकिन यह भी अंत में हो सकता है;)

+3

हम्म ... यह उत्तर की तुलना में एक टिप्पणी की तरह दिखता है? – kleopatra

संबंधित मुद्दे