php-newbies Posted September 24, 2011 Share Posted September 24, 2011 I have 3 set of pages. The first html page only contains a search textbox and a submit button which then call up the second page search.php which populate html table with search result. The third page is update.php which allows to update the records populated by search.php. The problem is that it when it update the records it automatically updates all rows in the table making all records look the same. I do not know where I have made a mistake please help. When I use phpadmin to update the records no problem as I can specifically input the cf_id number directly. Search.php code <?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="updatecus.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 $row[0];?></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="OS" type="text" value="<?php echo $OS; ?>"?> </td></tr> <tr><td width="45%" class="FormText">Appointment:</td> <td width="55%"><input name="Appointment_date" 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 ?> update.php 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_id = $_POST['cf_id']; $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']; if (isset($_POST['submit'])) { $query ="UPDATE Customer_Registration 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_id=cf_id"or die (mysql_error()); echo $query; mysql_query($query) or die(mysql_error()); //mysql_close($con); echo "<p>Congrats Record Updated</p>"; } ?> Thank you in advance for your help Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/ Share on other sites More sharing options...
jcbones Posted September 24, 2011 Share Posted September 24, 2011 You missed a $ in this line: $query ="UPDATE Customer_Registration 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_id=cf_id"or die (mysql_error()); I think it should be: $query ="UPDATE Customer_Registration 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_id=$cf_id"or die (mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272337 Share on other sites More sharing options...
php-newbies Posted September 24, 2011 Author Share Posted September 24, 2011 You missed a $ in this line: $query ="UPDATE Customer_Registration 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_id=cf_id"or die (mysql_error()); I think it should be: $query ="UPDATE Customer_Registration 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_id=$cf_id"or die (mysql_error()); Thanks for your response, I have tried that but got a syntax error Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272348 Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 When an error is returned, it's helpful to paste the error here. Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272350 Share on other sites More sharing options...
php-newbies Posted September 24, 2011 Author Share Posted September 24, 2011 When an error is returned, it's helpful to paste the error here. ok, error msg below. thanks UPDATE Customer_Registration SET Firstname='Mark',lastname='Peterson',Address='pitfield',Postcode='EC2 5GH',Phone='000245876',Email='blackogbe@yahoo.com',Customer_Type='Home',Computer_type='Laptop',Computer_maker='Acer',Model='aspire 1241',OS='Windows7', Appointment_date='2011-09-28 11:08:31',Problem='344bgte',Solution='I like working with mark',Comment='hello' WHERE cf_id=You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 2 Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272351 Share on other sites More sharing options...
Pikachu2000 Posted September 24, 2011 Share Posted September 24, 2011 You aren't passing the value of cf_id through the form, so it isn't available in the next script. Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272359 Share on other sites More sharing options...
jcbones Posted September 24, 2011 Share Posted September 24, 2011 I'm sorry, it is my fault: $query ="UPDATE Customer_Registration 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_id=$cf_id"or die (mysql_error()); You shouldn't add the last part to the variable. $query ="UPDATE Customer_Registration 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_id=$cf_id"; Now, if cf_id is not listed as an integer field, then you should enclose it in single quotes. WHERE cf_id='$cf_id' I re-instate what Pik says also. You need to pass the cf_id through your form. Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272361 Share on other sites More sharing options...
php-newbies Posted September 24, 2011 Author Share Posted September 24, 2011 I'm sorry, it is my fault: $query ="UPDATE Customer_Registration 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_id=$cf_id"or die (mysql_error()); You shouldn't add the last part to the variable. $query ="UPDATE Customer_Registration 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_id=$cf_id"; Now, if cf_id is not listed as an integer field, then you should enclose it in single quotes. WHERE cf_id='$cf_id' I re-instate what Pik says also. You need to pass the cf_id through your form. Thanks again for your help, but how do I pass the value of cf_id in the form "search.php" and the value location. Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272387 Share on other sites More sharing options...
jcbones Posted September 24, 2011 Share Posted September 24, 2011 <input type="hidden" name="cf_id" value="<?php echo $row[0]; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272394 Share on other sites More sharing options...
php-newbies Posted September 24, 2011 Author Share Posted September 24, 2011 <input type="hidden" name="cf_id" value="<?php echo $row[0]; ?>" /> I appreciate all the help from you and Pikachu2000. This is what is happening. I have 3 user cf_ids in the table namely 48, 47 and 51 all these 3 have different first names and last names etc cf_id 48 mark as first name cf_id 47 Peter as first name cf_id 51 Lisberth as first name Say I search Lisberth and it loads all its records after making a few changes I hit the submit button. these is what happens Liberth replaces Mark as first name as well other records on the column Liberth replaces Peter as first name as well other records on the column If I search for Mark or Peter I get empty textboxes. I do not know what is going on really. Pulling my hair off. It just deletes or previous records and replaces it with it own. Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272415 Share on other sites More sharing options...
php-newbies Posted September 24, 2011 Author Share Posted September 24, 2011 <input type="hidden" name="cf_id" value="<?php echo $row[0]; ?>" /> I appreciate all the help from you and Pikachu2000. Working now. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/247775-php-update-query-changed-all-rows-please-help/#findComment-1272421 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.