...

View Full Version : Inserting selected records using checkbox Getting ERROR MSG?



DataTalk
12-14-2010, 01:28 PM
Im Looking for a simple method of Inserting selected records
into database using checkbox

This is not a working example, but I hope it explains what I am trying to do.
Not sure how to define If (checked = true) then do post all selected to db



<form name="PostSelected" method="post">

Row1: <input type="checkbox" name="id[0]" value="" >
<input type="text" name="id[0]['FirstName']" value="Bob" >
<input type="text" name="id[0]['LastName']" value="Jones"><br>

Row2: <input type="checkbox" name="id[1]" value="" >
<input type="text" name="id[1]['FirstName']" value="John" >
<input type="text" name="id[1]['LastName']" value="Doe"><br>

Row3: <input type="checkbox" name="id[2]" value="" >
<input type="text" name="id[2]['FirstName']" value="Sandy" >
<input type="text" name="id[2]['LastName']" value="Beach"><br>

<input name="submit" type="submit"><br>
</form>

<?php
// connect to db
require_once("includes/connect.php");

// find selected rows
if($_POST[checkbox] == 'true') {

// loop through selected rows
foreach($_POST[id] as $post_key ){

// post selected rows to db
mysql_query("INSERT INTO businesses (FirstName, LastName) VALUES ('$post_key[FirstName]','$post_key[LastName]')")

or die("No Records Selected");
}
}
?>

mlseim
12-14-2010, 04:11 PM
Are you generating your form dynamically using PHP?
You show 3 names (or rows), but could there be any number of them?

The names you are giving your text and checkboxes is very odd.
I wonder if you can elaborate on what it is supposed to do (the purpose of your script).

Sort of have a hunch you're approaching this from the wrong direction.

DataTalk
12-14-2010, 06:01 PM
Maybe this will better define my task

1. I HAVE A LIST OF RECORDS WITH A CHECKBOX NEXT TO EACH RECORD TO SELECT FROM
2. I WANT TO SELECT SOME OF THOSE RECORDS USING THE CHECKBOX
3. AND INSERT THE SELECTED RECORDS INTO A NEW TABLE CALLED "TEST"

Please follow my // notes through the script to see where I need help



<div align='left'>
<form name"PostRecords" method="post">
<table border='1' bordercolor='#COCOCO' cellpadding='0'>
<tr>
<td width="200"><p style='margin: 1'>SELECT A RECORD</td>
<td width="50" ><p style='margin: 1'>id</td>
<td width="200"><p style='margin: 1'>CostCode</td>
<td width="300"><p style='margin: 1'>CostItem</td>
</tr>
<?php
require_once("includes/connect.php");

// First Lets List The Records for our selection

$sql= "SELECT * FROM costitems ORDER BY CostCode";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td><p style='margin: 1'><input type='checkbox' name='CB1' value=''>Select</td>
<td><p style='margin: 1'>$row[id]</td>
<td><p style='margin: 1'>$row[CostCode]</td>
<td><p style='margin: 1'>$row[CostItem]</td>
</tr>";
}
?>
</form>
</table>
</div>
<!-- 1. ABOVE - WE NOW HAVE A LIST OF RECORDS TO SELECT FROM -->
<!-- 2. NOW LETS SELECT SOME RECORDS FROM THE LIST ABOVE USING THE CHECKBOX -->
<!-- 3. NOW LETS INSERT THOSE RECORDS INTO A NEW TABLE CALLED "TEST" -->

<?php
// This is where I need Help
// IDENIFY THE ABOVE SELECTED RECORDS WITH IF STATEMENT

if($_POST[checkbox] == 'true') { // need the correct code here

// define the proper query to insert each record the database as a new record
foreach($_POST[id] as $post_key ){
mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$post_key[CostCode]','$post_key[CostItem]')")

or die("No Records Selected");
}
echo "All the selected records have been inserted in TEST";
}
?>

DataTalk
12-14-2010, 06:54 PM
Are you generating your form dynamically using PHP?
You show 3 names (or rows), but could there be any number of them?

The names you are giving your text and checkboxes is very odd.
I wonder if you can elaborate on what it is supposed to do (the purpose of your script).

Sort of have a hunch you're approaching this from the wrong direction.

I was trying to layout a simple apptoach, I revised my code to reflect exactly what I am trying to do

1. I have a list of records from db table with a checkbox next to each record
2. I want to select some of those records and insert into a new table
3. Im not sure how to identify each record that was selected for the insert query loop??

