Hi,

The supervisor of my system wants to modify customer details. I created this modify_form.php page.

But when i modifies one record of a particular customer it affected to all the records. Therefore all the records contain same thing.....

Can anyone give me the correct coding which modifies only that record.

This is my modify_form.php page.

<?php
	  
	  $connect=mysql_connect("localhost","root","");
      mysql_select_db("bank",$connect) or die ("could not select database");
	  
	  
	  
	             $nic = '';
                 $full_name = '';
                 $name_with_initials = '';
                 $address = '';
                 $contact_number = '';
                 $gender = '';
				 
      
                $nic =$_POST['nic'];
                $full_name =$_POST['full_name'];
                $name_with_initials =$_POST['name_with_initials'];
                $address =$_POST['address'];
                $contact_number =$_POST['contact_number'];
                $gender =$_POST['gender'];

                

             $result = mysql_query("UPDATE  customer SET nic = '$nic', full_name = '$full_name', name_with_initials =  '$name_with_initials',                                   address = '$address', contact_number = '$contact_number', gender  = '$gender'") ;                 

               if($result){
                  echo "Your information has been successfully added to the database.";
              }else{
                 echo "Failed";
              }

?>

Thanks,

Recommended Answers

All 12 Replies

But when i modifies one record of a particular customer

you need to also submit the id of that particular customer. Assuming each customer record has unique key (PRIMARY KEY) named 'cust_id', then on you also need to include it on your form (perhaps as a hidden field). So when the info is posted, your php script above will see the cust_id for the selected user and in your UPDATE statement you need to limit the affected records to just the selected customer by issuing a "... WHERE cust_id=$_POST['cust_id']" in your update statement.

BTW: If you look at my first post in your earlier thread, you will see how I included a hidden field name tran_ID for each of your rows. This is the same technique I am describing above.

I tried that. But a parsing error occurred in this line of modify_form.php page.

