Forum Moderators: coopster

Message Too Old, No Replies

Having trouble with updating my database

mysql and php updating

         

dbwieler

4:47 am on Oct 15, 2007 (gmt 0)

10+ Year Member



I can not for the life of my figure this out.....
The way its set up is like this. You login, go to the CMS page and can either compose or edit an article. when you click on the link to edit, it takes you to another page that shows you all the articles that are available to edit. So you click on the article you want to edit and it displays it in a form. All of this works.

after changing the details, you click the sumbit button, it updates the title, but not the article itself, it actually deletes it. So I`m thinking that the variables arent being passed to the update script and the field is empty. but when I added 2 $echo statements to see if they were, and they echoed the new, edited text.

Heres the code :

reviewarticle.php......

<?php
// Require the database class
require_once('DbConnector.php');

// Create a new DbConnector object
$connector = new DbConnector();

// IMPORTANT! Validate the ID number.

// Execute the query to retrieve the selected article
$result = $connector->query('SELECT ID,title,Composer,thearticle FROM cmsarticles WHERE ID = '.$_GET['id']);

// Get an array containing the resulting record
$row = $connector->fetchArray($result);

?>
<form name="form1" method="post" action="editArticle.php">

<p><label for="title">Title</label>
<input name="title" type="text" id="title" size="50" value="<?php echo $row['title']?>">
<input type=hidden name="id" value="<?php echo $row['ID']?>">
</p>
<p><label for="thearticle">Article</label>
<textarea name="thearticle" cols="50" rows="10" id="thearticle" value""><?php echo $row['thearticle']?> </textarea>

</p>
<p class="submit">
<input type="submit" name="Submit" value="Confirm">
</p>


</form>

editArticles.php.......

<?php

// Get the PHP file containing the DbConnector class
require_once('DbConnector.php');

// Check whether a form has been submitted. If so, carry on
if ($_POST){

// Create an instance of DbConnector
$connector = new DbConnector();

// IMPORTANT! ADD FORM VALIDATION CODE HERE

$insertQuery = (" UPDATE cmsarticles
SET title='$title', thearticle='$article' WHERE ID='$id'");

// Save the form data into the database
if ($result = $connector->query($insertQuery)){
$id=$_POST['ID'];
$title=$_POST['title'];
$article=$_POST['thearticle'];

// It worked, give confirmation
echo '<center><b>Article updated</b></center><br>';
echo $title;
echo '<br>';
echo $article;

}else{

// It hasn't worked so stop. Better error handling code would be good here!
exit('<center>Sorry, there was an error saving to the database</center>');

}

}
?>

If you notice the two $echo statements ($title and $article) in the confirmation part, I did that to MAKE SURE that the variables were being passed... and they are because when I run the script, they both show up with the edited variables.

this is what is shows me

Article updated

the test (which is the title)
testtest (which is the article)

So confused.... please help.

P.S. I tried the BBcodes to format the code part, but they didnt work.

Habtom

4:53 am on Oct 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You just have to put the assigning first then the insertion.
Change this part:
$insertQuery = (" UPDATE cmsarticles
SET title='$title', thearticle='$article' WHERE ID='$id'");

// Save the form data into the database
if ($result = $connector->query($insertQuery)){
$id=$_POST['ID'];
$title=$_POST['title'];
$article=$_POST['thearticle'];

// It worked, give confirmation
echo '<center><b>Article updated</b></center><br>';
echo $title;
echo '<br>';
echo $article;

to the following:

$id=$_POST['ID'];
$title=$_POST['title'];
$article=$_POST['thearticle'];

$insertQuery = ("UPDATE cmsarticles
SET title='$title', thearticle='$article' WHERE ID='$id'");

// Save the form data into the database
if ($result = $connector->query($insertQuery)){
// It worked, give confirmation
echo '<center><b>Article updated</b></center><br>';
echo $title;
echo '<br>';
echo $article;

Habtom

dbwieler

5:31 pm on Oct 16, 2007 (gmt 0)

10+ Year Member



ok I tried that, but now nothing gets changed.
it still echo's what it's changed too after clicking the "confirm" button, but when I check the article page, it stays the same as it was before.

Im thinking because the value for "thearticle" is empty, it's not even sending the text to the editArticle.php script.

<textarea name="thearticle" cols="50" rows="10" id="thearticle" value""><?php echo $row['thearticle']?> </textarea>