Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error in updating MySQL data using PHP 1

Status
Not open for further replies.

brogan

Technical User
Oct 7, 2003
44
GB
I have a major problem with the script below. The chosen data to be edited would be taken from the MySQL database and placed in the text boxes. I would like to be able to change this data and then select "make changes". However, all the data in that table is replaced with copies of the data that has just been changes.

This has really frustrated me, any help would be greatly appreciated.

Mark

<?php
if(isset($_GET['exe'])) {
mysql_connect("??????", "??????", "???????");
mysql_select_db('property');

$typ = $_GET['type'];
$add = $_GET['address'];
$are = $_GET['area'];
$bed = $_GET['bedrooms'];
$bat = $_GET['bathrooms'];
$ren = $_GET['rent'];
$new = $_GET['new_rent'];
$des = $_GET['description'];
//$ima = $_GET['image'];
$map = $_GET['map'];



print "$typ";
print "$add";
print "$are";
print "$bed";
print "$bat";
print "$ren";
print "$new";
print "$des";
//print "$ima";
print "$map";
print "property successfully changed";

$result = mysql_query(
"update property set type = '$typ', address= '$add', area= '$are', bedrooms = '$bed', bathrooms = '$bat', rent = '$ren', new_rent= '$new', description= '$des', map= '$map'");
} else {
$row = $_GET['id'];
//print_r($row);
$detail = explode(',',$row);
//print_r($detail);//prints out array

?>

</p>
<form name="form1" method="get"
action="change.php">

<p><br>
Type:
<input type="text" name="type" value="<? echo $detail[1]; ?>">
</p>
<p><br>
Address:
<input type="text" name="address" value="<? echo $detail[2]; ?>">
</p>
<p><br>
Area:
<input type="text" name="area" value="<? echo $detail[3]; ?>">
</p>
<p><br>
Bedrooms:
<input type="text" name="bedrooms" value="<? echo $detail[4]; ?>">
</p>
<p><br>
Bathrooms:
<input type="text" name="bathrooms" value="<? echo $detail[5]; ?>">
<br>

<br>
Rent:
<input type="text" name="rent" value="<? echo $detail[6]; ?>">
<br>

<br>
New Rent:
<input type="text" name="new_rent" value="<? echo $detail[7]; ?>">

0-100, 101-150, 151-200, 201-250, 251-300 etc<br>
</p>
<p>Description:

<input type="text" name="description" value="<? echo $detail[8]; ?>">
</p>
<p>&nbsp;</p>
<p>Map:
<input type="text" name="map" value="<? echo $detail[9]; ?>">
</p>



<p><br>

<input type="submit" name="exe"
value="Make Changes">
</p>
</form>

<? } ?>
 
the problem is that you execute the "update table" if the "Make Changes" button is pressed. What you need is to add another php file in order to confirm (or reject) the changes and *then* make the update, example:

<html>
<form name="form" action="confirm.php">
<inputs...>

<input type="submit" name="exe" value="Make Changes">

**************
confirm.php

$var1=$_POST['var1'];
$var2=$_POST['var2'];
....
$result = mysql_query(
"update property set type = '$typ', address= '$add', area= '$are', bedrooms = '$bed', bathrooms = '$bat', rent = '$ren', new_rent= '$new', description= '$des', map= '$map'");
you entered:
<form name="form2" action="update.php" method="POST">
$_GET['var1']
$_GET['var2']
$_GET['var3']
<input type="hidden" name="var1" value=<?php echo $_GET['var1'] ?>
<input type="hidden" name="var1" value=<?php echo $_GET['var1'] ?>

...
<input type="submit" name="exe" value="Make Changes">
</html>

********+
update.php:

if
 
hell... tek-tips cut my post.

update.php:

$var1=$_GET['var1'];
$var2=$_GET['var2'];
...

$result = mysql_query(
"update property set type = '$typ', address= '$add', area= '$are', bedrooms = '$bed', bathrooms = '$bat', rent = '$ren', new_rent= '$new', description= '$des', map= '$map'");
***************


BTW: the update IS NOT part of confirm.php... I don't know what happened to my post.

 
gosh... my post is really confusing.. sorry for that. what I'm trying to say is you need 3 files:

1. the form (get or post) action=confirm.php
2. confirm.php will have a form with action=update.php method=POST and a form with only hidden vars
3. update.php will read all the post vars and make the update

so file 2 should be something like:

confirm.php
<html>
you entered:
<form name="form2" action="update.php" method="POST">
$_GET['var1']
$_GET['var2']
$_GET['var3']
<input type="hidden" name="var1" value=<?php echo $_GET['var1'] ?>
<input type="hidden" name="var1" value=<?php echo $_GET['var1'] ?>
...
<input type="submit" name="exe" value="Make Changes">
</html>
 
Chacalinc
Please consider using the TGML markup for code sections: [ignore]
Code:
.....
This will make your code postings more readable as it keeps formatting like indentation. Thanks for your contribution.
 
So, now I meesed up my tags. It must be this thread, some kind of a sigital curse.
The tags are [ignore]
Code:
.....
[/ignore]
 
hehehe :) don't worry, I know about tags, but in this case I was thinking in the solution.

Anyway, thanks for the tip.

Cheers.
 
Here's what I have been doing lately. The following code is taken from a PHP file that takes updates from a form. This chunk is in the middle of a switch statement based on the value of the 'Submit' button:
Code:
case 'Update Contact':
     $tmp = array();
     $q = "select * from contacts where ind='".
          $_POST['ind'] . "'";
     $rs = @mysql_query($q);
     $rw = @mysql_fetch_assoc($rs);
     foreach ($_POST as $key =>$value) { 
       switch ($key) {
          case 'member':
          case 'memberemail':
            $enc_val = urlencode(stripslashes(strtolower(trim($value))));
            if ($enc_val != $rw[$key])
               $tmp[] = '`' . $key . "`='" . $enc_val . "'";
            break;
          case 'memberphone':
          case 'membercell':
            $enc_val = urlencode(stripslashes(trim($value)));
            $enc_val != $rw[$key])
            $tmp[] = '`' . $key . "`='" . $enc_val . "'";
            break;
          }
      }
      if (!empty($tmp)) {
        $q = "update contacts set " . implode(',',$tmp) . " where ind='" . $_POST['ind'] . "'";
        $rs = @mysql_query($q);
        if (!$rs) {
           $err_msg[] = 'Error updating Contact <span class=bold>' . ucwords(stripslashes(trim($_POST['member']))) . '</span> in group ' . $_POST['group'] . ", query: $q<br>".mysql_error();
            unset($_POST['submit']); }
         else $msg[] = 'Contact <span class=bold>' .
                ucwords(stripslashes(trim($_POST['member']))) .
                '</span> in group <span class=bold>' . $_POST['group'] . '
                </span> updated ok';
         }
else $msg[] = 'No updates needed for Contact <span class=bold>' . 
                ucwords(stripslashes(trim($_POST['member']))) . 
                '</span> in group ' . $_POST['group'];
break;
Basically, this code compares each POSTED value to the value in the database and only creates the "SET" substring if they are different.

The values POSTed from the form have the same names as the columns in the database.

BTW, my nice neat indentations probably got screwed up in the posting of the code... :)

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top