Jump to content

[SOLVED] Updating Database problems


Steppio

Recommended Posts

I have a small problem with regards to one of my last posts, i have an edit link below every news article, and that link goes to a page where the title and it's article are then displayed in a input box and a textarea respectively. The only problem is it's coming back that it was succesful, but the contents don't change. The code for the page is as follows:

 

<?php

require_once('main_fns.php');

$head=$_POST['up_head'];
$news=$_POST['up_news'];
$id=$_POST['up_id'];

session_start();
try
{
//check forms filled in
if (!filled_out($_POST))
{
	throw new Exception('You have not filled out the form correctly '.
								'- please go back and try again.');
}

// attempt to update
// this function can also throw an exception
updatenews($head, $news, $id);

// provide link to members page
require('head.inc');
echo $news;
echo 'The update was succesful. ';
	echo '<a href="main.php?<?php echo strip_tags(SID); ?>">Back to main page</a>';
require('foot.inc');

}
catch (Exception $e)
{
require('head.inc');
echo $e->getMessage();
require('foot.inc');
exit;
}

?>

and the code for the function is as follows:

 

function updatenews($head, $news, $id)
{
$conn = db_connect();
$result = $conn->query( "update news
			set newshead = '$head', news='$news'
			where id = '$id'");

if (!$result)
	throw new Exception('Could not change password.');

}

 

and the function that passes the variables from the editing page is:

 

function edit_news($id)
{
mysql_conn1();
$result2 = @mysql_query("SELECT id, newshead, news FROM news WHERE id='$id'"); 
	if (!$result2) 
	{ 
			exit('<p>Error performing query: ' . mysql_error() . '</p>'); 
	} 
	while ($row2 = mysql_fetch_array($result2)) 
	{
	$id = $row2['id'];
	$head = $row2['newshead'];
	$news = $row2['news']; 
	}
?>
<table><tr><td>
<form class="AddNews" method="post" action="updatenews.php">
<br><center>
Add an article: <br>
ID: <input disabled="disabled" type="text" class="sub1" name="up_id" size="3" value="<?php echo $id;?>"<br>
Title: <input type="text" class="sub1" name="up_head" size="40" value="<?php echo $head;?>"<br>
Content: <textarea cols="250" rows="40" class="sub1" name="up_news"><?php echo $news;?></textarea><br>
<input type="submit" class="sub" value="Update"><br />

</form></td></tr>
</table>
<?php
}

 

Any help would be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/44429-solved-updating-database-problems/
Share on other sites

Try this

 

function updatenews($head, $news, $id)
{
$conn = db_connect();
$conn->query( "update news
		set newshead = '$head', news='$news'
		where id = '$id'") OR throw new Exception('Could not change password. ' . mysql_error());
}

 

See where that gets you. Also I would look into the mysql_real_escape_string  to clean variables for entry into mysql.

The code still isnt working, i'm thinking it's probably a problem in seperating the variables so that the 'new' news article is being ignored by the 'old' article. I'm thinking i can maybe try it on a smaller project and try and get it working there, and then maybe introduce it to this project. There must be another way around it. Any ideas?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.