mx2maduro Posted January 4, 2010 Share Posted January 4, 2010 What I'm attempting to do is read a MySQL table and display the task's assigned to a User ID and place a checkbox at the start of each row. That part appears to be working ok. My problem is accessing the data that I'm storing in the arrays in my Submit Function. I've Googled PHP Arrays and tried several different methods but I'm just not getting something right. What my Function should do is if the checkbox is checked take that row of data and create a new record in a table called "completed". If anyone could shed some light on this for me I would greatly appreciate it. Thanks <?php session_start(); $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); if(isset($_POST['submit'])) { submit(); } ?> <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"; $taskarray[$i]=$row['task']; $typearray[$i]=$row['type']; print "<input type=checkbox name=$chname value=1>"; printf(" %s Task: %s", $row["type"], $row["task"]); print "<br\n>"; $i++; } print "# of rows: $num_rows<br>\n"; print "Test: $taskarray[1]<br>\n"; print "Type: $typearray[1]"; ?> <input type="submit" name="submit" value="Submit"> </form> </html> <?php function submit() { global $num_rows; global $result; global $taskarray; global $typearray; $querySQL = "insert into completed (username, task, type, date) values ($myusername, task, type, NOW())"; $counter = 1 while ($counter <= $num_rows){ if ($_POST['chbx$counter']==1){ $querySQL = "insert into completed (username, task, type, date) values ($myusername, $taskarray[$counter], typearray[$counter], NOW())"; } } } //echo "$myusername <br />"; //echo "Your task has been successfully added to the database."; 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/187168-can-you-help-a-beginner-with-php-arrays/ Share on other sites More sharing options...
KevinM1 Posted January 4, 2010 Share Posted January 4, 2010 First, you really shouldn't display your database password here. Making it visible on a public forum is a very large security risk for your system. And, second, you should place all code within BBCode code tags. Anyhoo, there's a lot going on here that's messed up. I have the feeling that you're not 100% sure about how the postback cycle should work, so here's a crash course. Right now, you have several magic variables floating around. What is $myusername, and where does it come from? Have the arrays come from some other script, or are you initializing and building them here? Regardless, a typical postback script looks like: <?php if (isset($_POST['submit')) // if the form has been submitted, process it { // form processing code goes here } ?> <!-- regardless, show the form --> <html> . . . <form ... > . . . </form> . . . </html> All form processing needs to happen first. Right now, you're trying to deal with your arrays in the submit() function, but they haven't been created yet, as you don't handle them until you decide to display your form. Just because you put the function definition after the form code doesn't mean that the function is actually invoked there. Finally, don't use global to pass in values to a function. It's very bad practice, can lead to a host of errors and debugging issues, and isn't necessary as functions have an argument list for a reason. Hope this nudges you in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/187168-can-you-help-a-beginner-with-php-arrays/#findComment-988410 Share on other sites More sharing options...
mx2maduro Posted January 4, 2010 Author Share Posted January 4, 2010 You are correct...this is my first attempt to do a simple check off type task list. Sorry about all the mistakes but like I said its a first attempt. The password didn't concern me as this is just a simple task list, just needed the help to figure out what I was doing wrong and why I couldn't get the data out of the array. $myusername comes from my login page, I have the user login and then I start a session. As to the arrays I'm building them as I read the table of tasks for the user that is logged in. Could someone maybe tell me which way I should go to accomplish this task or can it even be done with PHP & MySQL? I want to read the table "tasks" and bring back all the tasks for that user (this seems to be working). Then display the tasks on the screen and assign a checkbox to each task, then if the user checks it off, when they click submit have the tasks that are checked added to a table of completed tasks. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187168-can-you-help-a-beginner-with-php-arrays/#findComment-988427 Share on other sites More sharing options...
KevinM1 Posted January 5, 2010 Share Posted January 5, 2010 You are correct...this is my first attempt to do a simple check off type task list. Sorry about all the mistakes but like I said its a first attempt. The password didn't concern me as this is just a simple task list, just needed the help to figure out what I was doing wrong and why I couldn't get the data out of the array. $myusername comes from my login page, I have the user login and then I start a session. As to the arrays I'm building them as I read the table of tasks for the user that is logged in. Could someone maybe tell me which way I should go to accomplish this task or can it even be done with PHP & MySQL? I want to read the table "tasks" and bring back all the tasks for that user (this seems to be working). Then display the tasks on the screen and assign a checkbox to each task, then if the user checks it off, when they click submit have the tasks that are checked added to a table of completed tasks. Thanks So, you're trying to pass $myusername to this page via the session? For that to work, you need to assign the value to a session variable in your login page, like so: session_start(); // ALWAYS need session_start on the pages you try to use sessions on /* login code */ $_SESSION['username'] = $whateverVariableContainsTheUsername; Then, in this page, to retrieve the value, you need: session_start(); $myusername = $_SESSION['username']; For the rest, like I said before, it's a matter of timing - you're currently fighting against how the HTTP request cycle actually works. In order for your submit() function to work, your arrays need to persist between page refreshes. I think a sensible solution would be to store them in session variables as well, then check for their existence on page reload. If they exist, invoke submit(). Also, something else I noticed - submit() doesn't actually execute any queries. Remember: all MySQL queries need to be passed into mysql_query() to actually be run. Right now, you're merely creating queries in your loop, then overwriting them. EDIT: So, yes, what you want to do can be done. It's just a matter of designing it properly. Quote Link to comment https://forums.phpfreaks.com/topic/187168-can-you-help-a-beginner-with-php-arrays/#findComment-988799 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.