mx2maduro Posted December 23, 2009 Share Posted December 23, 2009 Ok, here's what I'm attempting to do. Create a website where we can document daily/weekly tasks with a simple check off type list. I've created a MySQL DB and I have a page where my users can setup their Login IDs and task list. My problem is with the code below, I have it to where it brings up the user's task list and places a checkbox by it. What I cannot get to work is checking to see if the checkbox has been checked, if it has then I want to update a table in the DB with the user's ID, the task by the checkbox and the current date. I have probably gotten this all backwards and would greatly appreciate any help. Thanks! <?php session_start(); if(isset($_POST['submit'])) { submit(); } //Function Update function submit() { //This is where I intend to place the code to update the MySQL table if (isset($_POST['chbx1'])){ print "Checked"; } } //End Function $con = mysql_connect("bmcsvrapache","root","brookwood"); //MySQL DB Username and Password if (!$con) { die('Could not connect: ' . mysql_error()); } print "<h3>Tasks for $myusername</h3>"; mysql_select_db("tasks", $con); //MySQL DB Name $result = mysql_query( "SELECT * FROM tasklist where username = '$myusername'" ); $num_rows = mysql_num_rows($result); $i=1; //set first checkbox number while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $chname="chbx$i"; print "<input type=checkbox name=$chname value=0>"; printf(" %s Task: %s", $row["type"], $row["task"]); print "<br\n>"; $i++; } ?> <html> <form action="<?= $PHP_SELF ?>" method="POST"> <input type="submit" name="submit" value="Submit"> </form> </html> <?php echo "<a href='login_success.php'>Return to Menu Page</a>"; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/ Share on other sites More sharing options...
piyush23424 Posted December 23, 2009 Share Posted December 23, 2009 Plz change the value of check box from 0 to 1.. instead of using isset function use if($_POST['chkBox'] == 1) condition Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983141 Share on other sites More sharing options...
mx2maduro Posted December 23, 2009 Author Share Posted December 23, 2009 Ok I tried that but for some reason it still not realizing that a checkbox is checked. Will the way I have assigned a checkbox to each row brought back from the DB work? LOL do you ever feel you just start over??? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983148 Share on other sites More sharing options...
piyush23424 Posted December 23, 2009 Share Posted December 23, 2009 you are printing checkboxes out of the form tags .........use the while loop inside the "<form>" tags .... it will work for sure,,,,, yes i am still a learner................ Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983154 Share on other sites More sharing options...
mx2maduro Posted December 23, 2009 Author Share Posted December 23, 2009 I really owe big time, that worked!!! I changed the code to this and it now sees if the checkbox is checked or not. Thank you very much! Now to see if I can get the code to update the table in...should be easy now. <html> <form action="<?= $PHP_SELF ?>" method="POST"> <?php $i=1; //set first checkbox number while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $chname="chbx$i"; print "<input type=checkbox name=$chname value=1>"; printf(" %s Task: %s", $row["type"], $row["task"]); print "<br\n>"; $i++; } ?> <input type="submit" name="submit" value="Submit"> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983158 Share on other sites More sharing options...
mx2maduro Posted December 23, 2009 Author Share Posted December 23, 2009 One more quick question if I could. I noticed that when I click the Submit button even though I'm posting back to the same page I loose the results that php returned from MySQL query when the page first loaded. Do I need to use the post function on the data returned in order to access it in my php function and if so what would be the syntax? Example of what I'm speaking of is the $num_rows, the print $num_rows right after the while statement returns the value but the print $num_rows in the function triggered by a checkbox being checked is empty. <?php session_start(); if(isset($_POST['submit'])) { submit(); } //Function Update function submit() { // Code to update MySQL table goes here for now the following is used for testing if ($_POST['chbx1']==1){ print "Checked"; print "Number of Rows: $num_rows"; } } $con = mysql_connect("bmcsvrapache","root","brookwood"); //MySQL DB Username and Password if (!$con) { die('Could not connect: ' . mysql_error()); } print "<h3>Tasks for $myusername</h3>"; mysql_select_db("tasks", $con); //MySQL DB Name $result = mysql_query( "SELECT * FROM tasklist where username = '$myusername'" ); $num_rows = mysql_num_rows($result); ?> <html> <form action="<?= $PHP_SELF ?>" method="POST"> <?php $i=1; //set first checkbox number while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $chname="chbx$i"; print "<input type=checkbox name=$chname value=1>"; printf(" %s Task: %s", $row["type"], $row["task"]); print "<br\n>"; $i++; } // Used for testing print "# of rows: $num_rows"; ?> <input type="submit" name="submit" value="Submit"> </form> </html> <?php echo "<br /><br />"; echo "<a href='login_success.php'>Return to Menu Page</a>"; mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983171 Share on other sites More sharing options...
Buddski Posted December 23, 2009 Share Posted December 23, 2009 Im not entirely sure what you mean by 'lose' the previous MySQL data, the code looks ok.. As for the checkbox thing.. The way you had it originally was correct. If a checkbox is NOT checked when the form is posted it will not be sent to PHP within the $_POST variables, therefor doing if($_POST['chbx1']==1){ will throw an undefined index error.. Although setting the value to 1 is more appropriate, the code doesnt care what the value is, as long as its got one and the box is ticked. Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983176 Share on other sites More sharing options...
mx2maduro Posted December 23, 2009 Author Share Posted December 23, 2009 I'm sorry, the way I explained maybe a bit confusing. What I'm trying to do link the checkboxes with the data in the rows. I was just going to use the variable $num_rows to setup a foreach loop to run through the checkboxes and if they were checked take the data on that row and send to a table called Completed. What I was finding after clicking on the submit button to check the status of the checkboxes was that the value in $num_rows was no longer there. Does that make more sense as to what I'm trying to do. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983188 Share on other sites More sharing options...
Buddski Posted December 23, 2009 Share Posted December 23, 2009 Presumably you are talking about this line inside your submit function print "Number of Rows: $num_rows"; yes? If so.. that is because the variable isnt defined until AFTER submit() is called.. so what you need to do is change the location of WHERE you call it. ie move the function submit() to after your initial query and also add 'global $num_rows; to the top of the submit function. This enables the function to read that variable.. $result = mysql_query( "SELECT * FROM tasklist where username = '$myusername'" ); $num_rows = mysql_num_rows($result); if (isset($_POST['submit'])) { submit(); } and function submit() { global $num_rows; // Code to update MySQL table goes here for now the following is used for testing if ($_POST['chbx1']==1){ print "Checked"; print "Number of Rows: $num_rows"; } } Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983203 Share on other sites More sharing options...
piyush23424 Posted December 23, 2009 Share Posted December 23, 2009 make following changes : while ($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $chname="chbx$i"; print "<input type=checkbox name=chk[] value=".$row['id'].">";// change no 1 printf(" %s Task: %s", $row["type"], $row["task"]); print "<br\n>"; $i++; } ---------------------- 2. replace the submit function with this & see if its working or not function submit() { //This is where I intend to place the code to update the MySQL table if (isset($_POST['chk'])){ print "Checked"; echo count($_POST['chk']); echo "<br/>"; $ids = implode(",",$_POST['chk'] ) mysql_query("UPDATE table set field='value' where id IN ($ids)"); } } //End Function You may get parse error with these modification ........please remove those and check the functionality Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983208 Share on other sites More sharing options...
Buddski Posted December 23, 2009 Share Posted December 23, 2009 Ignore what I said about $num_rows as its clearly irrelevant how many rows there are.. do what piyush23424 has suggested and count the number of boxes ticked.. Stupid 5am coding Quote Link to comment https://forums.phpfreaks.com/topic/186166-need-some-help-with-1st-attempt-using-php-and-mysql/#findComment-983222 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.