Jump to content

Can't delete record from MySQL with PHP!


dbk

Recommended Posts

Hi

 

I can't seem to delete from the database with the "DELETE RECORD" button, please help!

It works fine when saving records to the database!

 

CODE:

 

<?php // sqltest.php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());

if (isset($_POST['author']) &&
    isset($_POST['title']) &&
    isset($_POST['category']) &&
    isset($_POST['year']) &&
    isset($_POST['isbn']))
{
    $author = get_post('author');
    $title = get_post('title');
    $category = get_post('category');
    $year = get_post('year');
    $isbn = get_post('isbn');
if (isset($_POST['delete']) && $isbn != "")
{
    $query = "DELETE FROM classics WHERE isbn='$isbn'";
    if (!mysql_query($query, $db_server))
    echo "DELETE failed: $query<br />" .
    mysql_error() . "<br /><br />";
}
else
{
    $query = "INSERT INTO classics VALUES" .
    "('$author', '$title', '$category', '$year', '$isbn')";
    if (!mysql_query($query, $db_server))
    echo "INSERT failed: $query<br />" .
    mysql_error() . "<br /><br />";
}
}
echo <<<_END
<form action="sqltest.php" method="post"><pre>
    <table border=0 color=white>
    <tr>
    <td>Author</td><td><input type="text" name="author" /></td>
    </tr>
    <tr>
    <td>Title</td><td><input type="text" name="title" /></td>
    </tr>
    <tr>
    <td>Category</td><td><input type="text" name="category" /></td>
    </tr>
    <tr>
    <td>Year</td><td><input type="text" name="year" /></td>
    </tr>
    <tr>
    <td>ISBN</td><td><input type="text" name="isbn" /></td>
    </tr>
    <tr>
    <td><input type="submit" value="ADD RECORD" /></td>
    </tr>
</pre></form></table>
_END;
$query = "SELECT * FROM classics";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo <<<_END
    <form action="sqltest.php" method="post"><pre>
        <table border=0 cellspacing=5>
        <tr bgcolor=#ECECEC>
        <td width=100px>Author</td><td>:</td><td width=300px>$row[0]</td>
        </tr>
        <tr bgcolor=#ECECEC>
        <td>Title</td><td>:</td><td>$row[1]</td>
        </tr>
        <tr bgcolor=#ECECEC>
        <td>Category</td><td>:</td><td>$row[2]</td>
        </tr>
        <tr bgcolor=#ECECEC>
        <td>Year</td><td>:</td><td>$row[3]</td>
        </tr>
        <tr bgcolor=#ECECEC>
        <td>ISBN</td><td>:</td><td>$row[4]</td>
        </tr>
        <tr>
        <td></td>
        <td></td>
        <td>
        <input type="hidden" name="delete" value="yes" />
        <input type="hidden" name="isbn" value="$row[4]" />
        <input type="submit" value="DELETE RECORD" />
        </td>
        </tr>
    </pre></table></form>
_END;
}
mysql_close($db_server);
function get_post($var)
{
    return mysql_real_escape_string($_POST[$var]);
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.