Jump to content

[SOLVED] Using forms to update database data


LiamH

Recommended Posts

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?  :(

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

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.