Jump to content

[SOLVED] MySQL Update Command Create New Record


DiFi80211

Recommended Posts

I have a simple webpage where I am trying to have the information loaded from a database into an html form (made with php) and then the user can change the information and update the database.  Everything works fine except for when the Update command executes.  Instead of changing the record that I selected, It adds a new record to the database.  Does anyone have any idea why this might be?  Also if you do not change the primary key field, the query will fail because it is trying to add the new record instead of updated the old one.

 

Create Nav Links
  print("<html>
         <head>
         <title>Update A Newsletter Article</title>
	 </head>
	 <body>
	 <center>
	<h2>Update A Newsletter Article</h2>
	<h4><a href='index.php'>Control Panel</a></h4>
	<br />
	</center>
	 ");

//Declare Variables
   $user = "*****";
   $password = "******";
   $host = "localhost";
   $Database = "*****";
   $Table = "Newsletter";
   $Title = $_POST["Title"];
   $LOrder = $_POST["ListOrder"];
   $Body = $_POST["Body"];
   
   $Title = str_replace('"', "*", $Title);
   $Body = str_replace('"', "*", $Body);

   

// Connect to MySQL
   $connect = @mysql_connect($host, $user, $password);
   
// Display Message if unable to connect to MySQL Service
   if (!$connect) {
      print( "Unable to connect to the " .
      "database server at this time." );
      exit();
   }

// Connect to Database
   mysql_select_db($Database, $connect);

//Display Message if unable to connect to Databse
   if (! @mysql_select_db($Database) ) {
      print( "Unable to locate the ". $Database .
      " database at this time." );
      exit();
   }

   print("<form action = 'AddNewsletter.php' method = 'post'>
		<table>
			<tr>
				<td>
					<h3>Order</h3>
				</td>
				<td>
					<input type = 'text' maxlength = '2' name = 'ListOrder' value = '" . $LOrder . "'>
				</td>
			</tr>
			<tr>
				<td>
					<h3>Title</h3>
				</td>
				<td>
					<input type = 'text' maxlength = '50' name = 'Title' value = '" . $Title . "'>
				</td>
			</tr>
			<tr>
				<td>
					<h3>Body</h3>
				</td>
				<td>
					<textarea rows= '10' cols='25' name = 'Body'>" . $Body . "</textarea>
				</td>
			</tr>
			<tr>
				<td><center>Select Unchanged Field</center><br />
					<input type='radio' name='UnChanged' value='Order'> Order <br />
					<input type='radio' name='UnChanged' value='Title'> Title <br />
					<input type='radio' name='UnChanged' value='Body'> Body <br />
			</tr>
			<tr>
				<td colspan = '2'>
					<input type = 'submit' value = 'Submit'>
				</td>
			</tr>
		</table>
");
}		
else{
//Declare Variables
   $user = "pma";
   $password = "techdirect";
   $host = "localhost";
   $Database = "PWBB";
   $Table = "Newsletter";
   $LOrder = $_POST["ListOrder"];
   $Title = $_POST["Title"];
   $Body = $_POST["Body"];
   $Unchanged = $_POST["UnChanged"];
   settype($LOrder, "integer");


// Connect to MySQL
   $connect = @mysql_connect($host, $user, $password);
   
// Display Message if unable to connect to MySQL Service
   if (!$connect) {
      print( "Unable to connect to the " .
      "database server at this time." );
      exit();
   }

// Connect to Database
   mysql_select_db($Database, $connect);

//Display Message if unable to connect to Databse
   if (! @mysql_select_db($Database) ) {
      print( "Unable to locate the ". $Database .
      " database at this time." );
      exit();
   }
   $Title2 = str_replace("*", '"', $Title);
   $Body2 = str_replace("*", '"', $Body);
   
   switch ($Unchanged){
	case "Order":
		$results = mysql_query("Update Newsletter Set `Order` = $LOrder where `Order` = $LOrder;");
		break;
	case "Title":
		$resutls = mysql_query ("Update Newsletter Set Title = '$Title2' where `Order` = '$LOrder';");
		break;
	case "Body":
		$resutls = mysql_query ("Update Newsletter Set Body = '$Body2' where `Order` = '$LOrder';");
		break;
}
     
  if (!$results){
print ("Unable to delete entry<br>" . $LOrder . "<br>" . $Title2 . "<br>" . $Body2 . "<br>");
exit();
  }

Link to comment
Share on other sites

No that is the entire page besides for an authentication check at the top.  I am calling this script from the form.  I know there is no insert command and that is what is so mind boggling.  I have a lot of SQL experience but can not get this one to work.  Any suggestions ever heard of this problem with mysql?

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.