Since I'm programming a lot with mysql and php I got tired of changing 2 or 3 pages when I wanted to add a field. So at least for the insert and update statements I don't have to change anything anymore. I only have to add a field in the database and add it to the form.
this example will only work for a htmlform which will affect only 1 table. the names of the fields in the htmlform must be the same as the fieldnames in your table.
so if I want to update the artisttable the action would be
sqlaction.php3?arid=7
ps (!empty($keyvalue)) statement and method for having update and insert statements in one page is copied from o'reillys web database applications book page 334
and the code is here stripped to a minimum but working
this example will only work for a htmlform which will affect only 1 table. the names of the fields in the htmlform must be the same as the fieldnames in your table.
Code:
sqlaction.php3
<?php
include $DOCUMENT_ROOT."/cgi-bin/connopen.php3";
// the key is passed through the url with the name of the keyfield
list($tablekey, $keyvalue) = each($HTTP_GET_VARS);
//array for attaching the keynames to the corresponding tablenames fill in your own table names and keyfields
$tablearray=array();
$tablearray[arid]='artisttable';
$tablearray[clid]='clienttable';
$tablearray[orid]='ordertable';
$tablearray[orlid]='orderlinestable';
//loop through the posted variables
//formfieldnames must be corresponding to tablefieldnames
while ( list($field, $value) = each ($HTTP_POST_VARS)) {
if ($field <> "Submit" ){
$fieldlines=$fieldlines .$field ."='" .$value ."',";
}
}
$fieldlines=substr($fieldlines,0,strlen($fieldlines)-1); // get rid of the last ,
if (!empty($keyvalue)) //if no key in the url then consider it an insert statement
{
$query="UPDATE " . $tablearray[$tablekey] ." SET " . $fieldlines . " WHERE " . $tablekey ."='$keyvalue'";
}
else
{
$query="INSERT INTO " . $tablearray[$tablekey] . " SET ". $fieldlines;
}
$rs=mysql_query($query,$conn);
print ("
<html><body><table>
<tr><td>$query<br></td></tr>
</table>
</BODY>
</HTML>
");
mysql_close($conn);
?>
so if I want to update the artisttable the action would be
sqlaction.php3?arid=7
ps (!empty($keyvalue)) statement and method for having update and insert statements in one page is copied from o'reillys web database applications book page 334
and the code is here stripped to a minimum but working