PHPBuilder - MySQL INSERT generated from all $_POST vars



RSS Twitter
Snippets Databases

MySQL INSERT generated from all $_POST vars

by: Keith Thibodeaux
|
December 5, 2008

Version: 1

Type: Full Script

Category: Databases

License: GNU General Public License

Description: Generates a MySQL query from all fields on an HTML page. To add a new colum to insert data into, simply add a new field (make sure the field name is the same as teh column name in the MySQL table) and it does it all for you. I wrote this when I got tired of INSERT into whatever (id, name, date)VALUES('', '', '') and decided I wanted something to just generate it for me.



<?php

//Keith Thibodeaux
//qalimas [at) gmail (doT] com

/*
Note that to add a new column to the query, just add in an input box to the HTML, the PHP generates an insert for each field.

This simply displays the query to be executed, to make it execute and not display, change the link "echo "The MySQL Statement..." to "$this->exec($query);" at the bottom of the create_new function.
You would also have to uncomment the MySQL function and set it up accordingly.
*/

//MySQL Class
class MySQL
{
/*	function MySQL()
	{
		if (!$dblink = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)) {
			die (mysql_errno() . ": " . mysql_error());
		}

		if (!mysql_select_db(DB_DATABASE)) {
			die (mysql_errno() . ": " . mysql_error());
		}
	}
*/
	static function exec($query)
	{
		if (!$result = mysql_query($query)) {
			die (mysql_errno() . ": " . mysql_error());
		}
		return $result;
	}
	

	function create_new($table, $data)
	{		
		$query = "INSERT INTO $table (";
		
		foreach ($data as $field => $value) {
			$query .= "$field,";
		}

		$query .= ") VALUES (";

		foreach ($data as $field => $value) {
			$query .= "'$value',";
		}

		$query .= ")rplme";

		$query = str_replace(",) VALUES (", ") VALUES (", $query);
		$query = str_replace(",)rplme", ")", $query);
		
		echo "The MySQL Statement that would be executed is <b>$query</b>";
	}
}
//End MySQL Class

$mysql = new MySQL;

if ($_GET['action'] == "do") {

	$data = array();
	
	foreach ($_POST as $field => $value) {
		if ($field != "submit" && $field != "whatever field name you dont want to include (add to the query)") {
			$data[$field] = $value;
		}	
	}
	
	$mysql->create_new("customer", $data);

} else {

//Begin else content
?>
<form action="?action=do" method="POST">
		<table>

			<tr>

				<td>First Name</td>

				<td align="right"><input size="25" type="text" name="csfname" /></td>

			</tr>

			<tr>

				<td>Last Name</td>

				<td align="right"><input size="25" type="text" name="cslname" /></td>

			</tr>

			<tr>

				<td>Email Address</td>

				<td align="right"><input size="25" type="text" name="csemail" /></td>

			</tr>

			<tr>

				<td>Home Phone</td>

				<td align="right"><input size="25" type="text" maxlength="13" name="csphome" /></td>

			</tr>

			<tr>

				<td>Cell Phone</td>

				<td align="right"><input size="25" type="text" maxlength="13" name="cspcell" /></td>

			</tr>

			<tr>

				<td>Fax Number</td>

				<td align="right"><input size="25" type="text" maxlength="13" name="cspfax" /></td>

			</tr>

			<tr>

				<td>Street Address (123 Fake st)</td>

				<td align="right"><input size="25" type="text" name="csstadd" /></td>

			</tr>

			<tr>

				<td>City (Lake Charles)</td>

				<td align="right"><input size="25" type="text" name="cscity" /></td>

			</tr>

			<tr>

				<td>State (Abbreviation)</td>

				<td align="right"><input size="25" type="text" maxlength="2" name="csstate" /></td>

			</tr>

			<tr>

				<td>Zip Code (5 Digits)</td>

				<td align="right"><input size="25" type="text" maxlength="5" name="cszip" /></td>

			</tr>

		</table>

		<br />

		<div style="text-align: right;"><input class="button" type="submit" value="Create" name="submit" /></div>
</form>
<?php
//end else content
}

?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Keith Thibodeaux

Comment:



Comment:

(Maximum characters: 1200). You have characters left.