Hughesy1986 Posted January 14, 2007 Share Posted January 14, 2007 Hi guys!I have been making a news script in php and mysql its my first project ive done in mysql and im a bit stuck and really need some help. This script im making im gona release for free for everyone once its done.Ok the problem is im trying to make a update page and delete bit, whats happening is the id number isnt choosing the correct news when i update the table. Heres my whole code for the edit_news.php[code]<?require("connect.php");$query = " SELECT * FROM news WHERE id='$id'";$result = mysql_query($query);$num = mysql_numrows($result);mysql_close();$i=0;while ($i < $num) {$id = mysql_result($result, $i, "id");$news = mysql_result($result, $i, "news");$title = mysql_result($result, $i, "title");?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p> <input type="hidden" name="ud_id" value="<? echo $id; ?>"> News ID: <input name="ud_id" type="text" value="<? echo $id; ?>" size="3" maxlength="3"> </p> <p>News Title: <input name="ud_title" type="text" id="ud_title" value="<? echo $title; ?>"> </p> <p>News:<br> <br> <textarea name="ud_news" cols="75%" rows="15" id="ud_news"><? echo $news; ?></textarea> </p> <p> <input name="Update" type="Submit" id="Update" value="Update"> </p></form><p> <?++$i;}?></p><form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">News ID Number:<input name="id" type="text" id="id" size="3" maxlength="3"> <input name="Get" type="submit" id="Get" value="Get"></form><p> </p><?phpif ($_POST['Update']) { require("connect.php"); $id=$_GET['id']; $ud_title = $_POST['ud_title']; $ud_news = $_POST['ud_news']; $query = "UPDATE news SET id='$id', title='$ud_title', news='$ud_news'"; mysql_query($query); echo "The News Article: <b>$ud_title</b> ID: <b>$id</b> has been updated"; mysql_close(); } ?> [/code]You guys will probally say its something really simple but i cant find it anywhere.Many ThanksGlen Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 14, 2007 Share Posted January 14, 2007 Welcome to the board :)Ok, a couple of things, firstly, what does happen when you run the script? Second, when dealing with mysql problems, add an or die statement to the query. For instance your query here:[code]<?php$query = "UPDATE news SET id='$id', title='$ud_title', news='$ud_news'"; mysql_query($query);?>[/code]Can be changed to [code]<?php$query = "UPDATE news SET id='$id', title='$ud_title', news='$ud_news'"; mysql_query($query) or die(mysql_error().'<br />Query:'.$query);?>[/code]This will show any errors, and if they are, give the mysql error along with what was actually passed into the query.Finally, do you think you could edit your post so your code uses full php tags (<?php) rather than the short tags (<?) - otherwise you dont get the syntax highlighting and it makes it a bit hard to read. Quote Link to comment Share on other sites More sharing options...
Hughesy1986 Posted January 14, 2007 Author Share Posted January 14, 2007 Hi,I have tried to add that in but it wont let me edit my post sorry.The error is that, when i load the news say ?id=3 and change the entrys, what is happening is that it is updating it as news id=0 and not updating the correct one.Glen Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 14, 2007 Share Posted January 14, 2007 Im a little confused as to why there are two forms on this page. Is the second one used to select the news article that you want to edit, and the first used to actually edit the article? Quote Link to comment Share on other sites More sharing options...
Hughesy1986 Posted January 14, 2007 Author Share Posted January 14, 2007 Yes that is exactly it.Glen Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 14, 2007 Share Posted January 14, 2007 Ok, i see. I give this a go:[code]<?pjprequire("connect.php");$id = $_GET['id']; //im assuming that currently it works because register_globals is turned on, but really you should do this$query = " SELECT * FROM news WHERE id='$id'";$result = mysql_query($query)or die(mysql_error().'<br />QUERY:'.$query);;mysql_close();//you shouldn't really need the while statment here, there should only be one news article for each ID. $news = mysql_result($result, 0, "news");$title = mysql_result($result, 0, "title");?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <p> <input type="hidden" name="ud_id" value="<? echo $id; ?>"> <p>News Title: <input name="ud_title" type="text" id="ud_title" value="<? echo $title; ?>"> </p> <p>News:<br> <br> <textarea name="ud_news" cols="75%" rows="15" id="ud_news"><? echo $news; ?></textarea> </p> <p> <input name="Update" type="Submit" id="Update" value="Update"> </p></form><p></p><form name="form1" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">News ID Number:<input name="id" type="text" id="id" size="3" maxlength="3"> <input name="Get" type="submit" id="Get" value="Get"></form><p> </p><?phpif ($_POST['Update']) { require("connect.php"); $id=$_POST['ud_id'];//change to the ID comming from the POST form. I think this was probably the route of your problems $ud_title = $_POST['ud_title']; $ud_news = $_POST['ud_news']; $query = "UPDATE news SET title='$ud_title', news='$ud_news' WHERE `id`='$id'"; mysql_query($query) or die(mysql_error().'<br />QUERY:'.$query); echo "The News Article: <b>$ud_title</b> ID: <b>$id</b> has been updated"; mysql_close(); } ?>[/code]Ive put a few comments in there to explain a bit of what ive done. The only other thing is that ive removed the field where you would edit the ID. You're going to need this to be unique and remain constant for this kind of thing. Quote Link to comment Share on other sites More sharing options...
Hughesy1986 Posted January 14, 2007 Author Share Posted January 14, 2007 Worked perfect thanks alot, I had suspected that was the problem but couldnt figure out how to fix it, great mate thanks alot again!Glen Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted January 14, 2007 Share Posted January 14, 2007 No problem. Glad to help. 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.