freakus_maximus Posted August 23, 2006 Share Posted August 23, 2006 Hoping for some help here, I know how to do straightforward insert and update queries, but do not understand how to get an unknown set of variables to insert.In this case, I have a form that dynamically adds (incrementing id and name values) a checkbox and a textarea. Since I do not already know how many checkbox/textarea combinations are going to be added I cannot just hard code to expect those and update.Only the items I made red (below) are what go into this query. But how do I traverse through each set and ensure each checkbox and textarea get the same cr_num variable value. I'm sure I need a loop of some sort here, but not sure where to go...This is the output from the form as I dump all the variables:[code]Analyst = mynamehere[color=red]cr_num = cr12345[/color]date = 010101[color=red]Checkbox0 = onTdescr0 = dfgdfgdgdfgCheckbox1 = onTdescr1 = dfgdfg[/color]crsummary = summary hereSubmit1 = Submit[/code]This is my query to insert the values:[code]$result = mysql_query ("INSERT INTO tasks (parent_cr, task_done, task_desc) VALUES ('$cr_num', '$Checkbox', '$Tdesc')");[/code]Appreciate any help you can offer... Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 23, 2006 Share Posted August 23, 2006 The easiest way is to have your form fields follow a specific naming convention:[code]Description: <input type="text" name="input[1][description]" size="25"/>Checkbox: <input type="checkbox" name="input[1][checkbox]" value="somevalue" />[/code]Increment the number after the input (i.e. the next one would be input[2][description] etc.) for each set.Which will generate, on POST, a multidimensional array that you can then yeild all of your information from. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2006 Share Posted August 23, 2006 Similar but different naming convention[code]<?phpif (isset($_GET['submit'])) { $name = $_GET['name']; $cr_num = ${GET['cr_num']}; foreach ($_GET['descrip'] as $k => $descrip) { $cb = isset($_GET['cb'][$k]) ? 'on' : ''; $sql = "INSERT INTO tasks (parent_cr, task_done, task_desc) VALUES ('$cr_num', '$cb', '$descrip')" }}?><FORM>Name <input type="text" name="name" size="20"><br>CR Num <input type="text" name="cr_num" size="20"><br><input type="checkbox" name="cb[0]" value='on'> Descrip<textarea name="descrip[0]" rows="3" cols="40"></textarea><br><input type="checkbox" name="cb[1]" value='on'> Descrip<textarea name="descrip[1]" rows="3" cols="40"></textarea><br> <input type="checkbox" name="cb[2]" value='on'> Descrip<textarea name="descrip[2]" rows="3" cols="40"></textarea><br><input type="checkbox" name="cb[3]" value='on'> Descrip<textarea name="descrip[3]" rows="3" cols="40"></textarea><br><input type="submit" name="submit" value="Submit"></FORM>[/code] Quote Link to comment Share on other sites More sharing options...
freakus_maximus Posted August 23, 2006 Author Share Posted August 23, 2006 Thanks for the info Hitman!Well, I think I have that already. I have a javascript that adds the new checkbox/textarea when "add task" is clicked and each set's id is incremented by 1.[code] //checkbox var cell = row.insertCell(0); var checkboxfield = document.createElement("input"); checkboxfield.setAttribute("type", "checkbox"); checkboxfield.setAttribute("id", "Checkbox" + (numrows+i)); checkboxfield.setAttribute("name", "Checkbox" + (numrows+i)); //checkboxfield.setAttribute("value", "Checked"); cell.appendChild(checkboxfield); cell.setAttribute('align','center'); // task description cell = row.insertCell(1); var tdescfield = document.createElement("textarea"); tdescfield.setAttribute("cols", "60"); tdescfield.setAttribute("rows", "3"); tdescfield.setAttribute("id", "Tdescr" + (numrows+i)); tdescfield.setAttribute("name", "Tdescr" + (numrows+i)); cell.appendChild(tdescfield);[/code]Which all works fine and adds multiple rows which I can then echo on a dummy page to check and I can see all is good.I'm thinking it's not so much my building the array but the sql query which needs to loop through and do a new INSERT for each checkbox/textarea set along with the cr_num (which is a reference for which tasks they belong to). The other variables in the array are going to be used on the parent table which I can do just fine.How can I get the query to loop the correct number of times (depends on how many checkbox/textarea sets there are) and using each set of variables ($cr_num, $Checkbox0, $Tdesc0 and then move on to $cr_num, $Checkbox1, $Tdesc1). The cr_num would be the same for the post.[code]$result = mysql_query ("INSERT INTO tasks (parent_cr, task_done, task_desc) VALUES ('$cr_num', '$Checkbox', '$Tdesc')");[/code]I'n thinking linear here, so if you have another method you think might work please let me know. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 23, 2006 Share Posted August 23, 2006 If you use my code, do a count on $_POST['input'], then a for loop, or do a foreach loop on the same array.If you use Barand's code, do a count on either $_POST['descrip'] or $_POST['cb'], then do a for loop to get the matching elements from each. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2006 Share Posted August 23, 2006 You don't need a count. Use foreach() to loop throught the elements that are there, as in my code Quote Link to comment Share on other sites More sharing options...
freakus_maximus Posted August 25, 2006 Author Share Posted August 25, 2006 Hey Barand, I used your code like this after I modified the form the way you described:[code]if (isset($_POST['submit'])) { $name = $_POST['analyst']; $cr_num = $_POST['cr_num']; foreach ($_POST['Tdescr'] as $k => $descrip) { $cb = isset($_POST['Checkbox'][$k]) ? 'on' : ''; $sql = ("INSERT INTO tasks (parent_cr, task_done, task_desc) VALUES ('$cr_num', '$cb', '$descrip')"); }}[/code]Here is my output to the screen:[code]analyst = John Doecr_num = cr12345date = 080106Checkbox = ArrayTdescr = Arraycrsummary = here is my descriptionSubmit1 = Submit[/code]It does not perform the INSERT though to the db. I know that a basic INSERT with one checkbox/textarea works with straightforward code doing the INSERT, so I know it's not a db or table issue.Any ideas where this is going wrong? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 25, 2006 Share Posted August 25, 2006 You need to execute the query after defining the $sql stringmysql_query($sql); Quote Link to comment Share on other sites More sharing options...
freakus_maximus Posted August 25, 2006 Author Share Posted August 25, 2006 duh...sorry long day at work...but it still is not performing the query...(I did correct the table name to 'task" tho)I have double and tripled check the field names and I can see the output but the insert never happens... ???[code]if (isset($_POST['submit'])) { $name = $_POST['analyst']; $cr_num = $_POST['cr_num']; foreach ($_POST['Tdescr'] as $k => $descrip) { $cb = isset($_POST['Checkbox'][$k]) ? 'on' : ''; $sql = ("INSERT INTO task (parent_cr, task_done, task_desc) VALUES ('$cr_num', '$cb', '$descrip')"); mysql_query($sql); }}[/code] Quote Link to comment Share on other sites More sharing options...
Barand Posted August 25, 2006 Share Posted August 25, 2006 do you get a meesage with[code]mysql_query($sql) or die(mysql_error());[/code]Also, what does this give?[code]echo '<pre>', print_r($_POST, true), '</pre>';[/code] Quote Link to comment Share on other sites More sharing options...
freakus_maximus Posted August 25, 2006 Author Share Posted August 25, 2006 I got it figured out by checking if it was even moving inside the "if (isset($_POST['submit'])) {" which it was not. I had 2 typos and they were both with my form actually. It was passing the variable name as "Submit1" not "submit".Barand, thanks for your help! Quote Link to comment 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.