Link to home
Start Free TrialLog in
Avatar of Johnny
JohnnyFlag for United States of America

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
$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 &nbsp;=&nbsp; $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;
}

Open in new window

Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

Is this posting from a form?
nvm that last comment, I see now
Avatar of Johnny

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'>
Yeah, you can post it ad a hidden id.. I am almost done rewriting ift for ya
Avatar of Johnny

ASKER

sweet
I can't spell today.. sorry
ASKER CERTIFIED SOLUTION
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Johnny

ASKER

testing
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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());
  }
}

Open in new window

Avatar of Johnny

ASKER

i see we have
if (!in_array($key,$dontUse)){

but im still getting
Unknown column 'submit' in 'field list'
from both examples
Avatar of Johnny

ASKER

nvm found it
Avatar of Johnny

ASKER

thx for the help
Avatar of Johnny

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
Avatar of Johnny

ASKER

i used both some elements where missing from both i combined them.. thanks again and for the heads up too