[solved] getting form fields into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

[solved] getting form fields into database

Post by phase »

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...

Code: Select all

$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...

Code: Select all

$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?

help would be really appreciated.

phase
Last edited by phase on Wed Apr 06, 2005 12:42 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

didn't search enough! :lol:

viewtopic.php?t=30683
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

Post by phase »

thanks for that feyd, but ive been tring to get that to work for 5 hours now with no success :( grrr LOL.

ive pasted my full page below, if anyone can assist i getting this working i would be sooo happy., ive tried and unfortunately failed miserably :(

Code: Select all

<?
if($session->isAdmin()){
$filename = "test";
$table = "customers";
global $table;
if($action){
$filter_fields = get_db_columns($table); 
insert_form_data($_POST, $table, $action);
echo "added to $table $hr";
addform($view,$admin,$table);
 }//end if action
 
    else {
echo "
<form method=\"post\" action=\"index.php?view=$view&admin=$admin&action=insert\">
$hr FirstName	<input type=\"Text\" name=\"first\" value=\"$first\" size=\"40\">		
$hr Surname		<input type=\"Text\" name=\"second\" value=\"$second\" size=\"40\">		
$hr Company		<input type=\"Text\" name=\"company\" value=\"$company\" size=\"40\">	
$hr Home Phone	<input type=\"Text\" name=\"home\" value=\"$home\" size=\"40\">		
$hr Mobile		<input type=\"Text\" name=\"mobile\" value=\"$mobile\" size=\"40\">	
$hr Fax			<input type=\"Text\" name=\"fax\" value=\"$fax\" size=\"40\">
$hr Email		<input type=\"Text\" name=\"email\" value=\"$email\" size=\"40\">
$hr Address		<textarea rows=\"4\" name=\"address\" cols=\"37\">$address</textarea>
$hr Comments	<textarea rows=\"4\" name=\"comments\" cols=\"37\">$comments</textarea>
<input type=\"Submit\" name=\"submit\" value=\"Update\" class= \"abtnname\" style=\"float: left\"></form>";		

	}//end else

}//end if admin

else{
"not admin go away!";
}//end 


//functions to add data into database//
function get_db_columns($table){    
$query = "DESCRIBE ".$table;    
$links = mysql_query($query) or die(mysql_error());    
while($line = mysql_fetch_array($links, MYSQL_ASSOC)) 
{            
$filter_fields[] = $line['Field'];    
}    
return $filter_fields;
}

function insert_form_data($form_data, $table, $action) {     
$filter = get_db_columns($table);     
$sql = '';     foreach ($form_data as $key => $value) {         
if(in_array($key, $filter)) {             
$sql .= (!empty($sql)?', `':'`') . $key .'` = ' . (!is_numeric($value) && empty($value)?'NULL':"'$value'");         
}     
}     
$sql = $action.' INTO `' . $table . '` SET ' . $sql;     
return $sql;
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do you have register_globals on?

Code: Select all

echo 'Register globals are ' . (ini_get('register_globals') ? 'on' : 'off');
remove line 5. $table is already a global with the code you posted.

If register globals are not on (which they shouldn't), then line 6 will always be false, thus making insert_form_data() not run.

insert_form_data() does no insert of its own, but simply returns a sql query to perform it. Your code does not use the return from it.
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

Post by phase »

i have removed line 5 (global table)
i have checked my global variables and they are on
i really do not uderstand your third comment, i thought this script was to retrieve and submit form values? can youplease let me know how i would do that with the $sql ?

thanks again for your reply.

phase
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

the script merely generates the query necessary to insert the field's that match (exactly) fields from the table in the database. You still need to perform the query on your own.
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

Post by phase »

argh i been at this too long and still no further, would have loved a simple example instead of clues, as im totally lost after 7 hours.

where does the query go, how do i do the query, what variables are going in it, etc etc

its obvious i dont have a clue thats why i posted help and been sat on these forums hours on end hoping. LOL

nevermind.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

....but.. you have an insert query and query call in your original post. :?
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

Post by phase »

ok this is what im thinking...

Code: Select all

$sql = "INSERT INTO $table ($names) VALUES ('$values')"; } $result = mysql_query($sql); echo "You have added / edited that record!";
thats my query i originally posted, so please please please tell me how to use that within this script, im going mad!

obviously the variables are going to change, i know $table will be ok, $names would be? $values would be? and where exactly on my script shall i place this query, im guessing at line..9?

thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'll give you everything, this one time only. :?

Code: Select all

<?php

if($session->isAdmin())
{
	$filename = "test";
	$table = "customers";
	if($action)
	{
		$filter_fields = get_db_columns($table); 
		$sql = insert_form_data($_POST, $table, $action);
		mysql_query( $sql ) or trigger_error( 'Insertion failed: ' . mysql_error(), E_USER_ERROR);
		echo "added to $table $hr";
		addform($view,$admin,$table);
	}//end if action
	else
	{
		echo "
		<form method=\"post\" action=\"index.php?view=$view&admin=$admin&action=insert\">
		$hr FirstName    <input type=\"Text\" name=\"first\" value=\"$first\" size=\"40\">        
		$hr Surname        <input type=\"Text\" name=\"second\" value=\"$second\" size=\"40\">        
		$hr Company        <input type=\"Text\" name=\"company\" value=\"$company\" size=\"40\">    
		$hr Home Phone    <input type=\"Text\" name=\"home\" value=\"$home\" size=\"40\">        
		$hr Mobile        <input type=\"Text\" name=\"mobile\" value=\"$mobile\" size=\"40\">    
		$hr Fax            <input type=\"Text\" name=\"fax\" value=\"$fax\" size=\"40\">
		$hr Email        <input type=\"Text\" name=\"email\" value=\"$email\" size=\"40\">
		$hr Address        <textarea rows=\"4\" name=\"address\" cols=\"37\">$address</textarea>
		$hr Comments    <textarea rows=\"4\" name=\"comments\" cols=\"37\">$comments</textarea>
		<input type=\"Submit\" name=\"submit\" value=\"Update\" class= \"abtnname\" style=\"float: left\"></form>";        
	}//end else 
}//end if admin
else
{
	"not admin go away!";
}//end 
 
 
//functions to add data into database//
function get_db_columns($table)
{
	$query = "DESCRIBE ".$table;    
	$links = mysql_query($query) or die(mysql_error());    
	while($line = mysql_fetch_array($links, MYSQL_ASSOC)) 
	{            
		$filter_fields[] = $line['Field'];    
	}    
	return $filter_fields;
}
 
function insert_form_data($form_data, $table, $action)
{
	$filter = get_db_columns($table);     
	$sql = '';     foreach ($form_data as $key => $value)
	{
		if(in_array($key, $filter))
		{
			$sql .= (!empty($sql)?', `':'`') . $key .'` = ' . (!is_numeric($value) && empty($value)?'NULL':"'$value'");         
		}
	}     
	$sql = $action.' INTO `' . $table . '` SET ' . $sql;     
	return $sql;
}

?>
phase
Forum Newbie
Posts: 24
Joined: Sun Jul 18, 2004 10:47 am

Post by phase »

thank you very very much, finally sorted 6.40 am! lol

im really grateful for your help and i didnt expect the full code, but i was very close by the end, i said line 9 for my query, its just i was getting confused, it was real early in morning an di hate going to sleep not having resolved something :)

once again thank you for your time.
Post Reply