sblake161189 Posted February 3, 2011 Share Posted February 3, 2011 Hi Guys, I have a list of branches in a database table with the following collumns, Ter BranchName BranchAddress BranchTel BranchEmail BranchLink Ter = Terriotory ID However every time i update a branch using the edit.php code it always sets the Ter as 1 again! Rather than leaving it the same. The actual field is read-only to prevent that from happening. It then always comes up with the error Duplicate entry '1' for key 1 but thats because there is already a branch with Ter=1 include('config.php'); if (isset($_GET['Ter']) ) { $ter = (int) $_GET['Ter']; if (isset($_POST['submitted'])) { foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } $sql = "UPDATE `ter` SET `Ter` = `BranchName` = '{$_POST['BranchName']}' , `BranchAddress` = '{$_POST['BranchAddress']}' , `BranchTel` = '{$_POST['BranchTel']}' , `BranchEmail` = '{$_POST['BranchEmail']}' , `BranchLink` = '{$_POST['BranchLink']}' WHERE `Ter` = '$ter' "; mysql_query($sql) or die(mysql_error()); echo (mysql_affected_rows()) ? "Edited Branch.<br />" : "Nothing changed. <br />"; } $row = mysql_fetch_array ( mysql_query("SELECT * FROM `ter` WHERE `Ter` = '$ter' ")); <form action='' method='POST'> <p><b>Territory:</b><br /><input name='Ter' type='text' value='<?= stripslashes($row['Ter']) ?>' size="3" readonly="readonly" /> <p><b>Branch Name:</b><br /><input name='BranchName' type='text' value='<?= stripslashes($row['BranchName']) ?>' size="50" /> <p><b>Address:</b><br /> <textarea name="BranchAddress" cols="40" rows="5"><?= stripslashes($row['BranchAddress']) ?></textarea> <p><b>Telephone:</b><br /><input name='BranchTel' type='text' value='<?= stripslashes($row['BranchTel']) ?>' size="15" /> <p><b>Email:</b><br /><input name='BranchEmail' type='text' value='<?= stripslashes($row['BranchEmail']) ?>' size="50" /> <p><b>Link:</b><br /><input name='BranchLink' type='text' value='<?= stripslashes($row['BranchLink']) ?>' size="50" /> <p><input type='submit' value='Save' /><input type='hidden' value='1' name='submitted' /> </form> <? } ?> Any Ideas? Cheers, S Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/ Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Share Posted February 3, 2011 If a field is read-only you will not be able to edit it anyway. Also is it the Ter field that you want to edit or is this the field that is giving you the issue? James. Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/#findComment-1169312 Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Share Posted February 3, 2011 Also; $sql = "UPDATE `ter` SET `Ter` = `BranchName` = '{$_POST['BranchName']}' , `BranchAddress` = '{$_POST['BranchAddress']}' , `BranchTel` = '{$_POST['BranchTel']}' , `BranchEmail` = '{$_POST['BranchEmail']}' , `BranchLink` = '{$_POST['BranchLink']}' WHERE `Ter` = '$ter' "; SET `Ter` = ?? this should be SET `Ter` = '' OR SET `Ter` = '{$_POST['Ter']}', James. Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/#findComment-1169314 Share on other sites More sharing options...
sblake161189 Posted February 3, 2011 Author Share Posted February 3, 2011 Hi, Sorry I probably didnt make myself clear enough... Ter is the effiectively the ID no of the branch. I want it to appear on the edit page but make it read-only so the user doesn't accidently edit it by mistake but it needs to be visable. However when I click submit after making ammendments to any of the other fields (which are not read only). I get the error: Duplicate entry '1' for key 1 because it is trying to change Ter to equal 1 but it wont let it because Ter is the primary key. Does that make more sense? Ta, S Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/#findComment-1169315 Share on other sites More sharing options...
sblake161189 Posted February 3, 2011 Author Share Posted February 3, 2011 SET `Ter` = ?? this should be SET `Ter` = '' OR SET `Ter` = '{$_POST['Ter']}', Hi Thanks, Didnt spot that so thanks... I have now changed it to $sql = "UPDATE `ter` SET `Ter` = '{$_POST['Ter']}' , '{$_POST['BranchName']}' , `BranchAddress` = '{$_POST['BranchAddress']}' , `BranchTel` = '{$_POST['BranchTel']}' , `BranchEmail` = '{$_POST['BranchEmail']}' , `BranchLink` = '{$_POST['BranchLink']}' WHERE `Ter` = '$ter' "; However it now says: 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 ''Ter 1' , `BranchAddress` = 'fghgfh' , `BranchTel` = '' , `BranchEmail` = '' at line 1 Ta, S Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/#findComment-1169316 Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Share Posted February 3, 2011 Ah I see what you are trying to do. Try this; include('config.php'); if(isset($_GET['Ter'])) { $ter = (int) $_GET['Ter']; if(isset($_POST['submitted'])) { foreach($_POST AS $key => $value) { $_POST[$key] = mysql_real_escape_string($value); } /* UPDATE `ter` SET In this instance `ter` being the table I assume? */ $sql = "UPDATE `ter` SET `BranchName` = '{$_POST['BranchName']}' , `BranchAddress` = '{$_POST['BranchAddress']}' , `BranchTel` = '{$_POST['BranchTel']}' , `BranchEmail` = '{$_POST['BranchEmail']}' , `BranchLink` = '{$_POST['BranchLink']}' WHERE `Ter` = '$ter' "; mysql_query($sql) or die(mysql_error()); echo (mysql_affected_rows()) ? "Edited Branch.<br />" : "Nothing changed. <br />"; } $row = mysql_fetch_array ( mysql_query("SELECT * FROM `ter` WHERE `Ter` = '$ter' ")); <form action='' method='POST'> <p><b>Territory:</b><br /><?php echo stripslashes($row['Ter']); ?> <p><b>Branch Name:</b><br /><input name='BranchName' type='text' value='<?= stripslashes($row['BranchName']) ?>' size="50" /> <p><b>Address:</b><br /> <textarea name="BranchAddress" cols="40" rows="5"><?= stripslashes($row['BranchAddress']) ?></textarea> <p><b>Telephone:</b><br /><input name='BranchTel' type='text' value='<?= stripslashes($row['BranchTel']) ?>' size="15" /> <p><b>Email:</b><br /><input name='BranchEmail' type='text' value='<?= stripslashes($row['BranchEmail']) ?>' size="50" /> <p><b>Link:</b><br /><input name='BranchLink' type='text' value='<?= stripslashes($row['BranchLink']) ?>' size="50" /> <p><input type='submit' value='Save' /><input type='hidden' value='1' name='submitted' /> </form> <? } ?> James. Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/#findComment-1169317 Share on other sites More sharing options...
sblake161189 Posted February 3, 2011 Author Share Posted February 3, 2011 Aha, Cheers! Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/#findComment-1169318 Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Share Posted February 3, 2011 It's a simple case of using $_GET['Ter'] to set a var, E.G; $terID = $_GET['Ter']; // This is now the Ter ID of the field that will be edited. $update = "UPDATE `ter` SET `what ever you want` WHERE `Ter` = '$terID' LIMIT 1"; James. Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/#findComment-1169319 Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Share Posted February 3, 2011 No problem, my pleasure (: Let me know if there's any problems. James. Link to comment https://forums.phpfreaks.com/topic/226551-editphp-changing-the-id-no/#findComment-1169320 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.