Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2006
    Location
    Long Island, New York
    Posts
    3

    Unanswered: delete entry from table in database

    hello all, I am new here.

    i am slowly learning php and mysql and am stuck as im not sure how i would go about removing an entry from a table in my database.

    i have the following code:
    PHP Code:
    <?
    require_once("adminOnly.php");

    //include our database connection file
    include('dbconnect.php');

    if(
    $_POST['action']=="doedit"){

    //grab the post vars
    $title $_POST['title'];
    $id $_POST['id'];
    $news $_POST['news'];
    ?>
    <br /><br />Current News Entries<br />
    <?
    //update the database
    $news "UPDATE news SET title='$title', news='$news' WHERE id = $id";
    $editnews mysql_query($news);
    echo(
    "news edited.");
    }

    //print the news titles, with links to the edit page
    $getnews mysql_query("select * from news ORDER BY id DESC");
    while(
    $r=mysql_fetch_array($getnews)){
    extract($r);
    echo(
    "> <a href=editnews.php?id=$id&action=edit>$title</a><br />");
    }
    echo(
    "<br /><br />");

    //if we are editing a news item, print the following..
    if($_GET['action']=="edit"){
    $id $_GET['id'];
    $getnews mysql_query("select * from news WHERE id=$id");
    while(
    $r=mysql_fetch_array($getnews)){
    extract($r);
    //our form
    ?>
    Edit Entry
    <form action="editnews.php" method="POST">
    <input type="text" name="title" value="<? echo($title); ?>" /><br />
    <textarea name="news" rows="6" cols="50"><? echo($news); ?></textarea>
    <br /><input type="submit" value="Save" />
    <input type="hidden" name="id" value="<? echo($id); ?>" />
    <input type="hidden" name="action" value="doedit" />
    </form>
    <?
    }
    }
    ?>
    Add a new entry
    <form action="submit.php" method="post">
    <b>Title</b>
    <BR />
    <input type="text" name="title" size="40" maxlength="80" value="" />
    <br />
    <br />
    <b>News</b><BR><textarea name="news" rows="3" cols="40"></textarea>
    <br />
    <br />
    <input type="submit" value="submit" /> <input type="reset" value="reset" />
    </form>
    </body>
    </html>
    i tried adding this:
    PHP Code:
    //delete news
    if($_GET['action']=="delete"){
    $id $_GET['id'];
    $getnews mysql_query("delete * from news WHERE id=$id");
    while(
    $r=mysql_fetch_array($getnews)){
    exrtract($r);

    $getnews mysql_query("delete * from news ORDER BY id DESC");
    while(
    $r=mysql_fetch_array($getnews)){
    extract($r);
    echo(
    "> <a href=editnews.php?id=$id&action=delete>Delete $title</a><br />"); 
    to the code but i got an error, as im sure that isnt the correct way to delete an entry.

    thanks
    Last edited by east27; 08-08-06 at 12:17.

  2. #2
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    The basic syntax for delete is - DELETE FROM tbl_name WHERE some_condition

    Your first query would be - delete from news WHERE id=$id

    Your second query, without a WHERE clause, if it was formed correctly, would delete all the rows from the database. Is this what you want?

    You can use ORDER BY to control the order is which rows are deleted, but this does not affect the results for most table designs.

    Lastly, a DELETE query only returns a TRUE or FALSE result. A WHILE loop is probably not what you want. Also, using a mysql_fetch_array(...) on the TRUE/FALSE results will cause an error.

  3. #3
    Join Date
    Aug 2006
    Location
    Long Island, New York
    Posts
    3
    thanks,

    so ive come up with this:
    PHP Code:
    <?
    require_once("adminOnly.php");

    //include our database connection file
    include('dbconnect.php');

    echo(
    "<br /><h4>Current News Entries</h4>");

    if(
    $_POST['action']=="doedit"){

    //grab the post vars
    $title $_POST['title'];
    $id $_POST['id'];
    $news $_POST['news'];

    //update the database
    $news "UPDATE news SET title='$title', news='$news' WHERE id = $id";
    $editnews mysql_query($news);
    echo(
    "$title has been updated.<br />");
    }

    //edit links
    $getnews mysql_query("SELECT * FROM news ORDER BY id DESC");
    while(
    $r=mysql_fetch_array($getnews)){
    extract($r);
    echo(
    ">$title <br /><a href=editnews.php?id=$id&action=edit>Edit $title</a><br />");
    }
    //delete links
    $getnews mysql_query("DELETE FROM news WHERE id=$id");
    while(
    $r=mysql_fetch_array($getnews)){
    extract($r);
    echo(
    "> <a href=editnews.php?id=$id&action=delete>Delete $title</a><br />"); 

    //edit news
    if($_GET['action']=="edit"){
    $id $_GET['id'];
    $getnews mysql_query("SELECT * FROM news WHERE id='$id'");
    while(
    $r=mysql_fetch_array($getnews)){
    extract($r);
    }
    //delete news
    if($_GET['action']=="delete"){
    $id $_GET['id'];
    $getnews mysql_query("DELETE FROM news WHERE id=$id");
    while(
    $r=mysql_fetch_array($getnews)){
    extract($r);

    ?>
    Edit Entry
    <form action="editnews.php" method="POST">
    <input type="text" name="title" value="<? echo($title); ?>" /><br />
    <textarea name="news" rows="6" cols="50"><? echo($news); ?></textarea>
    <br /><input type="submit" value="Save" />
    <input type="hidden" name="id" value="<? echo($id); ?>" />
    <input type="hidden" name="action" value="doedit" />
    </form>
    <h4>Add A New Entry</h4>
    <form action="submit.php" method="post">
    <b>Title</b>
    <BR />
    <input type="text" name="title" size="40" maxlength="80" value="" />
    <br />
    <br />
    <b>News</b><BR><textarea name="news" rows="3" cols="40"></textarea>
    <br />
    <br />
    <input type="submit" value="submit" /> <input type="reset" value="reset" />
    </div>
    <div id="footer">
    </div>
    </form>
    </body>
    </html>
    <?
    }
    }
    ?>
    and now im getting this error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/east27n/public_html/clients/upi/news/editnews.php on line 30
    Last edited by east27; 08-08-06 at 20:21.

  4. #4
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    Without even counting line numbers, I am sure it is one of the two usages of the following -
    PHP Code:
    $getnews mysql_query("DELETE FROM news WHERE id=$id");
    while(
    $r=mysql_fetch_array($getnews)){ 
    The reason no one has answered your post about the error message is because the answer has already been given -
    Lastly, a DELETE query only returns a TRUE or FALSE result. A WHILE loop is probably not what you want. Also, using a mysql_fetch_array(...) on the TRUE/FALSE results will cause an error.

  5. #5
    Join Date
    Aug 2006
    Location
    Long Island, New York
    Posts
    3
    thanks dbmab and i realize that, but im not sure what i should replace that with to get it working

  6. #6
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    An if(...) conditional test would be appropriate to test a true/false result. If you want to find the number of rows that were changed, the mysql_affected_rows(...) function specifically returns the number of affected rows due to the last INSERT, UPDATE or DELETE query.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •