PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 300,406 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,652 people online right now. Registration is fast and FREE... Join Now!




problem on displaying and deleting

 

problem on displaying and deleting

mycraz

1 Jul, 2009 - 06:03 PM
Post #1

D.I.C Head
**

Joined: 10 Jun, 2009
Posts: 57



Thanked: 1 times
My Contributions
hi, I managed to display records according to categories in a table. Now I wanted to make a delete function to it but the delete button did not work. If I did not select records from list/menu, the delete button can work perfectly. However, I need to select records according to category. I did not know where to put '}' of the
CODE

if(isset($_POST['display']))

Can anyone help me with this?

CODE

<head>
<style type="text/css">
div
{
width:400px;
height:650px;
overflow: scroll
}
</style>
</head>
<div>
<?php

$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="spps"; // Database name

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

if(isset($_POST['display']))
{
$user = $_POST["levels"];
$sql="SELECT * FROM users WHERE level = '$user' ORDER BY user_id DESC ";
$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="1" cellpadding="3" cellspacing="1">
<tr>
</tr>
<tr>
<td align="center" ><strong>User Id</strong></td>
<td align="center" ><strong>Category</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? echo $rows['user_id']; ?></td>
<td align="center"><? echo $rows['level']; ?></td>
<td align="center" ><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
</tr>
<?php
}}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?
// Check if delete button active, start this
if($delete){
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=admin_submituser.php\">";
}
}}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

</div>


User is offlineProfile CardPM
+Quote Post


no2pencil

RE: Problem On Displaying And Deleting

1 Jul, 2009 - 06:14 PM
Post #2

i R L33t Skiddie, k?
Group Icon

Joined: 10 May, 2007
Posts: 13,228



Thanked: 289 times
Dream Kudos: 2875
Expert In: Goofing Off

My Contributions
QUOTE(mycraz @ 1 Jul, 2009 - 08:03 PM) *

CODE

...
if(isset($_POST['display']))
{
$user = $_POST["levels"];
...



Why do you check for display, but use levels?

QUOTE(mycraz @ 1 Jul, 2009 - 08:03 PM) *

CODE

...
<td align="center" ><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id'];
...
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];


Remember that JavaScript executes on the browser, & that PHP executes on the server.

So in order for PHP to read the values that the user checked, JavaScript will need to send the values in the Checkboxes back to PHP to be re-evaluated. At the time that PHP sends them, nothing is checked. By the time that they load on the clients browser & get checked, PHP is no longer able to read the input until it's sent through the form.
User is online!Profile CardPM
+Quote Post

mycraz

RE: Problem On Displaying And Deleting

1 Jul, 2009 - 07:22 PM
Post #3

D.I.C Head
**

Joined: 10 Jun, 2009
Posts: 57



Thanked: 1 times
My Contributions
CODE

...
if(isset($_POST['display']))
{
$user = $_POST["levels"];
...

$_POST['display'] is to check whether the display button is activated.
$_POST['levels'] is to check which categories the user chose. I assign it to $user to for the purpose of selection in sql query.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:53PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month