Jump to content

[SOLVED] generate an html form, to be submitted for insertion to a 3rd table


elkidogz

Recommended Posts

I'm stuck and i'm new at coding in general, so i would like some help please.

 

what i want these two pages to do:

 

1. display a form consisting of radio buttons and check boxes. multiple selection of check boxes per radio button basically.

 

I got this part done, it displays the data in the form which is being pulled from two separate tables.

 

2. Insert for each check box a record into a third table of the check box and radio button.

 

Code is as follows... this is what i have thus far and I'm not really sure why it's not working from what i can see step by step it should be inserting the records, these arrays are the most terrible thing i can think of having to do...

 

Code for the forms Generation of Checkboxes

<?php 
$result = mysql_query("SELECT * FROM pictures");
$output="<tr>";
while($row = mysql_fetch_array($result))
{
        $counter = $counter + 1;
        $output .="<!--START ROW FOR checkbox " .$row['fileName']." --> ";
        $output .="        <td width=10 id=left10 align=left ><!--".$counter."--><br></td>";
        $output .="        <td width=120 id=middle align=center valign=top >";
        $output .="        <a href=".$fileURL."".$row['fileName']." ><img src=".$fileURL."THUMBS/".$row['fileName']." ></a><br>";
        $output .="        <input type=checkbox name=PICTURES[] value=".$row['fileName']." />".$row[picTitle];
        $output .="        </td>";
        $output .="        <td width=10 id=right10  ><br></td>";
        $output .="<!-- END ROW FOR checkbox " .$row['fileName']." -->";
              if ( $counter % 4 == 0 )
              {
                     $output .= "</tr><tr>";
              }
}
$output .="</tr>";
// open the file
$fhandle = fopen( "FormParts/pictureCheckBoxes.php", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );?>

 

Code for the forms Generation of Radio buttons

<?php 
$result = mysql_query("SELECT * FROM persons");
$output="<tr>";
while($row = mysql_fetch_array($result))
{
        $counter = $counter + 1;
        $output .="<!-- START ROW FOR Radio " .$row['name']."--> ";
        $output .="        <td width=10 id=left10 align=left valign=top >".$counter.". ";
        $output .="        </td>";
        $output .="        <td width=120 id=middle align=left valign=top >";
        $output .="        <input type=radio name=PERSONS value=".$row['name']." >".$row['name'];
        $output .="        </td>";
        $output .="        <td width=10 id=right10 valign=top ></td>";
        $output .="<!-- END ROW FOR Radio " .$row['Name']."--> ";
              if ( $counter % 1 == 0 )
              {
                     $output .= "</tr><tr>";
              }
}
$output .="</tr>";
// open the file
$fhandle = fopen( "FormParts/personsBullets.php", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output ); ?>

 

so basically the form is

 

<form id="makerelation" action="makerelation.php" method="POST">
<?php include ('FormParts/personsBullets.php');
include('FormParts/pictureCheckBoxes.php');
?>
           <input type="submit" value="submit" name="Submit" />
           <input type="reset" value="reset" name="Reset" />
</form>

 

ok the form displays in html Perfectly fine.

 

submission is where i'm having an issue. I don't know what i'm doing wrong with the insert part...

 

<?php 
foreach ($_POST['PICTURES'] as $row=>$PICTURES)
{
      $pictures = mysql_real_escape_string($pics);
      $persons = mysql_real_escape_string($_POST['persons'][$row]);
      mysql_query('INSERT INTO relations (relPics, person) VALUES($pictures, $persons)') or die(mysql_error());
} ?>

so, what am i doing wrongly?

 

Link to comment
Share on other sites

In your last piece of code (the foreach), where is $pics defined? Also, $_POST['persons'] should be $_POST['PERSONS'] I believe. Your code logic is pretty difficult to follow. Why on earth are you physically writting data to a php file?

Link to comment
Share on other sites

well, basically the reason is that i want to reuse these form items over and over so they are more dynamic in nature (after i get it working...)

sorry that was older code in there... my apologies.

foreach ($_POST['PICTURES'][] as $row=>$PICTURES)
{
      $pictures = mysql_real_escape_string($PICTURES);
      $persons = $_POST['PERSONS'];
      mysql_query('INSERT INTO relations (relPics, person) VALUES ($pictures, $persons)') or die (mysql_error());
      echo $pictures." ".$persons;
}

 

Link to comment
Share on other sites

well, basically the reason is that i want to reuse these form items over and over so they are more dynamic in nature

 

To make them more dynamic in nature it would be best to store the information needed to create these forms within the database. Then query that data to create the forms when needed.

 

As for your problem... are you getting an error? This line....

 

$persons = $_POST['PERSONS'];

 

should be....

 

$persons = $_POST['PERSONS'][$row];

Link to comment
Share on other sites

yeah it doesn't generate any errors but it also doesn't insert anything either.

 

when i make the changes you suggest

 

Unknown column 'relPics' in 'field list'
is the error i get. the field name is correct however i just double checked the variable values on the relations table, they are correct.
Link to comment
Share on other sites

well, basically the reason is that i want to reuse these form items over and over so they are more dynamic in nature

 

To make them more dynamic in nature it would be best to store the information needed to create these forms within the database. Then query that data to create the forms when needed.

 

 

so instead of $output to a file, make it insert it into a table that would hold the values in a field... i like that! ~why didn't i think of that???? CAUSE i'm a NOOB!

Link to comment
Share on other sites

i added an echo before the insert to see what the values are...

 

the PERSONS was only passing the first character with the

 

$persons = $_POST['PERSONS'][$row];

 

Changed back to

 

$persons = $_POST['PERSONS']; and it sends all of the data from $PERSONS

 

1. Picture File Name=Mike.jpg

2.persons=Mike

Unknown column 'relPics' in 'field list'

 

again i know that the name of the field is relPics it's a text field...

Link to comment
Share on other sites

1. Picture File Name=genie.jpg persons=Genie

then it errors out (Unknown column '$pictures' in 'field list')

 

<?php 
foreach ($_POST['PICTURES'] as $row => $PICTURES)
{
      $pictures = mysql_real_escape_string($PICTURES);
      $persons = $_POST['PERSONS'];
      echo "1. Picture File Name=".$pictures."persons=".$persons." ";
      mysql_query('INSERT INTO relations (relPic, person) VALUES ($pictures, $persons)') or die (mysql_error());
      echo "2. Picture File Name=".$pictures." persons=".$persons." ";
}
?>

 

changing

$persons = $_POST['PERSONS'];

to

$persons = $_POST['PERSONS'][$row]; doesn't resolve this.

 

what is actually going on here? the error message seems to indicate that the SQL statement isn't correct... but it looks ok to me.

Link to comment
Share on other sites

<?php
$persons = $_POST['PERSONS'];
foreach ($_POST['PICTURES'] as $row => $PICTURES)
{
      $pics = mysql_real_escape_string($PICTURES);
      echo "Picture File Name ".$pictures." was related to: ".$persons."<br>";
      $sql="INSERT INTO relations (relPic, person)VALUES('$pics', '$persons')";
      if (!mysql_query($sql,$con))
            {
                  die('Error: ' . mysql_error());
            }
}
?>

 

I think the SQL statement was getting screwed up.

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.