Jump to content

news management


alen

Recommended Posts

I started reading this tutorial about how to make a simple news management script with php and mysql.

I'm new to all this php related stuff.. so could one of you guys help me with some errors?

I have this file called edit_process.php where I'm supposed to modifiy the news that I added. So I open the page and there I find beautiful errors which I tried to fix, but failed :\

Here's the error;

[b]Notice: Undefined index: id in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 7

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 9[/b]

Here's the code;

[b]<?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);

?> [/b]

Sincerely,
anigma

Link to comment
Share on other sites

[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");
if($_GET){
echo "<!-- Your query was selected with a get method -->";
$query=mysql_query("SELECT * FROM news WHERE id=$_GET[id]");
}else if($_POST){
echo "<!-- Your query was selected with a post method -->";
$query=mysql_query("SELECT * FROM news WHERE id=$_POST[id]");
}

while($r=mysql_fetch_array($result))
{   
echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$_GET[id]."'>
  <p>Title :
    <input type='text' name='title' value='".$r[title]."' />
  </p>
  <p>News :</p>
  <p>
    <textarea name='news' cols='40' rows='6'>".$r[news]."</textarea>
  </p>
  <p>
    <input type='submit' name='Submit' value='Save' />
  </p>
</form>";
}
?>
[/code]

Try that. Notice I made the query a tad more dynamic. You can undo that if you want, but use it now to find out whether you are using a post or get method to find the query.
Link to comment
Share on other sites

I got this,

[b]Notice: Undefined variable: result in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 13

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 13[/b]

Take a look at the whole script, http://www.oxyscripts.com/item-408.html.
Link to comment
Share on other sites

Looks like a bunch of quotes are missing

Try 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");
if($_GET){
echo "<!-- Your query was selected with a get method -->";
$query=mysql_query("SELECT * FROM news WHERE id=$_GET['id']");
}else if($_POST){
echo "<!-- Your query was selected with a post method -->";
$query=mysql_query("SELECT * FROM news WHERE id=$_POST['id']");
}

while($r=mysql_fetch_array($result))

echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$_GET['id']."'>
  <p>Title :
    <input type='text' name='title' value='".$r['title']."' />
  </p>
  <p>News :</p>
  <p>
    <textarea name='news' cols='40' rows='6'>".$r['news']."</textarea>
  </p>
  <p>
    <input type='submit' name='Submit' value='Save' />
  </p>
</form>";
}
?>
[/code]
Link to comment
Share on other sites

try 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");
if(isset($_GET['id'])) {
$id = $_GET['id'];
echo "<!-- Your query was selected with a get method -->";
$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
}
if(isset($_POST['id'])){
$id = $_POST['id'];
echo "<!-- Your query was selected with a post method -->";
$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
}

while($r=mysql_fetch_array($query)) {
$nid = $r['id']
$title = $r['title'];
$news = $r['news']
echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$nid."'>
  <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>";
}
?>
[/CODE]
Link to comment
Share on other sites

ya i made a mistake heres the new code tlel me if it works