$result = mysql_query("UPDATE  customer SET nic = '$nic', full_name = '$full_name', name_with_initials =  '$name_with_initials',                                   address = '$address', contact_number = '$contact_number', gender  = '$gender'" where customer_id=$_POST[                                   'customer_id']") ;

This is my form.

<form action="modify_form.php" method="post" name="form1" id="form1">
    <fieldset>
      <legend class="cap"> Customer Details</legend>
      <table width="85%" height="350" border="0" align="center" cellpadding="5" cellspacing="0">
      <?php
	  
	   
      
     //initilize variables
	             $var_customer_id='';
                 $var_nic = '';
                 $var_full_name = '';
                 $var_name_with_initials = '';
                 $var_address = '';
                 $var_contact_number = '';
                 $var_gender = '';
				 
                 if(isset($_POST['customer_id'])) {
                 $customer_id=(int)$_POST['customer_id'];
				 
                 $query = "select * from customer where customer_id=$customer_id LIMIT 1";
                 $result = mysql_query($query) or die(mysql_error());
                 if(mysql_num_rows($result) < 1) {
                       echo 'The record could not be found.';
                       }else {
					   $row=mysql_fetch_array($result);
					   
				// replace blank variables with variables from the database	  
				$var_customer_id = $row['customer_id'];
                $var_nic = $row['nic'];
                $var_full_name = $row['full_name'];
                $var_name_with_initials = $row['name_with_initials'];
                $var_address = $row['address'];
                $var_contact_number = $row['contact_number'];
                $var_gender = $row['gender'];
  }
}
		   
		   
	  ?>
      
        <tr>
          <td>&nbsp;</td>
          <td class="title02">&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr height="30">
          <td width="2%" height="35">&nbsp;</td>
          <td width="46%" class="title02" align="left">Customer ID</td>
          <td width="50%" class="attribute1" align="left"><input type="hidden" name="customer_id" size="30"   class="attribute1" value="<?php echo $var_customer_id; ?>"></td>
          <td width="2%">&nbsp;</td>
        </tr>
        <tr height="30">
          <td width="2%" height="35">&nbsp;</td>
          <td width="46%" class="title02" align="left">National ID</td>
          <td width="50%" class="attribute1" align="left"><input type="text" name="nic" size="30"   class="attribute1" value="<?php echo $var_nic; ?>"></td>
          <td width="2%">&nbsp;</td>
        </tr>
        <tr height="30">
        <td height="33">&nbsp;</td>
        <td width="46%" class="title02" align="left">Full Name</td>
        <td width="50%" class="attribute1" align="left"><input type="text" name="full_name" size="30"   class="attribute1" value="<?php echo $var_full_name; ?>"></td>
          <td width="2%">&nbsp;</td>
        </tr>
        <tr height="30">
          <td height="34">&nbsp;</td>
          <td class="title02" align="left">Name With Initials</td>
          <td width="50%" class="attribute1" align="left"><input type="text" name="name_with_initials" size="30"   class="attribute1" value="<?php echo $var_name_with_initials; ?>"></td>
        </tr>
        <tr height="30">
          <td width="2%">&nbsp;</td>
          <td width="46%" class="title02" align="left">Address</td>
          <td width="50%" class="attribute1" align="left"><label>
            <textarea name="address" id="textarea" cols="45" rows="5"><?php echo $var_address; ?></textarea>
          </label></td>
          <td width="2%">&nbsp;</td>
        </tr>
        <tr height="30">
          <td width="2%">&nbsp;</td>
          <td width="46%" class="title02" align="left">Contact Number</td>
          <td width="50%" class="attribute1" align="left"><input type="text" name="contact_number" size="30" class="attribute1" value="<?php echo $var_contact_number; ?>"></td>
          <td width="2%">&nbsp;</td>
        </tr>
        <tr height="30">
          <td width="2%">&nbsp;</td>
          <td width="46%" class="title02" align="left">Sex</td>
          <td width="50%" class="attribute1" align="left"><p>
		   <select name="gender" id="jumpMenu" >
          <option selected="selected"><?php echo $var_gender; ?></option>
            
            <option>Male</option>
            <option>Female</option>
          </select>          
          <td width="50%" class="attribute1" align="left">&nbsp;</td>
            
            <br />
          </p></td>
          <td width="2%">&nbsp;</td>
        </tr>
        
      </table>
      <p align="center">&nbsp;</p>
      <p align="center">
        <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>
        <label>
          <input name="button" type="submit" id="button"  value="Approve Account"  />&nbsp;&nbsp;&nbsp;
          <label>
          <input name="button2" type="submit" id="button2"  value="Modify Account"  />
          </label>
          &nbsp;&nbsp;
        <label> <a href="accsup_help.php">
          <input name="button" type="submit"  id="button"   value="Help"  />
        </a></label>
        </td>
      </p>
    </fieldset>
    <td width="5%">&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td align="center">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><font color="red" size="1" ></font></td>
      <td>&nbsp;</td>
    </tr>
    
      
    </table>
  </form>

You have an extra quote in your query: gender = '$gender'" <-- remove this double quote

:

$result = mysql_query("UPDATE  customer SET nic = '$nic', full_name = '$full_name', name_with_initials =  '$name_with_initials', address = '$address', contact_number = '$contact_number', gender  = '$gender' where customer_id=$_POST['customer_id']")

I removed that extra double quote. But then it gives following error.

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in C:\wamp\www\php files\modify_form.php on line 25

don't forget about the or die(mysql_error() ); whenever you execute a query:

$result = mysql_query("UPDATE  customer 
					SET	nic = '$nic'
						, full_name = '$full_name'
						, name_with_initials =  '$name_with_initials'
						, address = '$address'
						, contact_number = '$contact_number'
						, gender  = '$gender' 
					WHERE customer_id=$_POST['customer_id']")  or die( mysql_error() );

As for your parse_error, you would need to post your php code for modify_form.php for us to know what line is #25.

This is the line.

$result = mysql_query("UPDATE  customer SET nic = '$nic', full_name = '$full_name', name_with_initials =  '$name_with_initials',                                   address = '$address', contact_number = '$contact_number', gender  = '$gender' where customer_id=$_POST[                                   'customer_id']") or die (mysql_error()) ;

copy and paste the following:

$result = mysql_query("UPDATE  `customer` SET `nic` = '{$nic}', `full_name` = '{$full_name}', `name_with_initials` =  '{$name_with_initials}',                                   `address` = '{$address}', `contact_number` = '{$contact_number}', `gender`  = '{$gender}' where `customer_id`={$_POST['customer_id']}") or die (mysql_error()) ;

@ hielo, thanks a lot.it worked....

Can you explain what is the use of "{}" in this code?

it forces the interpreter to treat the "stuff" between {} as a variable. The reason it was chocking is because you have $_POST within a double quoted string, but for consistency I added it to all the other variables as well.

Thanks a lot...

You are welcome. Don't forget to mark the thread as solved.

Regards,
Hielo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.