WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
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 Click here to Update Your Picture
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

Submit your own code examples  Submit your own code examples 
 

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