[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");
if(isset($_GET['id'])) {
$id = $_GET['id'];
echo "<!-- Your query was selected with a get method -->";
$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
}
if(isset($_POST['id'])){
$id = $_POST['id'];
echo "<!-- Your query was selected with a post method -->";
$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
}

while($r=mysql_fetch_array($query)) {
$nid = $r['id']
$title = $r['title'];
$news = $r['news'];
echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$nid."'>
  <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>";
}
?>[/code]
Link to comment
Share on other sites

Now I get;

Notice: Undefined variable: query in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 16

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\nyheter\edit_process.php on line 16
Link to comment
Share on other sites

That likely means that your query ($query=mysql_query("SELECT * FROM news WHERE id='$id'") returned nothing.  No rows matched your selection criteria, so the resource ($query) is empty.  Then the call to mysql_fetch_array is invalid.

Try 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");
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "<!-- Your query was selected with a get method -->";
    $query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
  }
  if(isset($_POST['id'])){
    $id = $_POST['id'];
    echo "<!-- Your query was selected with a post method -->";
    $query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
  }


  if (mysql_num_rows($query)>0) {
    while ($r=mysql_fetch_array($query)) {
      $nid = $r['id']
      $title = $r['title'];
      $news = $r['news'];
      echo "<form name='edit_process' method='post' action='edit_save.php?id=".$nid."'>
        <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>";
    }
  }
  else {
    echo "<p>No news items.</p>";
  }
?>[/code]

I also think you could get rid of the "if (isset($_POST['id']..." statement.  You don't have any variable called 'id' in your form and you are only using a GET method in the query string.  If you wanted to make the processing code such that it didn't matter, then change the GET to REQUEST and then it will grab both POST & GET variables.
Link to comment
Share on other sites

[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");
if(isset($_GET['id'])) {
$id = $_GET['id'];
echo "<!-- Your query was selected with a get method -->";
$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
}
if(isset($_POST['id'])){
$id = $_POST['id'];
echo "<!-- Your query was selected with a post method -->";
$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
}

while($r=mysql_fetch_array($query)) {
$nid = $r['id'];
$title = $r['title'];
$news = $r['news'];
echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$nid."'>
  <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>";
}
?>
[/CODE]
Link to comment
Share on other sites

I tried the code bljepp69 gave me, and it got me another error;

[b]Notice:[/b] Undefined variable: query in [b]C:\Program Files\Server\public_html\news\edit_process.php[/b] on line [b]10[/b]

[b]Warning:[/b] mysql_fetch_array(): supplied argument is not a valid MySQL result resource in [b]C:\Program Files\Server\public_html\news\edit_process.php[/b] on line [b]10[/b]
Link to comment
Share on other sites

Now it's time to hunt down where the error is that's causing the query to not be valid.  Try this code.  I have added some echo statements to send output at various points in the code so we can see what's happening.

[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");
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "id = $id<br />";  //check the value of $id
    echo "<!-- Your query was selected with a get method -->";
    $query_text = "SELECT * FROM news WHERE id='$id'"; //verify that the query actually picked up the variable $id
    echo "query = $query_text<br />";
    $query=mysql_query($query_text) or die(mysql_error());

  }
  if (mysql_num_rows($query)>0) {
  echo mysql_num_rows($query)." rows returned<br />"; //see how many rows are actually returned
    while ($r=mysql_fetch_array($query)) {
      $nid = $r['id']
      $title = $r['title'];
      $news = $r['news'];
      echo "<form name='edit_process' method='post' action='edit_save.php?id=".$nid."'>
        <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>";
    }
  }
  else {
    echo "<p>No news items.</p>";
  }
?>[/code]

Also, you can get rid of the "Notice" by setting this at the top of your code...

error_reporting(E_ALL ^ E_NOTICE);
Link to comment
Share on other sites

[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");
  if(isset($_GET['id'])) {
    $id = $_GET['id'];
    echo "id = $id<br />";  //check the value of $id
    echo "<!-- Your query was selected with a get method -->";
    $query_text = "SELECT * FROM news WHERE id='$id'"; //verify that the query actually picked up the variable $id
    echo "query = $query_text<br />";
    $query=mysql_query($query_text) or die(mysql_error());

  }
  if (mysql_num_rows($query)>0) {
  echo mysql_num_rows($query)." rows returned<br />"; //see how many rows are actually returned
    while ($r=mysql_fetch_array($query)) {
      $nid = $r['id'];
      $title = $r['title'];
      $news = $r['news'];
      echo "<form name='edit_process' method='post' action='edit_save.php?id=".$nid."'>
        <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>";
    }
  }
  else {
    echo "<p>No news items.</p>";
  }
?>
[/CODE]
Link to comment
Share on other sites

[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");
if(isset($_GET['id'])) {
$id = $_GET['id'];
echo "<!-- Your query was selected with a get method -->";
$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
}
if(isset($_POST['id'])){
$id = $_POST['id'];
echo "<!-- Your query was selected with a post method -->";
$query=mysql_query("SELECT * FROM news WHERE id='$id'") or die(mysql_error());
}

while($r=mysql_fetch_array($query)) {
$nid = $r['id'];
$title = $r['title'];
$news = $r['news'];
echo "<form name='edit_process.php' method='post' action='edit_save.php?id=".$nid."'>
  <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>";
}
?>[/CODE]
Link to comment
Share on other sites

the code gave me these errors;

Notice: Undefined variable: query in C:\Program Files\Server\public_html\news\edit_process.php on line 16

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Server\public_html\news\edit_process.php on line 16
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.