You are here: Re: Form not working properly after adding new table columns to DB « All PHP « IT news, forums, messages
Re: Form not working properly after adding new table columns to DB

Posted by google@tfwasmus.enschedenet.nl on 09/28/08 11:48

Hmmmz, backticks aren't in your code you say....

What did I say?
> Use backticks ` instead of ' for fieldnames.

So:
$query .= '`inventory`, ';
etc.

(use "$query .=" instead of "$query = $query .", it's not an error,
but just unnecessary)

If that doesn't work, what does mysql_error() say? Did you check that?
Normally it points specifically to where you went wrong.

Some more advise:
create an array of your fields like:
$fields = array('inventory','date_entered','first'...etc

/* ALWAYS escape userdata before entering it in a database */
foreach($fields as $field){
if(isset($_REQUEST[$field])){
mysql_real_escape_string($_REQUEST[$field]);
} else {
//lets add empty values for the checkboxes that don't exist
$_REQUEST[$field])='';
}
}

/* make sure $fields an $_REQUEST are in the same order */
asort($fields);
ksort($_REQUEST);

/* build query */
$query = "INSERT INTO $table (`";
$query .= implode('`,`',$fields);
$query .= "`) VALUES {'";
$query .= implode("','",
array_intersect_key($_REQUEST,array_flip($fields)));
$query .= "')";

mysql_query($query);
if(mysql_affected_rows()<1){
echo mysql_error();
}

That way, next time some fields get added or removed, all you have to
take care of is the form and add/remove the field from to/from the
array, everything else is automatic.

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация