DiFi80211 Posted December 27, 2007 Share Posted December 27, 2007 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(); } Quote Link to comment Share on other sites More sharing options...
revraz Posted December 27, 2007 Share Posted December 27, 2007 With the code you posted, there are no INSERT statements at all. So you are either calling the wrong page, posted the wrong code, or didn't post the entire page. Quote Link to comment Share on other sites More sharing options...
DiFi80211 Posted December 27, 2007 Author Share Posted December 27, 2007 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? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 27, 2007 Share Posted December 27, 2007 Post the entire code and the form if you want. Quote Link to comment Share on other sites More sharing options...
DiFi80211 Posted December 27, 2007 Author Share Posted December 27, 2007 I was able to fix the insert problem, and all the update commands are working now. Thanks for the help anyhow. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 27, 2007 Share Posted December 27, 2007 Maybe post what the problem was? Quote Link to comment 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.