Jump to content

PHP and Mysql Help


Hughesy1986

Recommended Posts

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>&nbsp; </p>

<?php

if ($_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 Thanks

Glen
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Ok, i see. I give this a go:

[code]
<?pjp
require("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>&nbsp; </p>

<?php

if ($_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.
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.