Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    New Coder
    Join Date
    Jun 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts

    delete entry from table in database

    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

  • #2
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    DELETE works on an entire row, so don't specify columns:

    Code:
    DELETE FROM table WHERE col = 'x'
    If you specify columns, you'll get a syntax error (which can be caught using mysql_error()).

    Code:
    delete * from news ORDER BY id DESC
    Don't do this unless you want to delete your entire table (in which case, you should use TRUNCATE). You must have a WHERE clause to tell the server which rows to delete, or you lose everything.

    Also, mysql_query() only returns a boolean value to tell you whether the statement failed or not -- there's no result resource to loop through.

    I strongly suggest you read the MySQL manual page for DELETE before you go any further.

  • #3
    New to the CF scene
    Join Date
    Jul 2006
    Location
    Cork Ireland
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For help with insert update and delete www.w3schools.com is great.

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Quote Originally Posted by 27east
    i am slowly learning php and mysql and am stuck ....
    to the code but i got an error, as im sure that isnt the correct way to delete an entry.
    As you are new to this, may I point out that when you get an error that you post that as well? It will usually help people in assisting you as to what is wrong with your query etc.

  • #5
    New Coder
    Join Date
    Jun 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    when using 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 />");
    }

    //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(
    ">$title <br /><a href=editnews.php?id=$id&action=edit>Edit $title</a><br />");
    }
    //NEW
    $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 />"); 
    }

    //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);
    }

    //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);

    ?>
    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>
    i recieve this error:


    Parse error: syntax error, unexpected T_WHILE in /home/east27n/public_html/clients/upi/news/editnews.php on line 30
    Last edited by 27east; 08-08-2006 at 07:15 PM.

  • #6
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need to put single-quotes around your value, not doubles (and only if that value's a string -- integers don't need to be quoted).

  • #7
    New Coder
    Join Date
    Jun 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah sorry i had noticed that and fixed it and updated my post, i am getting a different error now

  • #8
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    AND the different error is what? Please post your errors, don't tell us you are getting one. We can't see it, we can't guess what it is.

  • #9
    New Coder
    Join Date
    Jun 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    AND if you look at the bottom of my post you can see the error i am getting

  • #10
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Line-ending semi-colons belong outside quotes.

  • #11
    New Coder
    Join Date
    Jun 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 />");
    }

    //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(
    ">$title <br /><a href=editnews.php?id=$id&action=edit>Edit $title</a><br />");
    }
    //NEW
    $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 />"); 
    }

    //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);
    }

    //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);

    ?>
    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>

    Parse error: syntax error, unexpected T_STRING in /home/east27n/public_html/clients/upi/news/editnews.php on line 36

  • #12
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Start using a text editor with syntax highlighting. It will help you track down parse errors like this. (Look at the code you just posted above -- the forum's highlighter will point your right at the problem.)

  • #13
    New Coder
    Join Date
    Jun 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ah yes, how stupid of me, i have been using notepad.

    so i got that error fixed no i am recieving this error:

    Parse error: syntax error, unexpected $end in /home/east27n/public_html/clients/upi/news/editnews.php on line 75


    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 />");
    }

    //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(
    ">$title <br /><a href=editnews.php?id=$id&action=edit>Edit $title</a><br />");
    }
    //NEW
    $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 />"); 
    }

    //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);
    }

    //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);

    ?>
    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>

  • #14
    New Coder
    Join Date
    Aug 2006
    Posts
    64
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);
    }
    ?>
    I bolded what you need to add [the } ], that should fix the problem.

  • #15
    New Coder
    Join Date
    Jun 2006
    Posts
    28
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks but im still getting the same error


    Parse error: syntax error, unexpected $end in /home/east27n/public_html/clients/upi/news/editnews.php on line 75


    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 />");
    }

    //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(
    ">$title <br /><a href=editnews.php?id=$id&action=edit>Edit $title</a><br />");
    }
    //NEW
    $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 />"); 
    }

    //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);
    }

    //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>


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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