PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
-- Hint: use Ctrl+Enter for SQL autocomplete create table table_name (name varchar(23),surname varchar(23), user_id varchar(25),user_email varchar(25)); insert into table_name set user_id =25, user_email='john@smith.com';
Copy Clear
Copy Format Clear
<?php $data = array( "name"=>"John", "surname"=>"Smith", ); $where = array( "user_id" => "25", "user_email"=>"john@smith.com", ); update_record($pdo, "table_name",$where,$data); var_dump($pdo->query("select * from table_name")->fetch()); function update_record($pdo, $table, $where, $fields) { $params = []; $set = ""; $condition = ""; foreach($fields as $key => $value) { $key = str_replace("`", "", $key); // a silly protection, works for mysql only $set .= ($set ? "," : "") . "`$key` = ?"; $params[] = $value; } foreach($where as $key => $value) { $condition .= ($condition ? " AND " : "") . "`$key` = ?"; $params[] = $value; } echo $sql = "UPDATE `$table` SET $set WHERE $condition"; $pdo->prepare($sql)->execute($params); }
Show:  
Copy Clear