Johnny
asked on
convert from insert to update (mysql data)
i had came across this nifty script a bit back but i now have need to make it update instead of insert
can someone help me convert this so it can be used on a an edit for please
i need to pass the value of
where ID='".$id."'
please see snip attached
thank you in advance for any code or help you may provide
can someone help me convert this so it can be used on a an edit for please
i need to pass the value of
where ID='".$id."'
please see snip attached
thank you in advance for any code or help you may provide
$TableName="contacts";
function quote_smart($val)
{
if( is_array($val) ) {
return array_map("quote_smart", $val);
} else {
if( get_magic_quotes_gpc() ) {
$val = stripslashes($val);
}
if( $val == '' ) {
//$val = 'No Description Available';
$val = '';
} if( !is_numeric($val) || $val[0] == '0' ) {
$val = "'".mysql_real_escape_string($val)."'";
}
return $val;
}
}
$keyStr='';$valStr='';
$dontUse = array("submit");
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
if (!in_array($key,$dontUse)){ if($keyStr) $keyStr.=',';
$keyStr.= str_replace(')','',$key);
if($valStr) $valStr.=',';
$valStr.= quote_smart($val);
$message .=" $key = $val<br>\n";
//echo $message;
}
}
// Enter Info into the Database.
// where ID='".$id."' <<< need clause
$insert = "INSERT INTO $TableName($keyStr) VALUES($valStr)";
mysql_query ($insert, $mysql_link)
or die(mysql_error());
$count=0;
$i=1;
while($i <= 4)
{
if($HTTP_POST_VARS["item$i"] != NULL)
{
if($count == 0)
$items .= $HTTP_POST_VARS["item$i"];
else
$items .= ",".$HTTP_POST_VARS["item$i"];
$count++;
}
$i++;
next;
}
Is this posting from a form?
nvm that last comment, I see now
ASKER
i came across this code works great for adding a record just want to use for updating the record.
where should probably be a hidden post value huh
like
<input name='id' type='hidden' value='1'>
where should probably be a hidden post value huh
like
<input name='id' type='hidden' value='1'>
Yeah, you can post it ad a hidden id.. I am almost done rewriting ift for ya
ASKER
sweet
I can't spell today.. sorry
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
testing
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Change the while loop to include your quote_smart() function
while (list ($key, $val) = each ($HTTP_POST_VARS)) {
if (!in_array($key,$dontUse)){
$query = "update $TableName set $key = '".quote_smart($val)."' where ID = '$id'";
mysql_query($query, $mysql_link)or die(mysql_error());
}
}
ASKER
i see we have
if (!in_array($key,$dontUse)) {
but im still getting
Unknown column 'submit' in 'field list'
from both examples
if (!in_array($key,$dontUse))
but im still getting
Unknown column 'submit' in 'field list'
from both examples
ASKER
nvm found it
ASKER
thx for the help
ASKER
both worked great thank you
hi,
just a note, if you use the first example, please make sure to mysql_real_escape_string your keys as well as your values, but don't use the quote_smart function to do it because you don't want quotes around the key names, if anything you want ` around the key names.
Glad these worked for you
just a note, if you use the first example, please make sure to mysql_real_escape_string your keys as well as your values, but don't use the quote_smart function to do it because you don't want quotes around the key names, if anything you want ` around the key names.
Glad these worked for you
ASKER
i used both some elements where missing from both i combined them.. thanks again and for the heads up too