Jump to content

Recommended Posts

Hi all

I have a form which is built to allow me to enter multiple sets of information using four form controls per set.  Think of it like an invoice in which you put one item on each line, and each item has a name, description, price, etc.  I built a form which allows me to add more sets of these four controls using javascript so I can enter multiple items.  When I have completed adding my items, I then hit the submit button to add what I just key entered to my MySQL Database.  So, if I have two rows of data entered, the code below will add two rows to my MySQL table called 'LINE_ITEMS' and grab the data from each of the four controls per item using $myvar = $_POST['controlx'] where "x" is an appended number referencing the correct control, based on which row the loop is on.

Everything here works, the number of rows I've created get added to my table, but no data makes it into the MySQL table.  Note below how I am trying to reference each control in the loops (e.g. itemSel1, txtQuantity1, txtTTValue1, txtSellValue1, then if there is another control set, itemSel2, txtQuantity2, txtTTValue2, txtSellValue2, on and on based on the number of rows (items).)

$cArray = array('itemSel','txtQuantity','txtTTValue','txtSellValue'); //the base name of each control before they have their number appended
$val_array = array(); //empty array for number of rows
$strSQL_C = '';
$cVal = '';
$trans_id = $_GET['trans_id'];//Just a value i pass along from a previous page
//
// 1 array 4xN

$numLines = $_POST['numLines']; //hidden field in my form keeping track of number of rows for the main loop.

// 1st loop handles rows, 2nd loop handles columns to refer to each control name
for ($i = 0; $i <= $numLines - 1; $i++ )
{
  for($j = 0; $j <= 3; $j++ )
  {
    $iteration = $i + 1;
$cVal = $cArray[$j] . (string)$iteration; //Concatenate the name & numerical iteration value to refer to the right control.  this turns each control into it's base name PLUS the row number (e.g. itemSel becomes itemSel1, etc.)

*****HERE IS WHERE IT SEEMS TO FAIL FOR SOME REASON*********

$val_array[$i][$j] = $_POST['$cVal']; //Get the value for the control, for example,  control itemSel1, or itemSel2 and pull the data in that control.  It seems like $cVal is not being named correctly?  I also tried omitting the single quotes between the $_POST[ ] brackets but no difference.

*****END POSSIBLE FAILURE******

  }
// concatenate the values into one set for the SQL statement.
$tempStr = "('$id','$trans_id','$val_array[$i][0]','$val_array[$i][1]','$val_array[$i][2]','$val_array[$i][3]'),";   

//Append $tempStr to $strSQL_C.
$strSQL_C = $strSQL_C . $tempStr;
//**************************next, strip out the final comma from the $strSQL variable.*********************************
}
$strSQL = rtrim($strSQL_C, ",");
// Build a SQL statement with VALUES(fld1, fld2, fld3, fld4), (fld1(2), fld2(2), fld3(2), fld4(2)), etc.
// you end up with a 2-dimensional array that holds the values for each control for each row.  4 controls x N rows.
 
//Change to line_items table
$table = "LINE_ITEMS";
$sqlquery = "INSERT INTO $table VALUES" . $strSQL;
$results = mysql_query($sqlquery) or die("Sql error: " . mysql_error());

Again, I can get the correct number of rows added to my MySQL table, just no data is actually making it into the database.

Help & TIA!

F
[code]
$val_array[$i][$j] = $_POST['$cVal'];
[/code]

That means "the value in the array $_POST with the key '$cVal'", where '$cVal' is the [i]string[/i] '$cVal' (a dollar sign and four letters), not the variable $cVal.  Despite that you said you've tried taking them out with no effect, you don't want those single quotes in there if you mean to use the variable $cVal as a hash key for $_POST.

