Hi nav, I have already made a dynamic table where the checkbox values are stored in db table and printed on the screen see code below..

what i want to do is edit/delete the database values in a php form. so when it can change the data in the db and countries can be added or deleted on a form rather than making changes in MYPHPADMIN.

Cheers :)

<form id="form1" name="form1" method="post" action="page2.php">
<?php
$sql = 'SELECT * From Countries';

$con = mysql_connect("localhost","root","mariapa") or die;


$result = mysql_db_query(talk21, $sql);

while ($rec = mysql_fetch_array($result))
{
print "\n<input type='checkbox' name=Countries[]' value='".$rec."' > ".$rec." </input>";
}

?><br />
<input type="submit" name="Submit" value="Register" />
</label>
</form>

The second script lists all the teams in the table. Select the ones which you want to delete and click on submit! It ll delete all those teams from the table. I don't know how you can edit and what you mean by edit !

By edit I mean to an existing record that has been added to the database e.g

if i have a record which i want to change

id| name|last_name|countries
1 priya rai BR, FR

Say i just wanted to change the record name from priya to rianah without changing anything else

id| name|last_name|countries
1 rianah rai BR, FR

While listing the records, show previous name in a textbox and when submit is clicked, update it ?

While listing the records, show previous name in a textbox and when submit is clicked, update it ?

yes thats right i will have record set displaying all the user informtion on one big table. user can look at this table and and user can select onclick or by checking a checkbox and the record that is selected like e.g 2 kelle bria BR can be changed or deleted on another page.

id| name|last_name|countries
1 priya rai BR, FR
2 kelle bria BR
3 adam kel GB

While listing the records, show previous name in a textbox and when submit is clicked, update it ?


Naveen I am wondering if its possible to edit in the record set by checkbox and redirect user to change records like a textbox field?

Hi, Sorry. I didn't get it. Could you please re-explain the whole thing to me ? Is it like this.
You have a textbox to enter the name, list of checkboxes to select the country and a submit button. You can add records to the table from this screen. You need another screen where you can edit the records in the table ? (change the name and checkbox values ?) Is this what you are asking ?

Yes exactly thats what i mean.

<?php
$conn=mysql_connect("localhost","root");
mysql_select_db("test");
$operation=$_POST['operation'];
$id=$_POST['id'];
if(isset($_POST['submit'])) {
		$team=implode(",",$_POST['team']);
		$name=$_POST['name'];
		$q="insert into table1 (name, team) values ('$name','$team')";
		mysql_query($q);
}
if($operation=="delete"){
	$q="delete from table1 where counter='$id'";
	mysql_query($q);
}
if(isset($_POST['update'])){
	$name=$_POST['name'];
	$team=implode(",",$_POST['team']);
	$q="update table1 set name='$name', team='$team' where counter='$id'";
	mysql_query($q);
}
?>
<html>
<body>
<form method="post" name="insertform" action="checkbox1.php">
Enter name: <input type="text" name="name" /><br />
<input type="checkbox" name="team[]" value="AG">Argentina <br />
<input type="checkbox" name="team[]" value="BR">Brazil <br />
<input type="checkbox" name="team[]" value="GE">Germany <br />
<input type="checkbox" name="team[]" value="EN">England <br />
<input type="submit" name="submit" value="submit">
</form>
<hr>
<form method="post" name="editform" action="checkbox1.php">
<input type="hidden" name="id">
<input type="hidden" name="operation">
<?php
	$q="select counter,name from table1";
	$result=mysql_query($q);
	while($row=mysql_fetch_array($result)){
		$name=$row['name'];
		$id=$row['counter'];
		echo $name ."<input type=\"button\" name=\"edit\" value=\"edit\" onclick=\"javascript: document.editform.operation.value='edit';document.editform.id.value='$id';document.editform.submit();\"><input type=\"button\" name=\"delete\" value=\"Delete\" onclick=\"javascript: document.editform.operation.value='delete';document.editform.id.value='$id'; document.editform.submit();\"><br />";
	}
