Jump to content


Photo

*** SOLVED *** INSERT unknown variable sets


  • Please log in to reply
10 replies to this topic

#1 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 23 August 2006 - 08:23 PM

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:
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

This is my query to insert the values:
$result = mysql_query ("INSERT INTO tasks (parent_cr, task_done, task_desc)
                VALUES ('$cr_num', '$Checkbox', '$Tdesc')");

Appreciate any help you can offer...

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 23 August 2006 - 09:48 PM

The easiest way is to have your form fields follow a specific naming convention:

Description: <input type="text" name="input[1][description]" size="25"/>
Checkbox: <input type="checkbox" name="input[1][checkbox]" value="somevalue" />

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.

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 August 2006 - 10:00 PM

Similar but different naming convention

<?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>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 23 August 2006 - 10:05 PM

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.

          //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);

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.

$result = mysql_query ("INSERT INTO tasks (parent_cr, task_done, task_desc)
                VALUES ('$cr_num', '$Checkbox', '$Tdesc')");

I'n thinking linear here, so if you have another method you think might work please let me know.

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 23 August 2006 - 10:08 PM

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.

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 23 August 2006 - 10:12 PM

You don't need a count. Use foreach() to loop throught the elements that are there, as in my code
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 25 August 2006 - 12:28 AM

Hey Barand, I used your code like this after I modified the form the way you described:

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')");
    }
}

Here is my output to the screen:
analyst = John Doe
cr_num = cr12345
date = 080106
Checkbox = Array
Tdescr = Array
crsummary = here is my description
Submit1 = Submit

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?

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 25 August 2006 - 12:32 AM

You need to execute the query after defining the $sql string

mysql_query($sql);
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 25 August 2006 - 12:41 AM

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... ???

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);
	}
}


#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,023 posts

Posted 25 August 2006 - 06:48 AM

do you get a meesage with
mysql_query($sql) or die(mysql_error());

Also, what does this give?
echo '<pre>', print_r($_POST, true), '</pre>';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 freakus_maximus

freakus_maximus
  • Members
  • PipPipPip
  • Advanced Member
  • 177 posts

Posted 25 August 2006 - 12:56 PM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users