This class is working perfectly on single array but when i add multidimensional array dont working.i create the class which automatically get column name and values.here is demo code array coming like this.
class myform{
public $key;
public $value;
public $query;
public $con;
public function __construct(){
$this->con = new mysqli('localhost','root','','form')or die('you are losing your mind');}
public function form_values($tablename,$values){
$columns = implode(", ",array_keys($value));
$escaped_values = array_map('esc_sql', array_values($value));
$values = implode("',' ", $escaped_values);
foreach($valuee as $key => $valued){
$this->query = "INSERT INTO {$tablename} ($columns)VALUES('$valued[$key]')";
} return $this->query;
}
}
Here is usage of this code
if(isset($_POST['submitt'])){
$new = new myform;
$values = $_POST;
$query_form = $new->form_values('myguests',$values);
}
Array (
[objective] => hj
[full_name] => sdad
[email] => dsafds
[phone] => dasfds
[nationality] => adf
[date_of_birth] => adsf
[country] => dsfasd
[website] => asdfds
[address] => sadfdsaf
[my_image_upload_nonce] => 3b17791a4e
[_wp_http_referer] => /cv_builder/
[job_title] => Array
(
[0] => asdfds
[1] => adsfdsaf
)
[company_name] => Array
(
[0] => dasfdaf
[1] => asfdsafsd
)
[comp_other_info] => Array
(
[0] => adsfdsfdsafdsafasdf
[1] => adfadfasfdfadsfsdafsadf
)
[qualification] => Array
(
[0] => adsfddsfasdfdaf
)
[refrence] => References available upon request.
[submitt] => submit
) i am new to oop concept please suggested me to imporve.Thanks to all
Generally its a bad idea to allow users to build directly your queries to DB.
By your case - at the begining of method form_values add code:
foreach($values as $key => $value) {
if (!is_string($value)) {
//do some stuff. Remove element from array, or convert it to string
}
}
Yahoo... I Got it myself here is the solution hope someone would help.
class myform{
public $key;
public $value;
public $query;
public $con;
public function __construct(){
$this->con = new mysqli('localhost','root','','data') or die('you are losing your mind');
}
public function array_implode( $array) {
if ( ! is_array( $array ) ) return $array;
$string = array();
foreach ( $array as $key => $val ) {
if ( is_array( $val ) )
$val = implode( ',', $val );
$string[] = "{$val}";
}
return $string;
}
public function form_values($tablename,$values){
// unset($values['sections']);
$array = $values;
echo '<pre>';
var_export($dg);
echo '</pre>';
$columns = implode(", ",array_keys($values));
$escaped_values = array_map('esc_sql', array_values($dg));
$values = implode("',' ", $escaped_values);
$this->query = "INSERT INTO {$tablename} ($columns)VALUES($values)";
return $this->query;
}
public function insert_form($query_form){
$stmt = $con->prepare($query_form) or trigger_error("ponka");
$stmt->execute();
}
}
Usage
$new = new myform;
$values = $_POST;
$final_values = $new->array_implode($values);
$query_form = $new->form_values('wp_cv',$final_values);
$new->insert_form($query_form);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.