Jump to content

Problem updating database records


php-newbies

Recommended Posts

Hello all.

 

Please I need help to update customer table. I get the texboxes populated with select query no problem.

I have html page with a search box and when the user enter their first name or last name and click search it will call up the search.php file here is the file:

 

This code below populate the textboxes to be updated:

 

<?php


     if(isset($_POST['submit'])){ 
     if(isset($_GET['go'])){ 
     if(preg_match("^/[A-Za-z]+/", $_POST['name'])){ 
      $name=$_POST['name']; 
     } 
     } 
     else{ 
     echo  "<p>Please enter a search query</p>"; 
     } 
   }
//connect  to the database 


$con = mysql_connect("localhost","dbusrn","dbpwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydb", $con);
$name = $_POST['name'];
$result = mysql_query ("select * from Customer_Registration where Firstname like '%$name%' or lastname like '%$name%' ");



$row = mysql_fetch_row($result);
$cf_uid = $row[0];
$Firstname = $row[6];
$lastname = $row[7];
$Address = $row[8];
$Postcode = $row[9];
$Phone = $row[10];
$Email = $row[11];
$Customer_Type = $row[12];
$Computer_type = $row[13];
$Computer_maker = $row[14];
$Model = $row[15];
$OS = $row[16];
$Appointment_date = $row[17];
$Problem = $row[18];
$Solution = $row[19];
$Comment = $row[20];
?>



<form action="updatecus1.php" method="post">
      
      <table width="100%" border="2" cellspacing="0" cellpadding="8">
      
      <tr><td width="45%" class="FormText">Customer ID:</td>
<td width="55%"><?php echo $cf_uid;?></td></tr>
<tr><td width="45%" class="FormText">First name:</td>
<td width="55%"><input  name="Firstname" type="text" value="<?php echo $Firstname;?>"?> </td></tr>


<tr><td width="45%" class="FormText">Last name:</td>
<td width="55%"><input  name="lastname" type="text" value="<?php echo $lastname;?>"?> </td></tr>



<tr><td width="45%" class="FormText">Address:</td>
<td width="55%"><input  name="Address" type="text" value="<?php echo $Address; ?>"?> </td></tr>

<tr><td width="45%" class="FormText">Postcode:</td>
<td width="55%"><input  name="Postcode" type="text" value="<?php echo $Postcode; ?>"?> </td></tr>


<tr><td width="45%" class="FormText">Phone:</td>
<td width="55%"><input  name="Phone" type="text" value="<?php echo $Phone; ?>"?> </td></tr>

<tr><td width="45%" class="FormText">Email:</td>
<td width="55%"><input  name="Email" type="text" value="<?php echo $Email; ?>"?> </td></tr>

<tr><td width="45%" class="FormText">Customer:</td>
<td width="55%"><input  name="Customer_type" type="text" value="<?php echo $Customer_Type; ?>"?> </td></tr>

<tr><td width="45%" class="FormText">Computer :</td>
<td width="55%"><input  name="Computer_type" type="text" value="<?php echo $Computer_type; ?>"?> </td></tr>

<tr><td width="45%" class="FormText">Manufactural:</td>
<td width="55%"><input  name="Computer_maker" type="text" value="<?php echo $Computer_maker; ?>"?> </td></tr>

<tr><td width="45%" class="FormText">Model:</td>
<td width="55%"><input  name="Model" type="text" value="<?php echo $Model; ?>"?> </td></tr>

<tr><td width="45%" class="FormText">Operating System:</td>
<td width="55%"><input  name="First name" type="text" value="<?php echo $OS; ?>"?> </td></tr>

<tr><td width="45%" class="FormText">Appointment:</td>
<td width="55%"><input  name="Operating System" type="text" value="<?php echo $Appointment_date; ?>"?> </td></tr>

  <tr><td width="45%" class="FormText">Problem:</td>
<td width="55%"><textarea name="Problem" rows="10" cols="20" ><?php echo $Problem; ?></textarea></td></tr>