?>
</form>
<hr>
<?php
if($operation=="edit") {
	$id=$_POST['id'];
	?>
	<form method="post" name="updateform" action="checkbox1.php">
	<input type="hidden" name="id" value="<?php echo $id; ?>">
	<?php
	$q="select * from table1 where counter='$id'";
	$result=mysql_query($q);
	$row=mysql_fetch_array($result);
	$name=$row['name'];
	$team=$row['team'];
	$teamarray=explode(",",$team);
	echo "<input type=\"text\" name=\"name\" value=\"$name\"><br />";
?>
<input type="checkbox" name="team[]" value="AG" <?php if(in_array("AG",$teamarray)){ echo "checked"; } ?>>Argentina <br />
<input type="checkbox" name="team[]" value="BR" <?php if(in_array("BR",$teamarray)){ echo "checked"; } ?>>Brazil <br />
<input type="checkbox" name="team[]" value="GE" <?php if(in_array("GE",$teamarray)){ echo "checked"; } ?>>Germany <br />
<input type="checkbox" name="team[]" value="EN" <?php if(in_array("EN",$teamarray)){ echo "checked"; } ?>>England <br />
<input type="submit" name="update" value="update">
<?php
}
?>
</body>
</html>

Well, Here is the code. The code is lil confusing (and untidy) but I am in no mood to write any better code. I am doing everything in 1 page. You can insert, edit and delete a record. Hope this will fix your problem.

Cheers,
Naveen

if(isset($_POST['team']))
{ 
	foreach($_POST['team'] as $value)
	{
	$insert="INSERT INTO team('team')VALUES ('$value')";
	echo "$insert<br>";
	}
}

are you want to do like this???

if(isset($_POST['team']))
{ 
	foreach($_POST['team'] as $value)
	{
	$insert="INSERT INTO team('team')VALUES ('$value')";
	echo "$insert<br>";
	}
}

are you want to do like this???

I guess you didn't read all the posts.

The code looks good but i have this error,


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\examples\new.php on line 42

the query isn't returning any resultset. Check your query, execute it in phpmyadmin/mysql and see if it returns any value.

the query isn't returning any resultset. Check your query, execute it in phpmyadmin/mysql and see if it returns any value.

I have managed to run the code, thanks for the tip;) , I executed the query in phpadmin and from there it was obvious I had counter missing in the table.

Now the problem I have is I cant add anything to the table or edit or delete. The input box name is there and the check boxes are there but when I submit nothing is added. The same goes for edit and delete when clicked the page checkbox1.php is loaded with no effect or change.

The table in mysql is as follows

Table1


Id   int    Not null    auto_increment
name        varchar 30  Not null
team        varchar 30  Not null
Counter     varchar 30  Not null



<?php
$con = mysql_connect("localhost","root","talk21") or die;
mysql_select_db("talk21");
$operation=$_POST;
$id=$_POST;
if(isset($_POST)) {
$team=implode(",",$_POST);
$name=$_POST;
$q="insert into table1 (name, team) values ('$name','$team')";
mysql_query($q);
}
if($operation=="delete")
{
$q="delete from table1 where counter='$id'";
mysql_query($q);
}
if(isset($_POST)){
$name=$_POST;
$team=implode(",",$_POST);
$q="update table1 set name='$name', team='$team' where counter='$id'";
mysql_query($q);
}
?>

This is the processing page checkbox1.php

<html>
<body>
<form method="post" name="insertform" action="checkbox1.php">
Enter name: <input type="text" name="name" /><br />
<input type="checkbox" name="team[]" value="AG">Argentina <br />
<input type="checkbox" name="team[]" value="BR">Brazil <br />
<input type="checkbox" name="team[]" value="GE">Germany <br />
<input type="checkbox" name="team[]" value="EN">England <br />
<input type="submit" name="submit" value="submit">
</form>
<hr>


<form method="post" name="editform" action="checkbox1.php">
<input type="hidden" name="id">
<input type="hidden" name="operation">


