Jump to content

*** SOLVED *** INSERT unknown variable sets


freakus_maximus

Recommended Posts

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 = on
Tdescr0 = dfgdfgdgdfg
Checkbox1 = on
Tdescr1 = dfgdfg[/color]
crsummary = summary here
Submit1 = 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...
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Similar but different naming convention

[code]<?php
if (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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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 Doe
cr_num = cr12345
date = 080106
Checkbox = Array
Tdescr = Array
crsummary = here is my description
Submit1 = 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?
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.