|
|
|
| Title : |
Easy mysql insert function - A PHP function that allows you to pass it an associative array and a table variable. It builds the insert query from the array. |
| Categories : |
PHP, MySQL, Databases |
 Diego Salazar |
| Date : |
Oct 10th 2008 |
| Grade : |
4 of 5 (graded 3 times) |
| Viewed : |
13297 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Diego Salazar |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
The array keys must match your column names, and the table variable is the table to insert into.
This function assumes you have already run mysql_connect($host, $user, $pass); and mysql_select_db($database);. This function can easily be changed into an update query with some mods.
my_handy_insert_function.php
| <?php
function insert($info, $table) {
if (!is_array($info)) { die("insert member failed, info must be an array"); }
//build the query
$sql = "INSERT INTO ".$table." (";
for ($i=0; $i<count($info); $i++) {
//we need to get the key in the info array, which represents the column in $table
$sql .= key($info);
//echo commas after each key except the last, then echo a closing parenthesis
if ($i < (count($info)-1)) {
$sql .= ", ";
} else $sql .= ") ";
//advance the array pointer to point to the next key
next($info);
}
//now lets reuse $info to get the values which represent the insert field values
reset($info);
$sql .= "VALUES (";
for ($j=0; $j<count($info); $j++) {
$sql .= "'".current($info)."'";
if ($j < (count($info)-1)) {
$sql .= ", ";
} else $sql .= ") ";
next($info);
}
//execute the query
mysql_query($sql) or die("query failed ".mysql_error());
return mysql_insert_id();
}
?> | |
EXAMPLE:
| <?php
mysql_connect("localhost", "root", "root");
mysql_select_db("mydb");
include("my_handy_insert_function.php");
$info = array("user" => $username, "email" => $email, "pass" => $pwd);
$table = "users";
//insert new user and get their new id
$id = insert($info, $table);
//with the id we can insert more info about the user into another table
$details = array("user_id" => $id, "phone" => $phone, "gender" => $gender, "addr" => $addr);
$table2 = "user_details";
insert($details, $table);
?> | | |
|
| This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server. Categories : Databases, MySQL, Complete Programs, PHP, Databases | | | bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager Categories : MySQL, PHP, MySQL, Complete Programs, Databases | | | Invision Forums Latest Threads list Categories : PHP, Miscellaneous, Databases, MySQL | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | | | Tropicalm Genetree Family (MySQL based family tree) Categories : PHP, Interfaces, Databases, MySQL, Complete Programs | | | This simple function will take a few arguments and easily set a associative array for each column in a result from a MySQL query Categories : Databases, PHP, MySQL, Arrays | | | usercounter class Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables | | | GroupIT Engine v1.00rc1
Categories : PHP, Content Management, MySQL, Databases | | | mySQL/PHP/search with multientry
form and table output with colored rows Categories : PHP, Beginner Guides, MySQL, HTML and PHP, Databases | | | The simple counter with use MySql and gd. Categories : MySQL, HTTP, Graphics, PHP, Databases | | | Email a user with out exposing email address Categories : PHP, Databases, MySQL, Email | | | Creating thumbnails from MySQL Blobs online Categories : PHP, MySQL, Graphics, HTML and PHP, Databases | | | Inserting data to a MySQL Database using AJAX Categories : AJAX, HTTP, PHP, Databases, MySQL | | | complete, simple, working example of a login screen/system using php functions, cookies, and a mysql database for begginers. Categories : Authentication, Complete Programs, PHP, MySQL, Databases | | | AJAX Application Categories : PHP, AJAX, Databases, MySQL | |
| |
| |
|