mlseim
12-14-2010, 07:02 PM
Checkbox arrays are somewhat different than text boxes.

With a checkbox array, you'll only see the checkboxes that were "checked".
Example, if you have 100 lines in your form, and the user checks 5 of them,
the checkbox array will only have 5 elements in it, numbered 0,1,2,3,4.

Yet, with text boxes and hidden tags, you'll see every one of them in the array.

So, we need to give the checkbox a value of it's position in the array.
That position will point to the correct location for the text box or hidden tag value.

My test script ... but this is untested. I'm not sure what will happen ...


<div align='left'>
<form name"PostRecords" method="post">
<table border='1' bordercolor='#COCOCO' cellpadding='0'>
<tr>
<td width="200"><p style='margin: 1'>SELECT A RECORD</td>
<td width="50" ><p style='margin: 1'>id</td>
<td width="200"><p style='margin: 1'>CostCode</td>
<td width="300"><p style='margin: 1'>CostItem</td>
</tr>
<?php
require_once("includes/connect.php");

// First Lets List The Records for our selection

$line=0;
$sql= "SELECT * FROM costitems ORDER BY CostCode";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td><p style='margin: 1'><input type='checkbox' name='cb[]' value='$line'>Select</td>
<input type='hidden' name='rid[]' value='$row[id]'>
<input type='hidden' name='cc[]' value='$row[CostCode]'>
<input type='hidden' name='ci[]' value='$row[CostItem]'>
<td><p style='margin: 1'>$row[id]</td>
<td><p style='margin: 1'>$row[CostCode]</td>
<td><p style='margin: 1'>$row[CostItem]</td>
</tr>";
$line++;
}
?>
</form>
</table>
</div>
<!-- 1. ABOVE - WE NOW HAVE A LIST OF RECORDS TO SELECT FROM -->
<!-- 2. NOW LETS SELECT SOME RECORDS FROM THE LIST ABOVE USING THE CHECKBOX -->
<!-- 3. NOW LETS INSERT THOSE RECORDS INTO A NEW TABLE CALLED "TEST" -->

<?php
// by looping through the checkboxes, you will only see the ones that were checked.
// the value of those checkboxes will be associated with the proper line from your form.
// i'm not sure what you want to do with $row[id]? Perhaps you don't need it, your new table will assign new ID?

// define the proper query to insert each record the database as a new record
foreach($_POST['cb'] as $line_no ){
mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$_POST[cc][$line_no]','$_POST[ci][$line_no]')")

or die("No Records Selected");
}

echo "All the selected records have been inserted in TEST";
?>

DataTalk
12-14-2010, 07:41 PM
REVISED CODE!

Were close, here is what hit the database selecting 5 random records
0 Array[0] Array[0] 0 NULL NULL NULL
0 Array[1] Array[1] 0 NULL NULL NULL
0 Array[2] Array[2] 0 NULL NULL NULL
0 Array[3] Array[3] 0 NULL NULL NULL
0 Array[4] Array[4] 0 NULL NULL NULL


<div align='left'>
<form method="post">
<p><input type="submit" value="Submit"></p>
<table border='1' bordercolor='#COCOCO' cellpadding='0'>
<tr>
<td width="200"><p style='margin: 1'>SELECT A RECORD</td>
<td width="50" ><p style='margin: 1'>id</td>
<td width="200"><p style='margin: 1'>CostCode</td>
<td width="300"><p style='margin: 1'>CostItem</td>
</tr>
<?php
require_once("includes/connect.php");

$line=0;
$sql= "SELECT * FROM costitems ORDER BY CostCode";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td><input type='checkbox' name='F1[]' value='$line'></td>
<td><input type='text' name='F2[]' value='$row[id]'></td>
<td><input type='text' name='F3[]' value='$row[CostCode]'></td>
<td><input type='text' name='F4[]' value='$row[CostItem]'></td>
</tr>";
$line++;
}
?>
</form>
</table>
</div>

<?php
foreach($_POST['F1'] as $line_no ){
mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$_POST[F3][$line_no]','$_POST[F4][$line_no]')") ;
}
?>

DataTalk
12-14-2010, 08:22 PM
REVISED CODE!

Were close, here is what hit the database selecting 5 random records
0 Array[0] Array[0] 0 NULL NULL NULL
0 Array[1] Array[1] 0 NULL NULL NULL
0 Array[2] Array[2] 0 NULL NULL NULL
0 Array[3] Array[3] 0 NULL NULL NULL
0 Array[4] Array[4] 0 NULL NULL NULL


