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 397,174 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,450 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

inserting checkbox values in mysql +PHP

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  
All times are GMT -4. The time now is 4:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC