php-newbies Posted September 23, 2011 Share Posted September 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/ Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272097 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 Look closely at this and see if you see anything that would cause a comparison to fail . . . $query = " . . . WHERE cf_uid='$cf_uid '"; Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272100 Share on other sites More sharing options...
php-newbies Posted September 23, 2011 Author Share Posted September 23, 2011 Look closely at this and see if you see anything that would cause a comparison to fail . . . $query = " . . . WHERE cf_uid='$cf_uid '"; Thanks for your response. Sorry I did not understand what you mean. I need a pointer. Correction welcome. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272126 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 Is 'string' identical to 'string ' ? Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272127 Share on other sites More sharing options...
php-newbies Posted September 23, 2011 Author Share Posted September 23, 2011 Is 'string' identical to 'string ' ? ok, I get what you mean. I have removed the single quote ie WHERE cf_uid=$cf_uid but is still not updating the records. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272138 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272142 Share on other sites More sharing options...
php-newbies Posted September 23, 2011 Author Share Posted September 23, 2011 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='[email protected]',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. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272149 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272157 Share on other sites More sharing options...
php-newbies Posted September 23, 2011 Author Share Posted September 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272164 Share on other sites More sharing options...
php-newbies Posted September 23, 2011 Author Share Posted September 23, 2011 I have a weird problem. Though is updating but it updates all other rows with same records. The idea was to update only records related to the user_id. Any ideas please Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272185 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 It should update only records with a cf_uid value matching the value in the WHERE clause. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272187 Share on other sites More sharing options...
php-newbies Posted September 23, 2011 Author Share Posted September 23, 2011 It should update only records with a cf_uid value matching the value in the WHERE clause. Yes that is what it suppose to do but it update other cf_uid making all records look the same Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272192 Share on other sites More sharing options...
Pikachu2000 Posted September 23, 2011 Share Posted September 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272198 Share on other sites More sharing options...
php-newbies Posted September 23, 2011 Author Share Posted September 23, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247719-problem-updating-database-records/#findComment-1272202 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.