0

I am trying to implement a UI which the user would be able to select entries by selecting checkboxes to delete them from the database. However, while everything looks fine and able to display, there is an error saying Notice: Undefined variable: delete in /opt/uiForm.php on line 154 whereas $delete is not readable. I am following the example given here. Am I missing anything?

<body>

<form name="frm" method="get" action="doSubmit();">


<?php

// Connect to server and select database.
 mysql_connect("127.0.0.1:3306", "root", "")or die("cannot connect"); 
 mysql_select_db("PushApplication")or die("cannot select DB");

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

$count=mysql_num_rows($result);

?>
 <table width="400" border="0" cellspacing="1" cellpadding="0">
 <tr>
 <td><form name="form1" method="post" action="">
 <table width="400" border="0" cellspacing="1" bgcolor="#CCCCCC">

 <tr>
 <td align="center" bgcolor="#FFFFFF">#</td>
 <td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>DeviceID</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>DeviceType</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>Description</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>OS_Version</strong></td>
 <td align="center" bgcolor="#FFFFFF"><strong>Status</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['DeviceID']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['DeviceType']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['Description']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['OS_Version']; ?></td>
 <td bgcolor="#FFFFFF"><? echo $rows['Status']; ?></td> 
 </tr>
<?php
 }
 ?>
 <tr>
 <td colspan="7" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
 </tr>
<?php
// Check if delete button active, start this
 if($delete){
 for($i=0;$i<$count;$i++){
 $del_id = $checkbox[$i];
 $sql = "DELETE FROM Device WHERE ID='$del_id'";
 $result = mysql_query($sql);
 }

// if successful redirect to delete_multiple.php 
 if($result){
 echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
 }
 }
 mysql_close();
 ?>
 </table>
 </form>
 </td>
 </tr>
 </table>

</form>
</body>
</html>
Hend
  • 589
  • 5
  • 15
  • 35
  • 2
    You can't have a `` tag in the middle of a ``, it's not valid HTML.
    – nickb Jan 30 '12 at 07:01
  • Where is `$delete` defined? I don't see it anywhere in your code or the code in the example. When you say `if ($delete) { ... }`, what are you actually checking? – Logan Serman Jan 30 '12 at 07:04
  • I can see you having problems with this code further down the line. Look into frameworks like Yii, Symfony, CodeIgniter, Cake, etc. They all provide a really solid codebase for basic CRUD functions like this. – Dan Blows Jan 30 '12 at 07:06

3 Answers3

1

You really should be using

if (!empty($_POST['delete'])) {

instead of

if ($delete) {

to avoid the notice you are getting. Also your code is prone to the so called SQL injection vulnerability which you read upon if you are considering to put it somewhere on the web.

Also you should be using method="post" instead of GET for this type of operations.

Martin Jansen
  • 276
  • 1
  • 7
0

I suspect the other example was not displaying all errors.

When checking for the existence of a variable, you need to use if(!empty($delete)), because if returns an error when the variable it's checking does not exist. empty() does not.

Dan Blows
  • 20,846
  • 10
  • 65
  • 96
0

Your code supposes that server's PHP has option register_globals = On; Which is not true by default for the late versions of PHP setup. Read this What are register_globals in PHP?

Community
  • 1
  • 1
Cheery
  • 16,063
  • 42
  • 57