Have you checked the value of $cVal?  Have you used [url=http://www.php.net/manual/en/function.print-r.php]print_r()[/url] on both $_POST and $valArray?

Can I see your HTML code?  I have a sneaking suspicion that your PHP can be made significantly less complex through the judicious use of PHP array names as form element names.
Hi,

the print_r piece helped me sort it out.  I had problems mainly in my sql string - not pulling values in correctly and instead trying to insert the names of the variables.  The iterative code worked fine which was great, but I didn't know about print_r.  I have programming experience so php has been about learning the syntax as well as brushing up on coding efficiency.  I know it's a mess, but I'll worry about that in ver 2.  I just want it working for now.

So, my old sql statement when from "('$var1', '$var2', ...)" to the more explicit "('','" . $var1 . "','" . $var2 . "','" . $var3 ... )"

I should be able to insert the $var1, $var2 values into a string using {} brackets right?  e.g. "('{$var1}','{$var2}', .... )"

If you would like to see what I'm doing I will show you the html/javascript that goes along with it, just PM me.

Thanks for your help!

fbliss
[quote author=fbliss link=topic=99937.msg394208#msg394208 date=1152495055]
the print_r piece helped me sort it out.  I had problems mainly in my sql string - not pulling values in correctly and instead trying to insert the names of the variables.  The iterative code worked fine which was great, but I didn't know about print_r.  I have programming experience so php has been about learning the syntax as well as brushing up on coding efficiency.  I know it's a mess, but I'll worry about that in ver 2.  I just want it working for now.[/quote]

Glad it's working!  Yeah, PHP has a ton of functions.  Sometimes it's just hard finding them.

[quote author=fbliss link=topic=99937.msg394208#msg394208 date=1152495055]
So, my old sql statement when from "('$var1', '$var2', ...)" to the more explicit "('','" . $var1 . "','" . $var2 . "','" . $var3 ... )"

I should be able to insert the $var1, $var2 values into a string using {} brackets right?  e.g. "('{$var1}','{$var2}', .... )"[/quote]

Actually that's odd that it didn't work in the first format you had there.  Variables within double quoted strings should have been interpolated.  I've started using sprintf() for composing lengthy SQL queries that contain many variables.  I find they're alot more readable if I use whitespace, too.

I.e.,
[code]
$query = sprintf('INSERT INTO my_table VALUES(%s,'%s',%u)',
    isset ($_POST['thisValue']) ? "'$_POST[thisValue]'" : 'NULL',
    $anotherValue,
    $count
);
[/code]

As long as things are working, I guess you're set, then, but for when you get to version 2, the useful syntax I was thinking might help you is [url=http://www.php.net/manual/en/faq.html.php#faq.html.arrays]creating arrays from HTML forms[/url].  If you give your form elements PHP array-style names, PHP will interpret them as arrays when it reads them from the POST data.

Example:

[code]

(HTML)
...
<input type=checkbox name="var1[]" value="a" checked>
<input type=checkbox name="var1[]" value="b" checked>
...

(PHP)

<?php

print_r($_POST);

/*
You'd be able to traverse the var1 array like:
foreach ($_POST['var1'] as $key => $value) { ...
*/
?>

(output)

Array
(
    [var1] => Array
        (
            [0] => a
            [1] => b
        )

)

[/code]

That way you probably wouldn't need to set the hidden count variable in HTML (just check the array size), and you won't need to assemble strings to loop through the variables you've set in the form.

Hope this helps.
[code]
(HTML)
...
<input type=checkbox name="var1[]" value="a" checked>
<input type=checkbox name="var1[]" value="b" checked>
...

(PHP)

<?php

print_r($_POST);

/*
You'd be able to traverse the var1 array like:
foreach ($_POST['var1'] as $key => $value) { ...
*/
?>

(output)

Array
(
    [var1] => Array
        (
            [0] => a
            [1] => b
        )

)
[/code]

That is VERY cool, just like control arrays in good ole' VB.  That will certainly help cut down on server side code.

regarding why the '$var1' parsing not working, I can only guess it has to do with something I did incorrectly.  Being a noob sucks sometimes!

The form is doing everything correctly (albeit very inefficiently, I'm sure).  now to move onto some client-side validation... :P

Thanks again!

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