Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > PHP
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 04-05-2005, 06:08 PM   #1 (permalink)
phase
Registered User
 
Join Date: Apr 2005
Posts: 1
phase is on a distinguished road
Generate query automatically from form

hi,

i have been scouring the internet lately trying to look for an answer to my question, but im at a loss, i kno whteres a way of doing what i want, im just not sure how to go about it.

so here goes, i hope somebody out there can help...

my question is how do i go about making one fnction which inserts posted values into a database, im sick of writin gth esame code over and over and just chnaging the values of the form,

this is what i have atm...

PHP Code:
$sql "INSERT INTO $table (first,second,company,home,mobile,fax,email,address,comments) VALUES ('$first', '$second', '$company', '$home', '$mobile', '$fax', '$email', '$address', '$comments')"; } $result mysql_query($sql); echo "You have added that record! "//end code 

as you can see the php is ok, bt to repeat this over and over is really getting on my nerves.

isnt the a way for example of doing it this way...


PHP Code:
$sql "INSERT INTO $table ($names) VALUES ('$values')"; } $result mysql_query($sql); echo "You have added / edited that record!"

then the sql command is generic and would work for all forms i create, how would i go about getting my form values and field names stored into arrays to do such a thing?

i have searched other forums, and i came across somethign tha tlooked ideal for me...http://forums.devnetwork.net/viewtopic.php?t=30683

i cannot get it to work at all tho, if ayone out there is capable of giving me a working example including form and where it goes to etc i would be extremely grateful

help would be really appreciated.

phase
__________________
phase is offline   Reply With Quote
Old 04-06-2005, 01:11 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 637
DJMaze is on a distinguished road
now you mention i do use such function.
Here's my code
PHP Code:
function sql_insert($table$fields)
    {
        if (
is_array($fields) && !empty($fields)) {
            foreach (
$fields AS $field => $value) {
                if (!
is_int($field)) {
                    
$qfields[] = $field;
                    
$qvalues[] = "'".mysql_real_escape_string($value)."'";
                }
            }
            return 
mysql_query('INSERT INTO '.$table.' ('.implode(', '$qfields).') VALUES ('.implode(', '$qvalues).')');
        }
        return 
false;
    } 
$fields is of the form
PHP Code:
$fields['fieldname'] = 'value'
Be aware this code needs unslashed data and then adds proper slashing for mysql itself.
There are several reasons why i don't allow PHP auto-slashed values.
One of them is magic_quotes_sybase=On which makes addslashes() pretty useless.
__________________
DJMaze is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Values from Popup Form to Main Form chrislopezz PHP 7 03-28-2005 12:45 PM
Form Field Change nyoung HTML, XML, Javascript, AJAX 3 03-17-2005 04:38 AM
How to insert HTML form data into mysql database ? plomon PHP 5 02-06-2005 08:23 AM
EMERGENCY: Dynamic form processing DavH27 PHP 8 10-27-2004 07:52 PM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 11:55 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle