tibberous Posted December 17, 2007 Share Posted December 17, 2007 I am trying to make a nice function to do a mysql insert. Right now I have 13 values, which means using a huge mysql query string. I was hoping to make a function that would just take in 13 variables and perform the insert. The variables are all named the same thing as they are in the database. The biggest problem I am having that there doesn't seem to be a good way to get the variables name from the variable, if you are using get_func_args to retrieve it. Link to comment https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/ Share on other sites More sharing options...
papaface Posted December 17, 2007 Share Posted December 17, 2007 Show us some code to work with. Link to comment https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/#findComment-417237 Share on other sites More sharing options...
corbin Posted December 17, 2007 Share Posted December 17, 2007 Wrote this just now..... Not the most effecient code ever (the way I create new arrays when I don't need to), but it would work fine. <?php function AutoInsert($table, $args, $exec = false) { $fields = array(); $values = array(); foreach($args as $k => $v) { $fields[] = $k; $values[] = addslashes($v); } unset($args); $f = implode('\',\'', $fields); $f = '\'' . implode('\',\'', $values) . '\''; $q = "INSERT INTO `{$table}` ({$f}) VALUES ('{$v}');"; if($exec) { return mysql_query($query); } else { return $q; } } ?> [/code] An example: AutoInsert('users', array('uname' => 'Corbin', 'password' => 'somepass', 'email' => '[email protected]')); Would return: INSERT INTO `users` (uname, password, email) VALUES ('Corbin', 'somepass', '[email protected]'); I don't think that's what you're trying to do though.... Lemme see if I can find out how to see the name of a var passed to a function (I don't know if that's possible). Link to comment https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/#findComment-417242 Share on other sites More sharing options...
pocobueno1388 Posted December 17, 2007 Share Posted December 17, 2007 You could pass a function an array of field names and then an array of values that correspond to the fields. Here is a simple function. <?php function insert($fields, $values){ $query = "INSERT INTO table (".implode(',', $fields).") VALUES (".implode(',', $values).")"; $result = mysql_query($query)or die(mysql_error()); } ?> Example use <?php $fields = array("1", "2", "3"); $values = array("uno", "dos", "tres"); insert($fields, $values); ?> Link to comment https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/#findComment-417243 Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 Another method. <?php $array = array( 'foo' => 'bar', 'bob' => 'boo', 'babe' => 'betty' ) function doinsert($arr) { $sql = "INSERT INTO tbl SET "; foreach($arr as $k => $v) { $sql .= "$k = '$v', "; } $sql = rtrim($sql,','); echo $sql; // here you would perform the query. } doinsert($array); ?> Link to comment https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/#findComment-417249 Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Ooohhhh I didn't realize you can use SET with inserts.... (Although I did know UPDATE INTO blah SET, so I guess that's logical).... Link to comment https://forums.phpfreaks.com/topic/82107-cleaner-mysql-insert/#findComment-417262 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.