LiamH Posted February 23, 2009 Share Posted February 23, 2009 Hi all. I have a form which pulls database data for a specific file when its hyperlink is selected. The form is filled correctly with the correct data. The page gives the user the option of editing this data and submitting the changes, which will update the database with the made changes. When the user clicks submit it will take the user to a new page called update.php where the changes take place. This is where my problems are happening. The message advising the updates have taken place, but when checking the DB, no changes have happened. <body> <?php include "configfile"; $aid=$_POST['AID']; $headline=$_POST['Headline']; $date=$_POST['Date']; $preview=$_POST['Preview']; $body=$_POST['Body']; $query="UPDATE tblname SET Headline='$headline', Date='$date', Preview='$preview', Body='$body' WHERE AID='$AID'"; mysql_query($query); echo "Record Updated"; mysql_close(); ?> </body> Where have I gone wrong? Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/ Share on other sites More sharing options...
wrathican Posted February 23, 2009 Share Posted February 23, 2009 put this at the end of your script: echo mysql_error(); see if you have an error in your sql Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769134 Share on other sites More sharing options...
LiamH Posted February 23, 2009 Author Share Posted February 23, 2009 No errors came up. Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769143 Share on other sites More sharing options...
wrathican Posted February 23, 2009 Share Posted February 23, 2009 edit: the mysql_error() needs to go before the mysql_close(), don't know if you tested it that way. change it to this: <?php //previous code above - starting from mysql_query $result = mysql_query($query); if($result) { echo "yes"; }else{ echo "no"; } ?> that should tell you if your query executed successfully Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769146 Share on other sites More sharing options...
LiamH Posted February 23, 2009 Author Share Posted February 23, 2009 Came up with yes and no errors. I have a feeling I am meant to have something in the header. Is that correct? To get the data from one page to another. Should $aid=$_POST['AID']; $headline=$_POST['Headline']; $date=$_POST['Date']; $preview=$_POST['Preview']; $body=$_POST['Body']; this appear in the header? Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769156 Share on other sites More sharing options...
wrathican Posted February 23, 2009 Share Posted February 23, 2009 are you sending the data from a form on a previous page? if so, can we see it? Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769158 Share on other sites More sharing options...
LiamH Posted February 23, 2009 Author Share Posted February 23, 2009 This is the first page. All of the data appears correctly <?php // checks to see if user logged in. If not, redirects user to login page located at admin.php session_start(); if(!session_is_registered(username)){ header("location:admin.php"); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> </head> <body> <?php if(empty($_GET['AID'])) { echo "<p class='error'>No ID supplied.</p>\n"; } else if(!$_POST['AID']) { //include config file $headline = ($_POST['Headline']); //needed? $query = "SELECT Headline, Date, Preview, Body FROM tblname WHERE AID=".$_GET['AID']; $result = mysql_query($query) or die("Query failed: ".mysql_error()); if(mysql_num_rows($result)) // found something { $row = mysql_fetch_assoc($result); echo "<form action='update.php' method='post'>"; echo "<table width='750px' border='0' align='center' cellpadding='0' cellspacing='1'>"; echo "<tr>"; echo "<td><input type='hidden' name='AID' value='".$_GET['AID']."'></td>"; echo "</tr>"; echo "<tr>"; echo "<td>Title:</td>"; echo"<td><input name='Headline' type='text' size='100' value='".$row['Headline']."'></td>"; echo"</tr>"; echo"<tr>"; echo "<td>Date:</td>"; echo"<td><input name='Date' type='text' value='".$row['Date']."'</td>"; echo"</tr>"; echo"<tr>"; echo "<td>Preview:</td>"; echo"<td> <textarea name='preview' cols='65' rows='15'>".$row['Preview']."</textarea></td>"; echo"</tr>"; echo"<tr>"; echo "<td>Body:</td>"; echo"<td><textarea name='body' cols='65' rows='15'>".$row['Body']."</textarea></td>"; echo"</tr>"; echo"<tr>"; echo"<td><input type='submit' name='submit' value='Submit'></td>"; echo"</tr>"; echo"</table>"; echo "</form>"; } else { echo "<p class='error'>Invalid article ID.</p>\n"; } } ?> </body> </html> And this is the code from the update page ?php // checks to see if user logged in. If not, redirects user to login page located at admin.php session_start(); if(!session_is_registered(username)){ header("location:admin.php"); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Update page</title> </head> <body> <?php include ".php"; $aid=$_POST['AID']; $headline=$_POST['Headline']; $date=$_POST['Date']; $preview=$_POST['Preview']; $body=$_POST['Body']; $query="UPDATE tblname SET Headline='$headline', Date='$date', Preview='$preview', Body='$body' WHERE AID='$AID'"; mysql_query($query); //echo "Record Updated"; //previous code above - starting from mysql_query $result = mysql_query($query); if($result) { echo "yes"; }else{ echo "no"; } echo mysql_error(); mysql_close(); ?> </body> </html> This is a project I stopped doing some time ago and have picked it up again, I could have sworn this was all working fine. You might be able to tell I'm not the most advanced with PHP as well, so I'm trying to remember everything again Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769173 Share on other sites More sharing options...
revraz Posted February 23, 2009 Share Posted February 23, 2009 Change $AID to $aid $query="UPDATE tblname SET Headline='$headline', Date='$date', Preview='$preview', Body='$body' WHERE AID='$AID'"; Variable names are case sensitive. Echo $query to verify. Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769178 Share on other sites More sharing options...
LiamH Posted February 23, 2009 Author Share Posted February 23, 2009 Ok to make things more legible for myself and of course everyone helping, AID has been changed to ArticleID. So they're now all changed. Using the echo "$query"; I noticed that the Article ID was not showing, neither was the preview or body text. So have gone through all my code and found two lines that were incorrect due to the case. So changed them and now get this; UPDATE tblname SET Headline='Test Article number 1', Date='2009-02-22', Preview='This is a test article', Body='The rest of the information goes in here' WHERE ArticleID='' so when I change the information and submit it, the changes show up in the printed query on the page, but when checking the DB no changes are made. Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769207 Share on other sites More sharing options...
revraz Posted February 23, 2009 Share Posted February 23, 2009 Your variable for ArticleID is still wrong, post your updated code. Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769212 Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 $query="UPDATE tblname SET Headline='$headline', Date='$date', Preview='$preview', Body='$body' WHERE AID='$aid'"; Notice it is not $AID it is $aid, php Is CaSe SenSiTive. Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769215 Share on other sites More sharing options...
LiamH Posted February 23, 2009 Author Share Posted February 23, 2009 When the query was printed and it didn't show the ArticleID I thought that was the issue. I went through a few times and couldn't see it. Went through again and found the variable had a lower case id instead of ID. So stupid. Thank you all for your help! Quote Link to comment https://forums.phpfreaks.com/topic/146506-solved-using-forms-to-update-database-data/#findComment-769221 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.