Вопрос

I'm trying to have a DELETE link printed alongside each $row. Also, on another page, have a "DOWNLOAD" link - they should work the same way. The problem is this: I am getting both DELETE and DOWNLOAD to print out alongside each record, but using the code I have, nothing happens or my script bails out on an error. For some reason I have the most complex features of my site up and running but this still eludes me. Here's some code:

    while ($row = mysqli_fetch_array($r,MYSQLI_ASSOC))
    {
  echo '<tr><td align="left">' .
    $row['title'] . '</td><td align="left">'
    . $row['genre'] . '</td><td align="left">'
    . $row['length'] . '</td><td align="left">'
    . $row['created'] . '</td><td align="left">'
    . $row['views'] . '</td><td align="left">'
    . "<a href='wprofile.php?id={$row['upload_id']}'>Delete</a></td> . '</td></tr>';
    }

and on the same php page I have this logic which is failing me. the example is for delete but the same fail follows for my DOWNLOAD link problem.

    if(isset($_GET['upload_id']))
    {
    $id = intval($_GET['upload_id']);

    require_once (dbconnectionpath);

    $delquery = "DELETE FROM 'upload' WHERE 'upload_id' = {$id}";
    $done = @mysqli_query ($dbc, $delquery); //run the query

    if($done) {
    if (mysqli_num_rows($done)==1) {
    echo 'Record deleted';
    }
    else{
    echo 'Delete failed';
    }

    @mysqli_free_result($result);
    }
    else{
    echo "Error! Query failed: <pre>{$dbc->error}</pre>";
    }
    mysqli_close(dbconnection);

the error I am getting is "Error, query failed"

Это было полезно?

Решение

if(isset($_GET['upload_id'])) change to if(isset($_GET['id']))

and $id = intval($_GET['id']);

The HTML parameter is id, not upload_id

Другие советы

In your link, you appear to be setting id:

<a href='wprofile.php?id={$row['upload_id']} ...

but your code references $_GET['upload_id']. In other words, you're referencing the wrong name in your code. Either use id in the code or upload_id in the link.

One other thing though this may just be because I can't see all the HTML - you appear to have to many </td> elements in your code. Check the one at the end of your link line though, as stated, this may close off an earlier <td> that isn't shown.


If, as you mention in the comments, your "view source" is showing the link as:

<a href='newwriter_profile.php?id='>Delete</a></td>

then there's something wrong with your creation of that HTML link.

There are a number of things that could be going wrong there, the first couple that spring to mind are that your original query doesn't have update_id in it, or the ID is indeed blank for the row you're looking at.

Check the value of $row['update_id'] before generating the HTML link line, that should confirm or deny that hypothesis at least. Beyond that, you'll have to figure out why your link is incorrect.


One thing I have just noticed, which you may want to look into, is your use of quotes:

$delquery = "DELETE FROM 'upload' WHERE 'upload_id' = {$id}";

Now I know MySQL uses backticks to sometimes protect table and column names from incorrect parsing (such as with select `column with spaces` from tbl1 ...), but your code seems to have single quotes rather than backticks.

It's just one more thing to check out. It may be that the where clause in your query is comparing the literal string "upload_id" with your variable, rather than comparing the upload_id column.

That may explain why your delete is not hitting any rows since your ID is (probably) an auto-incrementing integer field, meaning it would never be equal to a non-numeric string literal.

Try removing the curly brackets, beacuse when you print out the query created it shows the value of the id with curly braces which might result in not deleting the id.

This should work : "DELETE FROM upload WHERE upload_id = $id ";

to prevent the page directing to another page use preventdefault js function.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top