<tr><td width="45%" class="FormText">Solution:</td>
<td width="55%"><textarea name="Solution" rows="10" cols="20" ><?php echo $Solution; ?></textarea></td></tr>

<tr><td width="45%" class="FormText">Comment:</td>
<td width="55%"><textarea name="Comment" rows="10" cols="20" ><?php echo $Comment; ?></textarea></td></tr>


  
<td width="55%"><input name="submit"  value="submit"  type="submit" <br> <input type="Submit" value="Cancel"</br></td>


</form>
         <?php


 

--------------------------------------------------------------------------------------

The update.php file have the following code:

 

<?php
//connect  to the database 


$con = mysql_connect("localhost","dbusrn","dbpwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("mydb", $con);

if (isset($_POST['submit']))  {

$cf_uid  = $_POST['cf_uid'];
$Firstname = $_POST['Firstname'];
$lastname = $_POST['lastname'];
$Address = $_POST['Address'];
$Postcode = $_POST['Postcode'];
$Phone = $_POST['Phone'];
$Email = $_POST['Email'];
$Customer_Type = $_POST['Customer_Type'];
$Computer_type = $_POST['Computer_type'];
$Computer_maker = $_POST['Computer_maker'];
$Model = $_POST['Model'];
$OS = $_POST['OS'];
$Appointment_date = $_POST['Appointment_date'];
$Problem = $_POST['Problem'];
$Solution = $_POST['Solution'];
$Comment = $_POST['Comment'];



$query = "UPDATE Customer SET  Firstname='$Firstname',lastname='$lastname',Address='$Address',Postcode='$Postcode',Phone='$Phone',Email='$Email',Customer_Type='$Customer_Type',Computer_type='$Computer_type',Computer_maker='$Computer_maker',Model='$Model',OS='$OS',
Appointment_date='$Appointment_date',Problem='$Problem',Solution='$Solution',Comment='$Comment' WHERE cf_uid='$cf_uid '";


mysql_query($query) or die(mysql_error());
mysql_close($con);

echo "<p>Congrats Record Updated</p>";
}
?>

     

I got congrats record updated but in actual fact no record are updated.

 

Please help

 

MOD EDIT: code tags added.

Link to comment
Share on other sites

Ok, now its time to echo the query string and make sure it contains the values you'd expect it to contain. If it does, try pasting it into phpMyAdmin and see if you get a different outcome.

 

Thanks. I have echo the string. Result below:

 

UPDATE Customer_Registration SET Firstname='Dave',lastname='anderson',Address='56 Collington road',Postcode='SE34 5FG',Phone='078564258',Email='dave43@hotmail.com',Customer_Type='',Computer_type='Laptop',Computer_maker='Dell',Model='1534',OS='', Appointment_date='',Problem='My pc is really slow. driving my crazy',Solution='wref43g',Comment='wedwf4f' WHERE cf_uid=29

 

Congrats Record Updated

 

I noticed that Appointment_date, Customer_Type and OS does not return any value.  Still the table is not updated.

Link to comment
Share on other sites

As far as the empty values go, in your form markup, the name= attribute for the OS field is set to 'First name', and for appointment date it's set to 'Operating System'. For the issue of nothing being updated, I don't see anything syntactically wrong with the query, but you're running the SELECT query against the `Customer_Registration` table, then attempting to UPDATE the `Customer` table. Is that intentional? If it is, why have multiple tables with the same data?

Link to comment
Share on other sites

As far as the empty values go, in your form markup, the name= attribute for the OS field is set to 'First name', and for appointment date it's set to 'Operating System'. For the issue of nothing being updated, I don't see anything syntactically wrong with the query, but you're running the SELECT query against the `Customer_Registration` table, then attempting to UPDATE the `Customer` table. Is that intentional? If it is, why have multiple tables with the same data?

 

Thanks all working now lol.

Link to comment
Share on other sites

The only way that could happen, unless there's other code running somewhere, is if there is more than one record with the same cf_uid value.

 

first name and last name are different, but I have use the same email. I only have 2 set of code. The first one populate the form and the second update the records. I will try to use unique values and see what happens

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.