generating sql from postdata
Moderator: General Moderators
generating sql from postdata
hi, i want to create one universal processing page that will process forms with different fields, what i think is easiest is to analyze the POSTdata and based on that generate sql. (p.s. i'm a newbie so i need some pretty simple help:)
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Maybegets you a starter.
Code: Select all
<?php
foreach($_POST as $key=>$value) {
echo $key, '=>', $value, " <br />\n";
}
?>re
heres what i came up with:
comments?
Code: Select all
<?php
$post=Array('id'=> 21, 'title' => 'one', 'short_desc' => 'two', 'other' => 'three' ) ;
$col='';
$val='';
$i=1;
foreach($post as $key=>$value) {
$i>1?$c=', ':$c='';
$col=$col.$c.$key;
$val=$val.$c.$value;
$i++;
}
$sql="insert into tablename ($col) values ($val)" ;
echo $sql;
?>Code: Select all
$col2=implode(', ',array_keys($post));
$val2=implode(', ',array_values($post));
$sql2="insert into tablename ($col2) values ($val2)";one more question though, what if i have other stuff in the post array before the values i want to use for exaple:
Code: Select all
$post=Array('misc'=> '25', 'table' => 'curr', 'bla' => 'blabla', 'id'=> '21', 'title' => 'one', 'short_desc' => 'two', 'other' => 'three' ) ;- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Re: re
DESCRIBE or SHOW CREATE TABLE queries.yshaf13 wrote:first of all, how would i get that list from the db?
Yes.yshaf13 wrote:and second, is there anyway to just do everything from id and on?
array_keys() + array_search() + array_slice(); array_values() + <the array_search() result from before> + array_slice(); array_combine(). If you do a query blindly with the results your queries are very easy to attack. This is a major security hole.