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.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delete/change news

    Hi

    I'm working on a newssystem, I already can add newsitems to the database but i can't change or delete them. Does anyone have an idea what I'm doing wrong?

    CODE:

    ADD NEWSITEM:

    <?php
    require("constants.php");

    $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    if(!$connection) {
    die("Database connection failed!" . mysql_error());
    }

    $db_select = mysql_select_db(DB_NAME, $connection);
    if(!$db_select) {
    die("Database connection failed!" . mysql_error());
    }

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>AIKIBDUO - Nieuwsadmin</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <META HTTP-EQUIV="imagetoolbar" CONTENT="no">
    </head>
    <body>

    <table border="0" cellspacing="17" width="100%"><tr><td class="tekst">
    <div class="titel">Nieuws administratie</div><br>
    <div class="links">Bericht toevoegen</div>
    <hr noshade>
    <div align="right" class="small"><a href="nieuws_admin2.php">berichten aanpassen of verwijderen</a></div>
    <br>

    <?php
    if(isset($_POST['voegtoe']))
    {
    $datum = $_POST['datum'];
    $onderwerp = $_POST['onderwerp'];
    $bericht = $_POST['bericht'];

    mysql_query("INSERT INTO nieuws (datum, onderwerp, bericht)
    VALUES ('".$datum."', '".$onderwerp."', '".$bericht."')")
    or die (mysql_error());

    echo 'Het nieuws is succesvol opgeslagen.';
    }
    else
    {
    ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
    <table cellspacing="10" width="100%">
    <tr>
    <td class="tekst" valign="top"><b>Datum:</b></td>
    <td><input type="text" class="formke" name="datum" size="30" value="<?php echo date("d/m/y"); ?>"></td>
    </tr>
    <tr>
    <td class="tekst" valign="top"><b>Onderwerp:</b></td>
    <td><input type="text" class="formke" name="onderwerp" size="30"></td>
    </tr>
    <tr>
    <td class="tekst" valign="top"><b>Bericht:</b></td>
    <td><textarea cols="70" rows="20" class="formke" name="bericht"></textarea></td>
    <tr>
    <td class="tekst">&nbsp;</td>
    <td><input type="submit" class="form" name="voegtoe" value="Voeg toe"></td>
    </tr>
    </table>
    </form>
    <?php
    }
    ?>

    </td></tr></table>
    <br>

    </body>
    </html>
    <?php
    mysql_close(DB_SERVER, DB_USER, DB_PASS);
    ?>

    DELETE NEWSITEM


    <?php
    require("constants.php");

    $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    if(!$connection) {
    die("Database connection failed!" . mysql_error());
    }

    $db_select = mysql_select_db(DB_NAME, $connection);
    if(!$db_select) {
    die("Database connection failed!" . mysql_error());
    }

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>AIKIBUDO - Nieuwsadmin</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <META HTTP-EQUIV="imagetoolbar" CONTENT="no">
    </head>
    <body>

    <SCRIPT LANGUAGE="JavaScript">

    function doedelete(numb){
    if (confirm("Ben je zeker dat je dit bericht wil verwijderen?"))
    {
    window.document.submitform.del.value = numb;
    window.document.submitform.submit();
    }
    }

    </SCRIPT>

    <?php

    if (isset($del) && $del >= 0)
    {
    mysql_query("DELETE FROM nieuws
    WHERE id = '$del'");
    }
    ?>
    ?>

    <table border="0" cellspacing="17" width="100%">
    <tr>
    <td class="tekst">
    <div class="titel">Nieuws administratie</div><br>
    <div class="links">Berichten aanpassen/verwijderen</div>
    <hr noshade>
    <br><br>

    <div align="center">

    <table border=0 cellpadding="0" cellspacing="5" width="100%">
    <tr>
    <td class="tekst"><b>Datum</b></td>
    <td class="tekst" width="1"><font color="#c0c0c0">|</font></td>
    <td class="tekst"><b>Onderwerp</b></td>
    <td class="tekst" width="1"><font color="#c0c0c0">|</font></td>
    <td class="tekst" align="right"><b>Bewerken</b></td>
    </tr>
    <tr>
    <td colspan="5" bgcolor="#c0c0c0"><img src="images/niks.gif" height="1" width="1" alt=""></td>
    </tr>
    <?php
    $result = mysql_query("SELECT id, onderwerp, datum
    FROM nieuws
    ORDER BY id DESC");
    while($show_msg = mysql_fetch_array($result))
    {
    echo( "<tr>\n\t<td class=\"tekst\" colspan=\"2\">$show_msg[datum]</td>\n" .
    "\t<td class=\"tekst\" colspan=\"2\">$show_msg[onderwerp]</td>\n" .
    "\t<td class=\"tekst\" align=\"right\"><a href=\"nieuws_edit.php?id=$show_msg[id]\"><img src=\"images/edit.gif\" width=\"19\" height=\"14\" alt=\"Bewerk dit bericht\" border=\"0\"></a><a href=\"\" OnClick=\"Javascript:doedelete('$show_msg[id]');return false;\"><img src=\"images/delete.gif\" width=\"19\" height=\"14\" alt=\"Verwijder dit bericht\" border=\"0\"></a></td></tr>\n");
    }
    ?>
    <tr>
    <td colspan="5" bgcolor="#c0c0c0"><img src="images/niks.gif" height="1" width="1" alt=""></td>
    </tr>
    </table>
    </div>


    <form method="post" name="submitform">
    <input type="hidden" name="del" value="-1">
    </form>

    </td>
    </tr>
    </table>
    <br>
    </body>
    </html>
    <?php
    mysql_close(DB_SERVER, DB_USER, DB_PASS);
    ?>

    CHANGE NEWSITEM

    <?php
    require("constants.php");

    $connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
    if(!$connection) {
    die("Database connection failed!" . mysql_error());
    }

    $db_select = mysql_select_db(DB_NAME, $connection);
    if(!$db_select) {
    die("Database connection failed!" . mysql_error());
    }

    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>AIKIBUDO - Nieuwsadmin</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <META HTTP-EQUIV="imagetoolbar" CONTENT="no">
    </head>
    <body>

    <?php
    if(isset($_POST['voegtoe'])) {
    mysql_query("UPDATE nieuws
    SET bericht = '".$_POST['bericht']."'
    WHERE id ='".$_POST['id']."'")
    or die ("fout bij het opslaan van je bericht in de databank");
    }
    $result = mysql_query("SELECT *
    FROM nieuws
    WHERE id = '$id'");
    $show_msg = mysql_fetch_array($result);
    ?>

    <table border="0" cellspacing="17" width="100%">
    <tr>
    <td class="tekst">
    <div class="titel">Nieuws administratie</div><br>
    <div class="links">Bericht aanpassen</div>
    <hr noshade>
    <br><br>

    <form method="POST" name="submitform" action="nieuws_admin2.php">
    <table cellspacing="10" width="100%">
    <tr>
    <td class="tekst" valign="top"><b>Onderwerp:</b></td>
    <td><input type="text" class="formke" name="onderwerp" size="30" value="<?=$show_msg[onderwerp]?>"></td>
    </tr>
    <tr>
    <td class="tekst" valign="top"><b>Bericht:</b></td>
    <td><textarea cols="70" rows="20" class="formke" name="bericht"><?=$show_msg[bericht]?></textarea></td>
    <tr>
    <td class="tekst">&nbsp;</td>
    <td><input type="submit" class="form" name="voegtoe" value=" Wijzig "> <input type="reset" class="form" value=" reset "></td>
    </tr>
    </table>
    <input type="hidden" value="<?=$id?>" name="id">
    </form>
    </td>
    </tr>
    </table>

    </body>
    </html>
    <?php
    mysql_close(DB_SERVER, DB_USER, DB_PASS);
    ?>

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Should be using $_POST['del'].

    In the future, obey forum rules, read the stickies (even if it's just the topic name) and use [PHP] BB tags to wrap your PHP code. Makes things MUCH easier to read.

    Also, do you really need 3 different pages for this? Surely you could separate out the PHP logic (this is good practise, btw) and have the processing script work out what to do?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

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