473,738 Members | 1,949 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 1547
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
2433
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
2030
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
2458
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
2364
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
1751
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
1913
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
2070
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
9694
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
2235
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
1626
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
8968
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
8787
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
8208
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...
1
6750
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
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
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.