<div align='left'>
<form method="post">
<p><input type="submit" value="Submit"></p>
<table border='1' bordercolor='#COCOCO' cellpadding='0'>
<tr>
<td width="200"><p style='margin: 1'>SELECT A RECORD</td>
<td width="50" ><p style='margin: 1'>id</td>
<td width="200"><p style='margin: 1'>CostCode</td>
<td width="300"><p style='margin: 1'>CostItem</td>
</tr>
<?php
require_once("includes/connect.php");

$line=0;
$sql= "SELECT * FROM costitems ORDER BY CostCode";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td><input type='checkbox' name='F1[]' value='$line'></td>
<td><input type='text' name='F2[]' value='$row[id]'></td>
<td><input type='text' name='F3[]' value='$row[CostCode]'></td>
<td><input type='text' name='F4[]' value='$row[CostItem]'></td>
</tr>";
$line++;
}
?>
</form>
</table>
</div>

<?php
foreach($_POST['F1'] as $line_no ){
mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$_POST[F3][$line_no]','$_POST[F4][$line_no]')") ;
}
?>

PS: The list has about 500 records, loads very slow....

mlseim
12-14-2010, 08:53 PM
The slow loading is because you are using <table>.
I'll bet you're using IE too.

IE won't display anything until the <table> is complete.

Eliminate <table> and use CSS instead, or try it on FireFox .... it will load quicker.


===============

My bad! I'm referencing these wrong ... $_POST[F3][$line_no]

I think we need to make arrays out of them first ...

My revised code:


<div align='left'>
<form method="post">
<p><input type="submit" value="Submit"></p>
<table border='1' bordercolor='#COCOCO' cellpadding='0'>
<tr>
<td width="200"><p style='margin: 1'>SELECT A RECORD</td>
<td width="50" ><p style='margin: 1'>id</td>
<td width="200"><p style='margin: 1'>CostCode</td>
<td width="300"><p style='margin: 1'>CostItem</td>
</tr>
<?php
require_once("includes/connect.php");

$line=0;
$sql= "SELECT * FROM costitems ORDER BY CostCode";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td><input type='checkbox' name='F1[]' value='$line'></td>
<td><input type='text' name='F2[]' value='$row[id]'></td>
<td><input type='text' name='F3[]' value='$row[CostCode]'></td>
<td><input type='text' name='F4[]' value='$row[CostItem]'></td>
</tr>";
$line++;
}
?>
</form>
</table>
</div>

<?php
$ccode=$_POST['F3'];
$citem=$_POST['F4'];
foreach($_POST['F1'] as $line_no ){
mysql_query("INSERT INTO TEST (CostCode, CostItem) VALUES ('$ccode[$line_no]','$citem[$line_no]')") ;
}

DataTalk
12-14-2010, 09:37 PM
Thank you very much for your help! here is an update..

I'm getting an error msg;
Warning: Invalid argument supplied for foreach()

I added a quick echo "results" to see what all is posting
The script is working properly, the correct data is posted clean to both..
Database and echo out. Can't find the bug causing the error


Could it be $_POST['F3']; vs $_POST[F3] i get the error either way
something in: foreach($_POST['F1'] as $line_no ){


PS: Also, I fixed the speed issue by using hidden fields and not displaying
the data in the textbox in the selection list - see the revised table format.



<div align='left'>
<form method="post" target='_self'>
<p><input type="submit" value="Submit"></p>
<table border='1' bordercolor='#COCOCO' cellpadding='0'>
<tr>
<td width="200"><p style='margin: 1'>SELECT A RECORD</td>
<td width="50" ><p style='margin: 1'>id</td>
<td width="200"><p style='margin: 1'>CostCode</td>
<td width="300"><p style='margin: 1'>CostItem</td>
</tr>
<?php
require_once("includes/connect.php");

$line=0;
$sql= "SELECT * FROM costitems WHERE CostId = '10' ORDER BY CostCode";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)) {
echo "<tr>
<td>
<input type='checkbox' name='F1[]' value='$line'>
<input type='hidden' name='F2[]' value='$row[id]'>
<input type='hidden' name='F3[]' value='$row[CostCode]'>
<input type='hidden' name='F4[]' value='$row[CostItem]'>
</td>
<td>$row[id] </td>
<td>$row[CostCode]</td>
<td>$row[CostItem]</td>
</tr>";
$line++;
}
?>
</form>
</table>
</div>
<?php
$username = "test";
$costcode = $_POST['F3'];
$costitem = $_POST['F4'];
foreach($_POST['F1'] as $line_no ){
mysql_query("INSERT INTO test (CostCode, CostItem, UserName) VALUES ('$costcode[$line_no]','$costitem[$line_no]','$username')") ;
}

