User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 373,374 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,776 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 4926 | Replies: 66
Reply
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #41  
Feb 15th, 2008
Originally Posted by amin007 View Post
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.
Last edited by nav33n : Feb 15th, 2008 at 1:25 pm.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #42  
Feb 18th, 2008
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
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #43  
Feb 18th, 2008
the query isn't returning any resultset. Check your query, execute it in phpmyadmin/mysql and see if it returns any value.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #44  
Feb 18th, 2008
Originally Posted by nav33n View Post
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['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);
}
?>



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['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
$con = mysql_connect("localhost","root","talk21") or die;
mysql_select_db("talk21");
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>
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #45  
Feb 18th, 2008
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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #46  
Feb 18th, 2008
Originally Posted by nav33n View Post
the query isn't returning any resultset. Check your query, execute it in phpmyadmin/mysql and see if it returns any value.

Originally Posted by nav33n View Post
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['id']."' > ".$rec['country']." </input>";
}
?>

thanx for the help.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #47  
Feb 18th, 2008
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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #48  
Feb 19th, 2008
Originally Posted by nav33n View Post
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['id']."' > ".$rec['Country']." </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.
Reply With Quote  
Join Date: Feb 2008
Posts: 37
Reputation: asadalim1 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

Re: inserting checkbox values in mysql +PHP

  #49  
Feb 20th, 2008
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.
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,026
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 227
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: inserting checkbox values in mysql +PHP

  #50  
Feb 20th, 2008
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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 2:57 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC