Jump to content

edit.php changing the id no


sblake161189

Recommended Posts

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

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.

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

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

 

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.