Forum Moderators: coopster

Message Too Old, No Replies

Form Submission Problems

form select and PHP MySQl database

         

c2web

1:23 pm on Oct 8, 2004 (gmt 0)

10+ Year Member



Hi,

I have a form -> php mysql database problem that is causing me headaches.

Basically the difficulty I have is trying to capture details from a form submission in to a MySQL database through PHP - sounds simple enough eh... here's the problem:

I have a first page which goes to a table within my database and retrieves 50 photographs. The number of photos can change, so these are not set within HTML, but one PHP entry to the page which echos out a copy of each of the different photographs that appears in the database.

Under each photo is a drop down box (A form Select tag) which gives you two options.

The Select tag on the first page looks like this:

<select name="Size[]" size="1" id="Size[]">
<option value="">Please Select</option>
<option value="(<? echo $row["ref"]?>)-6x4">6x4'</option>
<option value="(<? echo $row["ref"]?>)-10x8'">10x8'</option>
</select>

Now the visitor can select the 'size' of the photograph for 1 or more of the photographs, up to the 50 displayed. The purpose of this is so I can see which photographs they are selecting and which size they require. As you will see from the coding above the option value includes the photos ID ('ref') and then the size.

What I need is, only the photos they select, to be entered in to a MySQL database.

Once they select the 'sizes' they require for the photos they want they then click submit.

The form is processed by a PHP script and, am having to seperate the different entries for the same form variable array and is shown below:

$size_array = isSet ($_POST['Size'])? $_POST['Size'] : array();
$cnt = count($size_array);
for ($i =0; $i < $cnt; $i++)

The next line of the script inserts to the table:

$query = mysql_query("insert into cart(ref, id, size) values('','" . session_id() . "', '$size_array[$i]')");

This all works fine for one small problem - and the cause of my lack of sleep and headaches - and is when the user clicks on submit (from the first page) all 50 photograph entires are submitted to the table but the Size field is left blank for any photos that are not selected. So for example if on the first page I changed the value in the drop down menu from 'Please Select' to either '6x4' or '10x8' for photographs 2 and 4 - the table looks like:

Ref ID Size
1 1A234
2 1A234 1-6x4
3 1A234
4 1A234 4-10x8
5 1A234
6 1A234
..
50 1A234

Is there a way I can only enter in to the database just the photos the user has selected as opposed to every item?

I do hope this makes sense with the information I have given but if it helps I ca provide more details or more extractions from the scripts.....

Many thanks,
C2

StupidScript

5:26 pm on Oct 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did this test, which may be useful to you:
====snipsnip====
<?
if ($didit) {
$size_array=isSet ($_POST['Size'])? $_POST['Size']:array();
$cnt=count($size_array);
for ($i=0;$i<$cnt;$i++) {
if ($size_array[$i]) {
echo "ArrayEntry ".$i.": ".$size_array[$i]."<br>\n";
}
}
}
?>
<form method="post" action="<?=$PHP_SELF?>">
<input type="hidden" name="didit" value=1>
<table border=0 cellpadding=10 cellspacing=0>
<tr>
<td>Item 1<br><select name="Size[]"><option value=0>Select<option value="Item1-1x2">1x2<option value="Item1-2x4">2x4<option value="Item1-3x5">3x5</select></td>
<td>Item 2<br><select name="Size[]"><option value=0>Select<option value="Item2-1x2">1x2<option value="Item2-2x4">2x4<option value="Item2-3x5">3x5</select></td>
<td>Item 3<br><select name="Size[]"><option value=0>Select<option value="Item3-1x2">1x2<option value="Item3-2x4">2x4<option value="Item3-3x5">3x5</select></td>
</tr>
<tr><td colspan=3 align="center"><input type="submit"></td></tr>
</table>
</form>
====snipsnip====

Note how the default dropdown entry has a value of 0, and how in the loop that pulls the array values for insertion, I eliminated the entries with a 0 value, leaving only the entries where a selection had been made.

(I did not use the "id" attribute, as it is intended to be a unique identifier, unlike "name".)

Hope it's handy!
(Welcome to the forums ... :)

c2web

6:42 pm on Oct 8, 2004 (gmt 0)

10+ Year Member



Many many thanks!

What a fantastic idea - I didn't think to label the default Select tag to 0 and then strip out anything with a value of > 1 - superb! Works great....

I have just one last (hopefully small problem) and this would be the solution to the end of my problems and that is:

Each photograph does have a the 'Select' tags within the form for the user to select the size, but what I am also trying to do is for them to click a 'Checkbox' first and then select the size.

The form script snippet is like this:

<input type="checkbox" name="photograph[]" value="<?php echo $row["image"];?>">
<br>
<input type="hidden" name="didit" value=1>
<select name="Size[]" size="1" id="Size[]">
<option value="0">Select</option>
<option value="(<? echo $row["ref"]?>)-6x4">6x4'</option>
<option value="(<? echo $row["ref"]?>)-10x8'">10x8'</option>
</select>

This then submits the entries the user has selected with the checkbox and also the size of the photo.

This now passes to this PHP snippet:

$photograph_array = isSet ($_POST['photograph'])? $_POST['photograph'] : array();
$cnt = count($photograph_array);
for ($i = 0; $i < $cnt; $i++)

if ($didit) {
$size_array = isSet ($_POST['Size'])? $_POST['Size'] : array();
$cnt2 = count($size_array);
for ($i2 =0; $i2 < $cnt2; $i2++) {
if ($size_array[$i2]){


$query = mysql_query("insert into cart(ref, id, photo, size) values('','" . session_id() . "', '$photograph_array[$i]', '$size_array[$i2]')");
}
}
}

Now, if for example, I selected the tick boxes for photos 2 and 4, and then corresponding sizes of 6x4 (for photo 2) and 10x8 (for photo4) I get the following input to the MySQL database:

Ref ID Photo Size
1 123A4 2 6x4
2 123A4 4 6x4
3 123A4 2 10x8
4 123A4 4 10x8

when I really want:

Ref ID Photo Size
1 123A4 2 6x4
2 123A4 4 10x8

I understand the script is not recognising that the photo number relates to the size and so is therefore assigning all selected photo numbers to all photo sizes - but is there a way of obtaining the above?

Many thanks,
C2 Web