<?php
$con = mysql_connect("localhost","root","talk21") or die;
mysql_select_db("talk21");
$q="select counter,name from table1";
$result=mysql_query($q);
while($row = mysql_fetch_array($result )){
$name=$row;
$id=$row;
echo $name ."<input type=\"button\" name=\"edit\"
value=\"edit\" onclick=\"javascript:
document.editform.operation.value='edit';document.editform.id.value='$id';document.editform.submit();\"><input type=\"button\" name=\"delete\" value=\"Delete\" onclick=\"javascript:
document.editform.operation.value='delete';document.editform.id.value='$id'; document.editform.submit();\"><br />";
}
?>
</form>
<hr>


<?php
$con = mysql_connect("localhost","root","talk21") or die;
mysql_select_db("talk21");
if($operation=="edit")
{   $id=$_POST;
?>  <form method="post" name="updateform" action="checkbox1.php">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<?php    $q="select * from table1 where counter='$id'";
$result=mysql_query($q);
$row=mysql_fetch_array($result);
$name=$row;
$team=$row;
$teamarray=explode(",",$team);
echo "<input type=\"text\" name=\"name\" value=\"$name\"><br />";
?>
<input type="checkbox" name="team[]" value="AG" <?php
if(in_array("AG",$teamarray)){ echo "checked"; } ?>>Argentina <br />
<input type="checkbox" name="team[]" value="BR" <?php
if(in_array("BR",$teamarray)){ echo "checked"; } ?>>Brazil <br />
<input type="checkbox" name="team[]" value="GE" <?php
if(in_array("GE",$teamarray)){ echo "checked"; } ?>>Germany <br />
<input type="checkbox" name="team[]" value="EN" <?php
if(in_array("EN",$teamarray)){ echo "checked"; } ?>>England <br />
<input type="submit" name="update" value="update">
<?php
}
?>
</body>
</html>

Again, print out all your queries. Check if they insert/update and delete the record. It worked fine when I wrote the code. I don't know where you have misplaced something.

the query isn't returning any resultset. Check your query, execute it in phpmyadmin/mysql and see if it returns any value.

Again, print out all your queries. Check if they insert/update and delete the record. It worked fine when I wrote the code. I don't know where you have misplaced something.

Hi naveen, the whole process is working now, but when you update/delete it affects all the records you have submitted to the database e.g. if you have 10 countries and you wanted to edit only 1, it changes all. Also I wanted to display the records in a table and onclick just change the selected record.

I am populating the checkbox from a database and printing them on the form like below. I want a form to basically edit the values like in an admin section.

I don’t require filling in the checkbox, because only users will be doing that process, I want to update the existing checkbox values or add new checkbox.

select Countries::
<?php
$sql = 'SELECT * From Countries';
$con = mysql_connect("localhost","root","talk21") or die;
$result = mysql_db_query(lastr, $sql);
while ($rec = mysql_fetch_array($result))
{
print "\n<input type='checkbox' name=Countries[]' value='".$rec."' > ".$rec." </input>";
}
?>

thanx for the help.

Hi naveen, the whole process is working now, but when you update/delete it affects all the records you have submitted to the database e.g. if you have 10 countries and you wanted to edit only 1, it changes all.

Are you updating the table depending on the unique key ? If you try updating the table like, update table1 set col1='value1', col2='value2'"; , it will update all the records.

I don’t require filling in the checkbox, because only users will be doing that process, I want to update the existing checkbox values or add new checkbox.

I still don't understand the flow of your program.

Are you updating the table depending on the unique key ? If you try updating the table like, update table1 set col1='value1', col2='value2'"; , it will update all the records.

I still don't understand the flow of your program.

FORM
user can view form,there are a few textboxes and a few checkboxes and listboxes.


the data in the listboxes and checkboxes are populated from the database and printed on the form.

e.g print "\n<input type='checkbox' name=Countries[]' value='".$rec."' > ".$rec." </input>";

what i want do:

update checkboxes and listboxes. update team checkboxes values

1.edit teams

2. add/delete


so the form can be updated.

Hi i'm i making any sense or do need more clarification?

For now all i want to do is add/delete checkboxes in the database in a php form.

As I told you already, If you want to 'update' checkboxes, ie., If you want to delete a checkbox, you have to delete all the checkboxes and then insert only those checkboxes which are checked. I don't know how you can 'add' checkboxes.

As I told you already, If you want to 'update' checkboxes, ie., If you want to delete a checkbox, you have to delete all the checkboxes and then insert only those checkboxes which are checked. I don't know how you can 'add' checkboxes.

