note 75637 added to function.mysqli-query
| From: | kylealanhale at vanveninc dot com | Date: | Sat, 09 Jun 2007 19:41:20 +0000 |
| Subject: | note 75637 added to function.mysqli-query | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-127336@lists.php.net to get a copy of this message | ||
I created a simple function for the oft used INSERT query:
string queryInsert (string $table, array $query)
This function assumes several things, and is used in cases where a simple INSERT is needed for an
existing table with existing columns.
<?php
function queryInsert ($table, $query) {
if (is_array ($query)) {
foreach ($query as $key => $value) {
$columns[] = $key;
$values[] = $value;
}
unset ($key, $value);
return 'INSERT INTO ' . $table . ' (' . implode(", ", $columns) .
') VALUES (' . implode(", ", $values) . ');';
}
else {
return false;
}
}
?>
Usage:
(Assumes that $dbObject is a valid mysqli object and $formDataPost is an array of data from an HTML
form)
<?php
$query = array (
'date_submission' => "'" . date("Y-m-d H:i:s") .
"'",
'form_values' => "'" .$dbObject->escape_string (json_encode
($formDataPost)) . "'",
'ip_address' => "INET_ATON('$clientIP')",
'web_browser' => "'$clientBrowser'",
'system_os' => "'$clientOS'",
'session_id' => "'" . session_id () . "'"
);
if ($result = $dbObject->query (queryInsert ('form_data', $query))) {
echo '<p>Thank you! Your form has been submitted.</p>';
}
?>
Returns something like this:
INSERT INTO form_data (date_submission, form_values, ip_address, web_browser, system_os, session_id)
VALUES ('2007-06-09 13:23:40', '{\"1\":\"I\'m a rad
form.\",\"2\":1,\"3\":[2,3,5]', INET_ATON('127.0.0.1'),
'MOZILLA 1.8.1.4', 'WIN XP', '52cbb555c6e7950bf7ac245a805b78de');
----
Server IP: 83.138.144.80
Probable Submitter: 208.187.181.162
----
Manual Page -- http://www.php.net/manual/en/function.mysqli-query.php
Edit -- https://master.php.net/note/edit/75637
Del: integrated -- https://master.php.net/note/delete/75637/integrated
Del: useless -- https://master.php.net/note/delete/75637/useless
Del: bad code -- https://master.php.net/note/delete/75637/bad+code
Del: spam -- https://master.php.net/note/delete/75637/spam
Del: non-english -- https://master.php.net/note/delete/75637/non-english
Del: in docs -- https://master.php.net/note/delete/75637/in+docs
Del: other reasons-- https://master.php.net/note/delete/75637
Reject -- https://master.php.net/note/reject/75637
Search -- https://master.php.net/manage/user-notes.php