Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Update and Delete Data Using PHP, Forms and Mysql

  • 12-05-2008 12:52pm
    #1
    Closed Accounts Posts: 24


    Hi All

    I was wondering if any of you could help me with a problem I am having with Updating my Database. I am trying to do exactly what is on this website

    http://www.dmxzone.com/ShowDetail.asp?NewsId=4114

    But the code from the site does not seem to work.

    I was wondering if anybody had a similar code that I could try I have trawling the net looking for one.

    Thanks in advance!


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Doesn't work how? What're you seeing happen?

    What form and table structure do you have?


  • Closed Accounts Posts: 24 minicin


    I have tried taking that code and changing the relevant parts but nothing comes up.

    I am not sure where the error is. I am basically trying to echo the articles from my database and add a link to update and delete.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    We still need to see what you changed it to...


  • Closed Accounts Posts: 24 minicin


    This is what I have changed the code to. I really am trying to update my database using edit and delete links, trying to echo the articles into a form so I can update them from there. This is the closest code I could find to do it.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <?php
    if(!isset($a){
    $link = @mysql_connect(&quot;localhost", "root", "root");
    if(!$link){
    echo('Error connecting to the database: ' . mysql_error());
    exit();
    }
    $db = @mysql_select_db('GlobalWarrior');
    if(!$db){
    echo('Error selecting database: ' . mysql_error());
    exit();
    }
    $query = "SELECT Aid, AAuthor, ATitle, AAbstract, AArticle, ATimeStamp FROM GlobalWarriorArticles ORDER BY Aid";
    $result = @mysql_query($query);
    if(!$result){
    echo('Error selecting information: ' . mysql_error());
    exit();
    }
    if (mysql_num_rows($result) > 0){
    while($row = mysql_fetch_object($result))
    {
    ?>
    <font size="-1"><b><?php echo $row->AAuthor; ?></b><i><?php echo
    formatDate($row->ATimeStamp); ?></i></font>
    <br />
    <font size="-2"><a href="edit.php?a=edit&Aid=<?php echo $row->Aid;
    ?>">edit</a> |
    <a href="delete.php?a=delete&Aid=<?php echo $row->Aid; ?>">delete</a></font>
    <?php
    }else{
    ?>
    <font size="-2">No information in the database</font>
    <?php }
    mysql_close($link);
    }elseif($a == 'edit'){
    if(!isset($update)){
    $link = @mysql_connect(&quot;localhost", "root", "root");
    if(!$link){
    echo('Error connecting to the database: ' . mysql_error());
    exit();
    }
    $db = @mysql_select_db('GlobalWarrior');
    if(!$db){
    echo('Error selecting database: ' . mysql_error());
    exit();
    }
    $query = "SELECT Aid, AAuthor, ATitle, AAbstract, AArticle, ATimeStamp FROM GlobalWarriorArticles Where Aid = '$i'";
    $result = @mysql_query($query);
    if(!$result){
    echo('Error selecting information: ' . mysql_error());
    exit();
    }
    mysql_fetch_object($result);
    ?>
    <form name="form1" method="post" action="edit.php?a=edit&Aid=<?php echo($id)
    ?>&update=1">
    <table width="50%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="50%">Name</td>
    <td><input name="Author" type="text" id="AAuthor" value="<?php echo($row-
    >name) ?>" /></td>
    </tr>

    <tr>
    <td>Email</td>
    <td><input name="Title" type="text" id="ATitle" value="<?php echo($row-
    >email) ?>" /></td>
    </tr>
    <tr>
    <td> </td>
    <td> </td>
    </tr>
    <tr>
    <td>Headline</td>
    <td><input name="Abstract" type="text" id="AAbstract" value="<?php
    echo($row->headline) ?>" /></td>
    </tr>
    <tr>
    <td>News Story</td>
    <td><textarea name="Article" id="AArticle" value="<?php echo($row->story)
    ?>"</textarea></td>
    </tr>
    </tr>
    <td colspan="2"><div align="center">
    <input name="hiddenField" type="hidden" value="update" />
    <input name="add" type="submit" id="add" value="Update" />
    </div></td>
    </tr>
    </table>
    </form>
    <?
    }else{
    $query = "UPDATE GlobalWarriorArticles SET AAuthor = '$Author, ATitle = '$Title', AAbstract =
    '$Abstract', AArticle = '$Article', ATimeStamp = NOW() WHERE Aid = '$id'";
    $result = @mysql_query($query);
    if(!$result){
    echo('Error updating news item: ' . $mysql_error());
    exit();
    }else{
    mysql_close($link);
    echo('Update Successful!');
    }
    }elseif($a == 'delete'){
    $link = @mysql_connect(&quot;localhost", "root", "root");
    if(!$link){
    echo('Error connecting to the database: ' . mysql_error());
    exit();
    }
    $db = @mysql_select_db('GlobalWarrior');
    if(!$db){
    echo('Error selecting database: ' . mysql_error());
    exit();
    }
    $query = "DELTE FROM GlobalWarriorArticles WHERE Aid = '$id'";
    $result = @mysql_query($query);
    if(!$result){
    echo('Error selecting information: ' . mysql_error());
    exit();
    }
    mysql_close($link);
    echo('News item deleted.');
    } ?>


  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    The first php statement
    if (!isset($a)) {
    The $a is not set anywhere before that, presumably you need to get it from $_REQUEST based on the likes of
    <a href="edit.php?a=edit&Aid=
    The original author must have had register_globals on, bad news.

    Also watch the curly braces, there seems to be a missing terminating brace somewhere.


  • Advertisement
Advertisement