0

Trying to write a code that will delete a row from MySQL database. Getting error Undefined variable: del in C:\wamp64\www\allstuff\del_data.php on line 18, but i've already initialized $del var. I think, cause of that error my deleting function does not work. Here is the code:

error_reporting(E_ALL & ~E_DEPRECATED);

$hostname = "localhost";
$username = "root";
$password = "";

$dbName = "lage_base";
$table = "lage_table";

$conn = mysql_connect($hostname, $username, $password);

mysql_connect($hostname, $username, $password) or die ("No Connection");

mysql_select_db($dbName) or die (mysql_error());

$del = $query = "DELETE FROM $table WHERE id='$del'";
//mysql_select_db('lage_base');
mysql_query($query) or die(mysql_error());

$query = "SELECT * FROM lage_table";
$res = mysql_query($query) or die(mysql_error());
$row = mysql_num_rows($res);
//$retval = mysql_query($query,$conn);

echo ('
    <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
        <html xmlns=\"http://www.w3.org/1999/xhtml\">

        <head>

            <meta http-equiv=\"Content-Type\" content=\"text/html; charset=windows-1251\" />

            <link rel="stylesheet" href="style.css" type="text/css">

        </head>
    <body id="viewData">
        <table>
            <tr>
            <td id="nummer"><b>#</b></td>
            <td align = \"center\" id="date"><b class="tabtext">Datum</b></td>
            <td align = \"center\" id="name1"><b class="tabtext">Name</b></td>
            <td align = \"center\" id="stuck"><b class="tabtext">Stuck</b></td>
            </tr>
    ');

while ($row = mysql_fetch_array($res)) {
echo "<tr>\n";
echo "<td class='rowCont'>".$row['id']."</td>\n";
echo "<td class='rowCont'>".$row['data']."</td>\n";
echo "<td class='name2'>".$row['name']."</td>\n";
echo "<td class='rowCont'>".$row['stuck']."</td>\n</tr>\n";
echo "<td><a name=\"del\" href=\"del_data.php?del=".$row["id"]."\">Loschen</a></td>\n";
echo "</tr>\n";
}

echo ("</table>\n");

mysql_close();

echo ("<div style=\"text-align: center; margin-top: 10px;\"><a href=\"index.html\">Zuruck!</a></div>");


?>

I would appreciate every answer! PS. sry for my low skill and stupid questions (=.

1 Answers1

1

Try like this:

$del = $_GET['del'];
$query = "DELETE FROM $table WHERE id='$del'";
Md Mahfuzur Rahman
  • 2,319
  • 2
  • 18
  • 28
  • Thank u sir! That works. If it would not be hard for you, could u explain me why can't i use my code? – Alex Kuleshov Apr 29 '16 at 15:32
  • Variable must be initialized/defined before using it. The variable `$del` you used in query was not initialized. In my code, I received the `del` variable by using `$_GET` method and assign it into `$del` variable. Then I used the `$del` variable. – Md Mahfuzur Rahman Apr 29 '16 at 15:36
  • Thanks for ur deep explanation! Best regards – Alex Kuleshov Apr 29 '16 at 15:57