-4

I'm learning php, now I'm trying to get use with databases and etc stuff. My code (that is below) seems that is not working, and I can't solve it out why. It looks good but still after pushing "delete" nothing happens. Maybe you will be able to help me. Thanks in advance.

<?php
$host="localhost";
$username="root";
$password="";
$db_name="zurnalas";
$tbl_name="zurnalas";

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>
<form name="form1" method="post" action="">
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Žurnalų trinimas iš duomenų bazės</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Pavadinimas</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Kiekis</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Leidykla</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Metai</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Kaina</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['pavadinimas']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['kiekis']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['leidykla']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['metai']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['kaina']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="delete"></td>
</tr>

<?php

// Check if delete button active, start this 
if($_POST['delete']){
$checkbox[] = $_POST['checkbox[]'];
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=trinti.php\">";
}
}
mysql_close();
?>

</table>
</form>
</td>
</tr>
</table>
labasRyta
  • 13
  • 1
  • 5
  • Turn on error reporting and don't suppress messages. `$delete` looks to be undefined unless you are excluding part of your code. – Devon Bessemer Dec 05 '16 at 15:18
  • No it's the full code. How to set it up after pushing '`Delete`' button? – labasRyta Dec 05 '16 at 15:21
  • Since your form method is `post` and your input name is `delete`, PHP will make available at `$_POST['delete']`. – Devon Bessemer Dec 05 '16 at 15:22
  • you didn't assign a POST array to it, so what you have now won't work until you do. – Funk Forty Niner Dec 05 '16 at 15:25
  • @Devon oh ... I tried it before posting this. It didn't help me... I have edited the code. – labasRyta Dec 05 '16 at 15:26
  • The `if($delete)` is always false, because `$delete` is not initialized. Instead, use `$_POST['delete']` because when you click on your submit button, your form is submitted using method `POST`. So, PHP initializes an array $_POST – Anthony Dec 05 '16 at 15:28
  • @Fred-ii- explanation please? :D – labasRyta Dec 05 '16 at 15:28
  • @AnthonyB I have edited the code. It still doesn't work – labasRyta Dec 05 '16 at 15:29
  • ;you need to do a foreach with the post array and use their key values – Funk Forty Niner Dec 05 '16 at 15:29
  • What error PHP is giving to you ? To turn on error display, add `error_reporting(E_ALL)` at top of you php file – Anthony Dec 05 '16 at 15:31
  • Where are you closing the `form` tag? – Anthony Dec 05 '16 at 15:32
  • ^ that and `
    ` cannot be child of ``.
    – Funk Forty Niner Dec 05 '16 at 15:33
  • @AnthonyB forgot to copy that, edited the code now it's there. – labasRyta Dec 05 '16 at 15:39
  • @labasRyta have you tried to write an `var_dump($checkbox)`inside your `if ($_POST['delete'])`? I think you should remove `[]` on initializing of $checkbox. – Anthony Dec 05 '16 at 15:42
  • Also, you have two form opened, and only one closed. – Anthony Dec 05 '16 at 15:44
  • Solution: `if(isset($_POST['delete'])){ for($i=0;$i – labasRyta Dec 05 '16 at 15:45
  • Indeed I forgot to tell `isset`. Other possible problems could be your html tags, two form nested. – Anthony Dec 05 '16 at 15:47
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 05 '16 at 16:26
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Dec 05 '16 at 16:26

1 Answers1

-2

Your form's action field is empty. You have to get fields from form with php "$checkbox = $_POST['checkbox']". After you can do like you do, or you can write other mysql query " DELETE FROM YOUR_TABLE WHERE id in (1,2,3,4) ".