SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why Does This Delete Query Not Work?

    I have the following code in a page of mine, and for some reason it doesn't delete from the database, but if I run the query direclty in MySQL Front it does work.

    I have ripped all the stuff out that doesn't matter to this query and so it's easier to see.

    If it's any help, the $id is an auto_increment number


    Many Thanks
    Ian Gunter


    PHP Code:
    <?
    //uploader.php
    function delete_file() {
    ?><table width="615" border="0" cellspacing="2" cellpadding="2">
            <tr>
                <td><p><strong>File Deleted</strong></p></td>
            </tr><tr>
                <td><hr color='#333333' size='1'></td>
            </tr>
            </table>
            <?
            
    // create SQL statement
            
    $sql "DELETE FROM files WHERE id=$id";

            
    $results mysql_query($sql);

            
    index();
    }
    function 
    index() {
    ?><table width="615" border="0" cellspacing="2" cellpadding="2">
            <tr>
                <td width='**'><p><strong>Filename</strong></p></td>
                <td width='55' align="center" colspan="3"><p><strong>actions</strong></p></td>
            </tr><tr>
                <td colspan='2'><hr color='#333333' size='1'></td>
            </tr><?
            
    // create SQL statement
            
    $sql "SELECT * FROM files";

            
    // execute SQL query and get result
            
    $sql_result mysql_query($sql)
                or die(
    "Couldn't execute query.");

            
    // format results by row
            
    while ($row mysql_fetch_array($sql_result)) {
                
    $id $row["id"];
                
    $filename $row["filename"];

            
    // format html output
            
    echo "<tr>
                <td width='**'>
    $filename</td>
                <td width='55' align='center'><a href=\"?action=delete&id=
    $id\" onclick=\"return confirm('Are you sure you wish to delete this file?')\"><img src='images/but-delete.gif' width='50' height='15' border='0'></a></td>
            </tr>"
    ;
            }
            
    ?><tr>
                <td colspan='2'><hr color='#333333' size='1'></td>
            </tr><tr>
                <td width='**'><p></p></td>
                <td width='55' align='center'><a href='?action=add'><img src='images/but-add.gif' width='50' height='15' border='0'></a></td>
            </tr>
            </table>
    <? require('auth.inc'); ?>
    <html>
    <head>
        <title>Mando Group - Control-C</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <link href="styles/control_c.css" rel="stylesheet" type="text/css">
    </head>

    <body>

    <? include ('../includes/connect.inc'); ?>

    <table width="738" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
        <td><strong>Downloads</strong><br>
            <span class="textsmall">Add a new download</span></td>
    </tr><tr>
        <td align="center"><?
            
    if($action == 'upload'upload_file();
            else if(
    $action == 'add'upload_form();
            else if(
    $action == 'delete'delete_file();
            else 
    index();
        
    ?></td>
    </tr>
    </table>
    </body>
    </html>

  2. #2
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think your delete_file() function should be called with $id as parameter, otherwise I don't know how that function have any idea about $id.

  3. #3
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What would be the best (or only) way of doing that? I am very new to the use of functions.

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    delete_file($id);

  5. #5
    SitePoint Evangelist spoondevil's Avatar
    Join Date
    Jun 2001
    Location
    Harlow, Essex, UK
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers for that

    It works a treat.


Bookmarks

Posting Permissions

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