lt Posted January 27, 2011 Share Posted January 27, 2011 Hi y'all! I'm working on a form-system in PHP that connects to a MySQL database. I have a lot of these: $id = ''; $time = ''; $heading = ''; $for = ''; $description = ''; //and more... if (isset($_REQUEST['id'])) {$id = $_REQUEST['id'];} if (isset($_REQUEST['time'])) {$time = $_REQUEST['time'];} if (isset($_REQUEST['heading'])) {$heading = $_REQUEST['heading'];} if (isset($_REQUEST['for'])) {$for = $_REQUEST['for'];} if (isset($_REQUEST['description'])) {$description = $_REQUEST['description'];} mysql_query (" UPDATE posts SET time = '$time', heading = '$heading', for = '$for', description = '$description' WHERE id = $id LIMIT 1 ;"); I'm thinking if it is possible to make a dynamic function to handle this? Kind of...: req("id", array("time", "heading", "for", "description")); function req($id, $reqThings){ foreach($reqThings as $reqThing){ //something.... } } The "id" is special because it is used in the "WHERE id = $id"-part of the mysql_query I really hope you can help! Just ask if I have to be more precise... ;-) Quote Link to comment https://forums.phpfreaks.com/topic/225853-dynamic-request-mysql_query-function/ Share on other sites More sharing options...
ignace Posted January 27, 2011 Share Posted January 27, 2011 function update($table, $data, $where) { mysql_query('UPDATE ' . $table . ' SET ' . _create_set_clause($data) . ' WHERE ' . _create_where_clause($where)); } $data and $where are both array's in the format: $column => $value Quote Link to comment https://forums.phpfreaks.com/topic/225853-dynamic-request-mysql_query-function/#findComment-1166122 Share on other sites More sharing options...
AbraCadaver Posted January 27, 2011 Share Posted January 27, 2011 You can abstract it more or less, this is kind of in between (not tested): function update($table, $id, $reqThings) { if(empty($table) || empty($id) || !is_array($reqThings)) { return false; } foreach($reqThings as $thing) { $value = !empty($_REQUEST[$thing]) ? mysql_real_escape_string($_REQUEST[$thing]) : ''; $set[] = "`$thing` = '$value'" } $set = implode(',', $set); return mysql_query("UPDATE `$table` SET $set WHERE id = '$id' LIMIT 1"); } Quote Link to comment https://forums.phpfreaks.com/topic/225853-dynamic-request-mysql_query-function/#findComment-1166180 Share on other sites More sharing options...
ignace Posted January 27, 2011 Share Posted January 27, 2011 You can abstract it more or less, this is kind of in between (not tested): function update($table, $id, $reqThings) { if(empty($table) || empty($id) || !is_array($reqThings)) { return false; } foreach($reqThings as $thing) { $value = !empty($_REQUEST[$thing]) ? mysql_real_escape_string($_REQUEST[$thing]) : ''; $set[] = "`$thing` = '$value'" } $set = implode(',', $set); return mysql_query("UPDATE `$table` SET $set WHERE id = '$id' LIMIT 1"); } Assuming the PK to be named id is a false-positive, mine actually have the table name prefixed. An anti-pattern some frameworks have picked up. LIMIT 1 is also obsolete, ID is a PK and therefor unique. Also, not every table has a single column as an ID, some are compound. Quote Link to comment https://forums.phpfreaks.com/topic/225853-dynamic-request-mysql_query-function/#findComment-1166234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.