I think we are still confused, I don’t want to add a checkbox like that at all.

I have 1 table called team, with 2 columns.

Id team
1 BR
2 GB
3 FR

the only purpose this table serves is to be printed on the form dynamically instead of hard coding the checkboxes.

$result = mysql_db_query(team, $sql); 
 
   while ($rec = mysql_fetch_array($result)) 
    {
       print "\n<input type='checkbox' name=team[]' value='".$rec['id']."' > ".$rec['team']." </input>";

So all I want to do is update what’s on the team table, so the form can dynamically show what’s on the table, I know I can just add the values on phpmyadmin but I want to make a admin page so admin can change what appears on the forms.


id team
1 BR
2 GB
3 FR

cheers naveen

Thats very simple.
1. To add a team: Have a textbox where the user can enter a team and submit it.
2. To update/delete a team, list all the teams as you are doing here. By default, check all the checkboxes. The user can remove a team by clearing the checkbox and click on submit. When the user clicks on submit, remove all the records from team and insert only those which are checked. So, in the end, the table will have only those teams which were checked. I hope this is what you are saying.

Thats very simple.
1. To add a team: Have a textbox where the user can enter a team and submit it.
2. To update/delete a team, list all the teams as you are doing here. By default, check all the checkboxes. The user can remove a team by clearing the checkbox and click on submit. When the user clicks on submit, remove all the records from team and insert only those which are checked. So, in the end, the table will have only those teams which were checked. I hope this is what you are saying.

Ok, i tried this but I have an error on deleteting and updating, when user submits, it should delete all the unset checkboxes.

<form id="form1" name="form1" method="post" action="result.php">
    
team : <input type="text" name="team" /><br />

    <br>
    
    <?php
    $sql = 'SELECT * From team';
 
    $con = mysql_connect("localhost","root","talk21") or die;
    $result = mysql_db_query(lastr, $sql); 
    
   while ($rec = mysql_fetch_array($result)) 
    {
       print "\n<input type='checkbox'  name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>"; 
       }
  if(isset($_POST['submit'])){
  }
  if(unset($_GET['id']))	{
  $q="delete from team where team[]='$id'";
            	mysql_query($q);
              }            
?>
  <input type="submit" name="Submit" value="Register" />
  </label>
</form>

That wont work because it wouldn't know what are the unset fields. Why don't u delete all the records and insert only those which are checked ?

Thats very simple.
1. To add a team: Have a textbox where the user can enter a team and submit it.
2. To update/delete a team, list all the teams as you are doing here. By default, check all the checkboxes. The user can remove a team by clearing the checkbox and click on submit. When the user clicks on submit, remove all the records from team and insert only those which are checked. So, in the end, the table will have only those teams which were checked. I hope this is what you are saying.

That wont work because it wouldn't know what are the unset fields. Why don't u delete all the records and insert only those which are checked ?

This is as close as i come to what you have mentioned above, the code is error free but does not do anything. I am looping trough the checkboxes and trying to remove the ckecked checkboxes.

<form id="form1" name="form1" method="post" action="edit.php">
   
team: <input type="text" name="team" /><br />
    <br>
        <?php
    $sql = 'SELECT * From team';
    $con = mysql_connect("localhost","root","talk21") or die;
    $result = mysql_db_query(lastr, $sql); 
       while ($rec = mysql_fetch_array($result)) 
    {
       print "\n<input type='checkbox'  name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>"; 
       }
       ?>
  <?      
  if(isset($_POST['delete'])){
  }
 $id = $_GET['id'];
 (!isset($_GET['id'])) 
 { 
 foreach($_GET['team'] as $value)
	{ 
 
 $delete = "DELETE FROM team WHERE id='$id' ";
 mysql_query($delete);
 mysql_close();


echo "Entry deleted";           
?>
  <input type="submit" name="Submit" value="submit" />
  </label>
</form>

No. I didn't mean this. Well, Imagine I have 5 records in my table which resemble the checkboxes like above.

id | team
1 | team1
2 | team2
3 | team3
4 | team4
5 | team5
if(isset($_POST['submit'])){
  $q="delete from team"; //delete all the teams
  mysql_query($q);
  foreach($_POST['team'] as $value){
    $q="insert into table (team) values ('$value')";
mysql_query($q); //so by the end, the table will have records which were checked and all the records which were unchecked will be removed.
 }
}
//I list these teams in my form. 
$q="select id,team from table";
$res=mysql_query($q);
echo "<form method='post' action='check.php'>";
while($row=mysql_fetch_array($res)){
  echo "<input type='checkbox' name='team[]' value='$id' checked>".$team."<br />"; //this will list all the teams in the database
}
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";

:) I dont think I can think more than that.

There are errors in the code,

Warning: Invalid argument supplied for foreach() in C:\wamp\www\dynamic\edit.php on line 16

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\dynamic\edit.php on line 25

<form id="form1" name="form1" method="post" action="edit.php">
   
   team: <input type="text" name="team" /><br />
    <br>
        <?php
    $sql = 'SELECT * From team';
    $con = mysql_connect("localhost","root","talk21") or die;
    $result = mysql_db_query(lastr, $sql); 
       while ($rec = mysql_fetch_array($result)) 
    {
       print "\n<input type='checkbox'  name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>"; 
 }
 if(isset($_POST['submit'])){
  $q="delete from team"; //delete all the teams
  mysql_query($q);
  foreach($_POST['team'] as $value){
    $q="insert into team (team) values ('$value')";
mysql_query($q); //so by the end, the table will have records which were checked and all the records which were unchecked will be removed.
 }
}
//I list these teams in my form. 
$q="select id,team from table";
$res=mysql_query($q);
echo "<form method='post' action='check.php'>";
while($row=mysql_fetch_array($res)){
  echo "<input type='checkbox' name='team[]' value='$id' checked>".$team."<br />"; //this will list all the teams in the database
}
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";

i tried even diffrent ways, to delete,

if(isset($_POST['delete'])){
  }
 $id = $_GET['id'];
 (!isset($_GET['id'])) 
 foreach($_GET['team'] as $value)
{ 
  $team = $_REQUEST['team']; //'countries' is the name of the combobox
for ($i=0; $i<sizeof($team); $i++)
{
if (isset($team[$i]))
{ 
 $delete = "DELETE FROM team WHERE id='$id' ";
 mysql_query($delete);
 mysql_close();

Isnt there an easir way to delete a checkbox??

Isnt there an easir way to delete a checkbox??

Deleting the checkbox which is checked or deleting the checkbox which isn't checked ? I am not sure about the second case, but deleting records when checked works just like how you do while inserting.

For Now, all i want to do is delete a checkbox, in mysql

$delete = "DELETE FROM team WHERE id='$id' ";
 mysql_query($delete);
 mysql_close();

this code u looks good but has has errors.

form id="form1" name="form1" method="post" action="edit.php">

team: <input type="text" name="team" /><br />
<br>
<?php
$sql = 'SELECT * From team';
$con = mysql_connect("localhost","root","talk21") or die;
$result = mysql_db_query(lastr, $sql); 
while ($rec = mysql_fetch_array($result)) 
{
print "\n<input type='checkbox' name=team[]' value='".$rec['id']."'checked> ".$rec['team']." </input>"; 
}
if(isset($_POST['submit'])){
$q="delete from team"; //delete all the teams
mysql_query($q);
foreach($_POST['team'] as $value){
$q="insert into team (team) values ('$value')";
mysql_query($q); //so by the end, the table will have records which were checked and all the records which were unchecked will be removed.
}
}
//I list these teams in my form. 
$q="select id,team from table";
$res=mysql_query($q);
echo "<form method='post' action='check.php'>";
while($row=mysql_fetch_array($res)){
echo "<input type='checkbox' name='team[]' value='$id' checked>".$team."<br />"; //this will list all the teams in the database
}
echo "<input type='submit' name='submit' value='submit'>";
echo "</form>";

What error exactly ?
Dude, list all the teams. Let the user select the checkboxes and then click on delete. Go back to page 1 and check the example I have given. Do the same, but use delete query. And instead of passing the team name as value, pass the id.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.