if ($_POST) {
$user = $_POST[user];
$sql2= "SELECT * FROM test WHERE UserName = 'test' ORDER BY CostCode";
$result2 = mysql_query($sql2);
while($row2 = mysql_fetch_assoc($result2)) {
echo "TEST DATAENTRY RESULTS: $row2[CostCode], $row2[CostItem]<br>";
}}
?>

DataTalk
12-14-2010, 11:06 PM
Need help resolving the error mesage in the above example:)

mlseim
12-15-2010, 12:01 AM
hmmm .... try this:



<?php
$username = "test";
$costcode = $_POST['F3'];
$costitem = $_POST['F4'];
$chkbox = $_POST['F1'];
foreach($chkbox as $line_no ){
mysql_query("INSERT INTO test (CostCode, CostItem, UserName) VALUES ('$costcode[$line_no]','$costitem[$line_no]','$username')") ;
}

if ($_POST) {
$user = $_POST[user];
$sql2= "SELECT * FROM test WHERE UserName = 'test' ORDER BY CostCode";
$result2 = mysql_query($sql2);
while($row2 = mysql_fetch_assoc($result2)) {
echo "TEST DATAENTRY RESULTS: $row2[CostCode], $row2[CostItem]<br>";
}}
?>

DataTalk
12-15-2010, 12:33 AM
Sorry! no-go-bro;

Still get the error??

mlseim
12-15-2010, 03:44 AM
I'm stumped.

DataTalk
12-15-2010, 09:23 PM
I posted a working example of this script

http://quickweboffice.com/weblink.php?id=22

The script works great! how do we solve the for each error msg?
worst case - how could we write a custom error msg for the foreach
function and leave it blank

mlseim
12-15-2010, 09:38 PM
doh!

That's a warning, not a fatal failure ... why didn't I see that before?

change this:

foreach($chkbox as $line_no ){

to this:

@foreach($chkbox as $line_no ){

See if that stops the warning.
Otherwise, put this at the top of your script:

<?php
error_reporting (E_ALL ^ E_NOTICE);

DataTalk
12-15-2010, 11:03 PM
When adding error_reporting (E_ALL ^ E_NOTICE);
no change: still getting error in all browsers

When using @foreach it produces this error
Parse error: syntax error, unexpected T_FOREACH

NOTE: Does not make sense because after selecting the first post the warning goes away

DataTalk
12-16-2010, 03:25 AM
doh!

That's a warning, not a fatal failure ... why didn't I see that before?

change this:

foreach($chkbox as $line_no ){

to this:

@foreach($chkbox as $line_no ){

See if that stops the warning.
Otherwise, put this at the top of your script:

<?php
error_reporting (E_ALL ^ E_NOTICE);

I already tried this, no success...

Notice that After posting the first query, the page reloads the error message does not display again.
I have the code posted with this example for testing

http://quickweboffice.com/weblink.php?id=22

mlseim
12-16-2010, 04:10 AM
Do you have a submit button on your form?
I can't remember if you did or not.

How about this ...
Don't process the loop unless the submit button is seen ...



<?php
if($_POST['submit']=="Submit"){
$username = "test";
$costcode = $_POST['F3'];
$costitem = $_POST['F4'];
foreach($_POST['F1'] as $line_no ){
mysql_query("INSERT INTO test (CostCode, CostItem, UserName) VALUES ('$costcode[$line_no]','$costitem[$line_no]','$username')") ;
}

if ($_POST) {
$user = $_POST[user];
$sql2= "SELECT * FROM test WHERE UserName = 'test' ORDER BY CostCode";
$result2 = mysql_query($sql2);
while($row2 = mysql_fetch_assoc($result2)) {
echo "TEST DATAENTRY RESULTS: $row2[CostCode], $row2[CostItem]<br>";
}}

}
?>

DataTalk
12-16-2010, 04:18 AM
Thanks bro - We were running the forech loop before its time

if ($_POST) {
foreach($chkbox as $line_no ){

this example is now producing the correct results

http://quickweboffice.com/weblink.php?id=22



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum