473,698 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with check boxes.

1 New Member
hi ,


i wrote a code for to delete the particular rows in a table using check boxes.But in that i select the particular rows and click on the delete button these rows didn't delete.plz check it what's the problem in that code.


<?php
$host="localhos t"; // Host name
$username="root "; // Mysql username
$password=" "; // Mysql password
$db_name=" db1"; // Database name
$tbl_name="test _mysql"; // Table name

// 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_q uery($sql);

$count=mysql_nu m_rows($result) ;

?>
<table width="400" border="0" cellspacing="1" cellpadding="0" >
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCC C">
<tr>
<td bgcolor="#FFFFF F">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFF F"><strong>Dele te multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFF F">#</td>
<td align="center" bgcolor="#FFFFF F"><strong>I d</strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Name </strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Last name</strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Emai l</strong></td>
</tr>
<?php
while($rows=mys ql_fetch_array( $result)){
?>
<tr>
<td align="center" bgcolor="#FFFFF F"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFF F"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFF F"><input name="delete" type="submit" id="delete" value="Delete"> </td>
</tr>
<?php
echo "hello";
// Check if delete button active, start this

if($delete) {
echo "hello";

for($i=0;$i<$co unt;$i++){
$del_id = $checkbox[$i];
echo "$del_id";
echo "hello";
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sq l);
}

// if successful redirect to delete_multiple .php
if($result){
echo "<meta http-equiv=\"refresh \" content=\"0;URL =checkk.php\">" ;
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
Apr 14 '08 #1
2 1545
Markus
6,050 Recognized Expert Expert
Does $result return true or false?

It is strongly recommended that you do not use register globals - php.net.

Regards,
Apr 14 '08 #2
dlite922
1,584 Recognized Expert Top Contributor
hi ,


i wrote a code for to delete the particular rows in a table using check boxes.But in that i select the particular rows and click on the delete button these rows didn't delete.plz check it what's the problem in that code.


<?php
$host="localhos t"; // Host name
$username="root "; // Mysql username
$password=" "; // Mysql password
$db_name=" db1"; // Database name
$tbl_name="test _mysql"; // Table name

// 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_q uery($sql);

$count=mysql_nu m_rows($result) ;

?>
<table width="400" border="0" cellspacing="1" cellpadding="0" >
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCC C">
<tr>
<td bgcolor="#FFFFF F">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFF F"><strong>Dele te multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFF F">#</td>
<td align="center" bgcolor="#FFFFF F"><strong>I d</strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Name </strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Last name</strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Emai l</strong></td>
</tr>
<?php
while($rows=mys ql_fetch_array( $result)){
?>
<tr>
<td align="center" bgcolor="#FFFFF F"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFF F"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFF F"><input name="delete" type="submit" id="delete" value="Delete"> </td>
</tr>
<?php
echo "hello";
// Check if delete button active, start this

if($delete) {
echo "hello";

for($i=0;$i<$co unt;$i++){
$del_id = $checkbox[$i];
echo "$del_id";
echo "hello";
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sq l);
}

// if successful redirect to delete_multiple .php
if($result){
echo "<meta http-equiv=\"refresh \" content=\"0;URL =checkk.php\">" ;
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
first, your logic is backwards. why delete AFTER you've displayed the page. and you need to turn of register_global s and access your page variables with POST[] (if that's the method for the form)

Here's how it should happend. Display list, mark and submit, display the list again.

This is done in one page refresh.

I've modified your code, but not checked, you'll get the point::

[PHP]


<?php
$host="localhos t"; // Host name
$username="root "; // Mysql username
$password=" "; // Mysql password
$db_name=" db1"; // Database name
$tbl_name="test _mysql"; // Table name
// 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");
// if delete is set, delete the values
if(isset($_POST['delete']))
{
foreach ($_POST['checkbox'] as $del_id)
{
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sq l) or die("I made a boo boo: " . mysql_error());

}
}
// get all records from DB from display (will not include those deleted above)
$sql="SELECT * FROM $tbl_name";
$result=mysql_q uery($sql);
mysql_close();

?>
<table width="400" border="0" cellspacing="1" cellpadding="0" >
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCC C">
<tr>
<td bgcolor="#FFFFF F">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFF F"><strong>Dele te multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFF F">#</td>
<td align="center" bgcolor="#FFFFF F"><strong>I d</strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Name </strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Last name</strong></td>
<td align="center" bgcolor="#FFFFF F"><strong>Emai l</strong></td>
</tr>
<?php
while($rows=mys ql_fetch_assoc( $result)){ //<-- changed this to fetch_assoc
?>
<tr>
<td align="center" bgcolor="#FFFFF F"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFF F"><?php echo $rows['id']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['name']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['lastname']; ?></td>
<td bgcolor="#FFFFF F"><?php echo $rows['email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFF F"><input name="delete" type="submit" id="delete" value="Delete"> </td>
</tr>
</table>

[/PHP]

Let me know if any questions. Sorry about any mistakes.
Apr 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2430
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head> <body> <form name="myform" action=test.php method=post>
9
2028
by: | last post by:
hi everyone, i was hoping someone could help me. the code below - in IE the menu options (text) are a couple of pixels too low and i cant move them up, and the form boxes are ok. in mozilla the reverse is true - the text links are ok but the form boxes are too low! I cant figure out why this is happening. I want the text to be vertically centered in the black menu bar, and the form boxes likewise. any help greatly appreciated!
3
2455
by: Adam Toline | last post by:
In reference to the following: http://www.bellecose.com/form.htm At the top of each column there is a box for "All". When one is checked I need to check all of (and only) those boxes underneath. Now, the rub here is that every checkbox on the page (except the "All"s)
2
2362
by: aaj | last post by:
Hi all I have a continuous bound form and on each record is a tick box. The user ticks the boxes and these boxes define the batch. for future operations before they leave the page I count the number of ticks using (ADO) rst.Open "SELECT COUNT(update_po_ref) AS update_qty FROM dbo.tbl_forecasted
4
1747
by: Mark C | last post by:
I currently have an Excel spreadsheet with numerous text boxes and check boxes that I want to populate with data from an Access 97 database. I have used Excel automation in Access before but only to update cells not controls like text boxes. Can someone steer me onto the correct path? I am not using a VBA form in Excel it is just merely various text boxes and checkboxes placed on a worksheet. Any help would be much appreciated.
2
1910
by: muthu | last post by:
Hi freinds, In my aspx page i have generated some check boxes dynamically using dhtml. When i check any check box and submit the form,how can i capture the value of check box. How should i identify wich check box is been checked.And the check boxes are not server controls.They are the ordinary html check boxes.Can any one help me out
1
2068
by: DJG79 | last post by:
Hi all, I am using an open source menu that i found and it works great, except for one thing that when the web page is not scrolled to the very top the drop down links will not stay visible. Has anyone else had this sort of problem with javascript? and any ideas how to fix it would be greatly appreciated.. I have included a copy of the code below, thanks. /**
9
9692
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a master-child link to the first subform. subform2 - Master Field: subTrainingModule.Form!TrainingModuleTopicSK Child Field: TrainingModuleTopicSK
2
2229
by: useasdf4444 | last post by:
I have the following program: #include <stdio.h> #include <string.h> #include <stdlib.h> void main () { char command; char *com,*pos,*stack1,*stack2; int boxes,boxtable,i,j,k,st1,st2; int check=1;
1
1625
by: Java Kumar | last post by:
Hi Friends, I have a form which contains elements such as check boxes,text box,text area ., Problem is in Check boxes. By default, Check boxes are unchecked and text boxes are blank. I clicked one check box and entered some values in text boxes. then After a page refresh, the values of text boxes are cleared.But the check box is appears with check mark. the default value (unchecked) is not restored... Why ? How can i...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7739
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.