Jump to content

Archived

This topic is now archived and is closed to further replies.

Guest kilbad

Can this part of my script be consolidated somehow?

Recommended Posts

Guest kilbad
This is part of my address book script that updates existing db entries.  Is there someway this part of my script be consolidated somehow?    Thanks in advance!

[code]   
          $id=mysql_escape_string($_POST["id"]);
          $letter=$_POST["letter"];
          $first=mysql_escape_string($_POST["first"]);
          $last=mysql_escape_string($_POST["last"]);
          $address1=mysql_escape_string($_POST["address1"]);
          $address2=mysql_escape_string($_POST["address2"]);
          $city=mysql_escape_string($_POST["city"]);
          $state=mysql_escape_string($_POST["state"]);
          $zip=mysql_escape_string($_POST["zip"]);
          $country=mysql_escape_string($_POST["country"]);
          $phone1=mysql_escape_string($_POST["phone1"]);
          $phone2=mysql_escape_string($_POST["phone2"]);
          $email1=mysql_escape_string($_POST["email1"]);
          $email2=mysql_escape_string($_POST["email2"]);
          $website1=mysql_escape_string($_POST["website1"]);
          $website2=mysql_escape_string($_POST["website2"]);
          $notes=mysql_escape_string($_POST["notes"]);
          $aim_sn=mysql_escape_string($_POST["aim_sn"]);

          mysql_query("UPDATE contactbook SET first = '$first' WHERE id = '$id'");
          mysql_query("UPDATE contactbook SET last = '$last' WHERE id = '$id'");
          mysql_query("UPDATE contactbook SET address1 = '$address1' WHERE id = '$id'");
          mysql_query("UPDATE contactbook SET address2 = '$address2' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET city = '$city' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET state = '$state' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET zip = '$zip' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET country = '$country' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET phone1 = '$phone1' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET phone2 = '$phone2' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET email1 = '$email1' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET email2 = '$email2' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET website1 = '$website1' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET website2 = '$website2' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET notes = '$notes' WHERE id = '$id'");
  mysql_query("UPDATE contactbook SET aim_sn= '$aim_sn' WHERE id = '$id'");
[/code]

Share this post


Link to post
Share on other sites
[code]foreach ($_POST as $key => $value) {
if ($key != "Submit") {
$$key = mysql_real_escape_string($value);
}
}

$query = "
UPDATE contactbook SET
first = '$first',
last = '$last',
address1 = '$address1',
address2 = '$address2',
city = '$city',
state = '$state',
zip = '$zip',
country = '$country',
phone1 = '$phone1',
phone2 = '$phone2',
email1 = '$email1',
email2 = '$email2',
website1 = '$website1',
website2 = '$website2',
notes = '$notes',
aim_sn= '$aim_sn',
WHERE id = '$id'";
$result = mysql_query($query) or die(mysql_error());[/code]
 

Share this post


Link to post
Share on other sites
As you are doing the same thing over and over on each line of code. Use a foreach loop which will do all the hard work for you:

[code=php:0]// get the id from the POST array. This will be used latter
$id = mysql_real_escape_string($_POST['id']);

// remove the id from $_POST array. As we dont need it any more.
unset($_POST['id']);

// now the fun starts! Put PHP into auto mode!
foreach($_POST as $key => $value)
{
    $value = mysql_real_escape_string($value);

    mysql_query("UPDATE contactbook SET " . $key . " = '" . $value . "' WHERE id='" . $id . "'");
}[/code]

Thats it!

NOTE in order for this code to work the form fields will have to have the same name as the name of the fields in your contactbook table.

Share this post


Link to post
Share on other sites
Guest kilbad
This was just what I needed!  Thank you so much!

Share this post


Link to post
Share on other sites
too many queries in there!!!!

[code]

<?php
// get the id from the POST array. This will be used latter
$id = mysql_real_escape_string($_POST['id']);

// remove the id from $_POST array. As we dont need it any more.
unset($_POST['id']);

$str = NULL;

// now the fun starts! Put PHP into auto mode!
foreach($_POST as $key => $value)
{
$value = !empty($value) ? "'" . mysql_real_escape_string($value) . "'" : "NULL";
$ftr .= is_null($str) ? "`" . $key . "` = "  . $value: ", `" . $key . "`= "  . $value;
}

$qry = mysql_query("UPDATE contactbook SET " . $str . " WHERE id='" . $id . "'");

?>
[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.