news script

A

Anonymous

Guest
I have made a news script...
and when i am going to use the edit script i get this msg:
Code:
<?php 

include "config.php"; 

$db = mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db ($db_name) or die ("Cannot connect to database"); 
$query = "SELECT title, news FROM news WHERE id = $_GET[id]"; 
$result = mysql_query($query); 
while($r=mysql_fetch_array($result))
{    
$title=$r["title"]; 
$news=$r["news"]; 
    echo "<form name=\"edit_process.php\" method=\"post\" action=\"edit_save.php?id=$_GET[id]\"> 
  <p>Title : 
    <input type=\"text\" name=\"title\" value=\"$title\"> 
  </p> 
  <p>News :</p> 
  <p> 
    <textarea name=\"news\" cols=\"40\" rows=\"6\">$news</textarea> 
  </p> 
  <p> 
    <input type=\"submit\" name=\"Submit\" value=\"Save\"> 
  </p> 
</form>"; 
} 
mysql_close($db); 
?>

and the edit_save file:
Code:
 <?php 

include "config.php"; 

$db = mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db ($db_name) or die ("Cannot connect to database");

$query = "UPDATE news SET title='$_POST[title]', news='$_POST[news]' WHERE id = $_GET[id]"; 
$result = mysql_query($query); 
echo "News item saved."; 
mysql_close($db); 
?>

When i open the edit.php file i get this error msg:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in d:\easyphp\www\newssystem\edit.php on line 9

Can somebody help me?
 
it needs the where statement otherwise it will update ALL records with the same info..


Your erroe might occur if $_GET['id'] does not have a value.
 
$query = "SELECT title, news FROM news WHERE id = $_GET[id]";
->
try this:
Code:
$query = "SELECT title, news FROM news WHERE id =".$_GET['id'];
 
Okey... i have removed:
Code:
WHERE id = $_GET[id]
from the query...

my edit.php looks like this:
Code:
<?php 

include "config.php"; 

$db = mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db ($db_name) or die ("Cannot connect to database"); 
$query = "SELECT title, news FROM news"; 
$result = mysql_query($query); 
while($r=mysql_fetch_array($result)) 
{    
$title=$r["title"]; 
$news=$r["news"]; 
/* Now lets display the titles */ 
    echo "<form name=\"edit_process.php\" method=\"post\" action=\"edit_save.php?id=$_GET[id]\"> 
  <p>Title : 
    <input type=\"text\" name=\"title\" value=\"$title\"> 
  </p> 
  <p>News :</p> 
  <p> 
    <textarea name=\"news\" cols=\"40\" rows=\"6\">$news</textarea> 
  </p> 
  <p> 
    <input type=\"submit\" name=\"Submit\" value=\"Save\"> 
  </p> 
</form>"; 
} 
mysql_close($db); 
?>

And my edit_save.php file looks like this:
Code:
<?php 

include "config.php"; 

$db = mysql_connect($db_host,$db_user,$db_pass); 
mysql_select_db ($db_name) or die ("Cannot connect to database"); 
/* We have now connected, unless you got an error message */ 
/* Lets save some news ! */ 
$query = "UPDATE news SET title='$_POST[title]', news='$_POST[news]'"; 
$result = mysql_query($query); 
echo "News item saved."; 
mysql_close($db); 
?>

When i open the edit file i get the tabels up like when i use post.php
It have the news inside the table... everything looks great.
But when have edited one news it edit all the news...
I think the problem is that he open all the news when i open edit.php !
How can i make the edit file look like this when i open:

Select news:
"title"
"title"
"title"
...........
And then when i press the title i get up the table with the info and when i press save
it only saves that news.

Can anybody help me?
Plz!
 
Try what Sigix says.

You need the where clause in your statement.
 
Try this code
Your edit page
Code:
//.. give a sql statement to fetch news subject AND ID
$sql = "select newsid, newssubject from news;";

$result = mysql_query($sql);
echo ("<select name='news' rows=1>")
while ($row=mysql_fetch_assoc($result))
{
echo ('<option value='.$row['newsid'].'>'.$row['newssubject'].'</option>')
}
echo ('</select>');
This will create a drop down list with the id as the value in the option

Now in your edit_save.php file

Code:
$sql = "update news set col_name='whatever_value' where newsid=".$_POST['newsid'].";";

mysql_query($sql);
 
ruturajv said:
Try this code
Your edit page
Code:
//.. give a sql statement to fetch news subject AND ID
$sql = "select newsid, newssubject from news;";

$result = mysql_query($sql);
echo ("<select name='news' rows=1>")
while ($row=mysql_fetch_assoc($result))
{
echo ('<option value='.$row['newsid'].'>'.$row['newssubject'].'</option>')
}
echo ('</select>');
This will create a drop down list with the id as the value in the option

Now in your edit_save.php file

Code:
$sql = "update news set col_name='whatever_value' where newsid=".$_POST['newsid'].";";

mysql_query($sql);

As it stand under my nick:
Newbe...

Can you please put that in my edit.php file and my edit_save.php file... I tryed that and i din`t get it to work!

I don`t think i ask too much...?
 
I would really would have not replied to your post, but then we are following a free open source product, and sometimes we do hope a little free support would be ideal.
But it is not so! Support is never free, but today i'm in a good mood, so its a bonus for you

here is your first edit page
Code:
<form name='edit' action='edit_savephp' method='get'>
<?php
$sql = "select newsid, newssubject from news;";

$result = mysql_query($sql);
echo ("<select name='news' rows=1>")
while ($row=mysql_fetch_assoc($result))
{
echo ('<option value='.$row['newsid'].'>'.$row['newssubject'].'</option>')
}
echo ('</select>'); 
?>
<br>
<input type=submit>
</form>

Now here is the edit_save.code
Code:
<?php
if (!isset($_POST['save']))
{
$newsid = $_GET['news'];

include('connect.php');

$sql = "select * from news where newsid=" . $newsid . ";";

$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);

?>
<form name='edit_save' action='edit_save?save=1' method=post>
<input type=text name='newssubject' value='<?php echo($row['newssubject']) ?>'>
<br>
<textarea name='newstext' rows=10 cols=30>
<?php echo($row['newssubject']) ?>
</textarea>
<input type=hidden name='newsid' value='<?php echo($row['newsid']) ?>'>
<br>
<input type=submit>
</form>
<?php
}

if (isset($_POST['save']))
{
$newsid = $_POST['newsid'];
$newssubject = $_POST['newssubject'];
$newstext = $_POST['newstext'];

include('connect.php');

$sql = "update news set newsid=$newsid, newssubject=$newssubject, newstext=$newstext where newsid=$newsid;";

mysql_query($sql) ;

}
?>
 
Thx... just what i was looking for!
What company do you work in?
 
Back
Top