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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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