Вопрос

I am very new to HTML and SQL so everything I am doing is self taught through this and other online sites. I create a number of forms based on the number of entries in my database. This number can vary up approximately 50 entries. Essentially, what I want to do is create a submit button that posts three pieces of information to a second database. These pieces of information are the part number (always a unique value), the cure time, and the press number. I know part of my problem is giving values unique names so the submit knows what data it is to be using, but I cannot seem to figure out the proper format. For the time being, I only want to pass the $partnum variable to the second database. I can reverse engineer backward from there to get the rest.

PHP code

function press_list($partnum)
{
    echo "<select name=\"Press_used".$partnum."\"style=\"float:right\">";
    echo "<option selected>---</option>";
    echo "<option value =\"A1-T\">A1-T</option>";
    ... <-- other options omitted to save space
    echo "<option value =\"A10-B\">A10-B</option>";
    echo "</select>";
}
function load_button($cureleft)
{
   if ($cureleft <= 0)
   {
       echo "<input type=\"submit\" value=\"Loading\" />";
   }
   else
   {
       echo "<input type=\"submit\" value=\"Loading\" disabled>";
   }

}
while($row = mysqli_fetch_array($result))
{
   $partnum = $row['Part_Number'];
   echo "<form action=\"loadpost.php\" name=\"".$row['Part_Number']."\">";
   echo "<fieldset>";
   echo "<h3>".$row['Part_Number']."</h3>";
   echo "<input type=\"hidden\" name=\"".$partnum."\" value=\"".$partnum."\">";
   echo "<p class=\"center\">".$row['New_#']."</p>";
   echo "<p class=\"center\">Size: <b>".$row['Tire_Size']."</b></p>";   
   echo "<p class=\"leftalign\">Tread: ";
   echo $row['Type_&_Tread'];
   $weight=number_format($row['Build_Weight'],1);
   echo "<span class=\"rightalign\">Weight: ".$weight."</span></p>";
   echo "<p class=\"box\">--Notes--";
   echo "<br><span class=\"center\">".$row['NOTES']."</span></p>";
   echo "<p class=\"leftalign\">Scheduled Press: ";
   echo $row['Press'];
   press_list($partnum);
   echo "</p>";
   echo "<p class=\"leftalign\">Cure time: ";
   echo ($row['Cure_Time']*60);
   $cureleft = (($row['Cure_Time']*60)-190);
   echo "<span class=\"rightalign\">Cure left: ".$cureleft."</span></p>";
   load_button($cureleft);
   echo "</fieldset>";
   echo "</form>
   ";
}

mysqli_close($con);

?>

Loadpost.php

<?php
    $con=mysqli_connect("localhost","root","admin","floortracking");
// Check connection
if (mysqli_connect_errno())
  {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $sql="INSERT INTO loaded (Cure, Time, Tire, Press)
VALUES ('53',now(),'$_POST[$partnum]','$_POST[Press_used]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "<p>1 record added</p>";
mysqli_close($con);
?>

One form HTML

<form action="loadpost.php" name="01-590905-001R"><fieldset><h3>01-590905-001R</h3><input type="hidden" name="partnum" value="01-590905-001R"><p class="center">20005133</p><p class="center">Size: <b>RECLAIM</b></p><p class="leftalign">Tread: BO2K<span class="rightalign">Weight: 56.0</span></p><p class="box">--Notes--<br><span class="center">RECLAIM</span></p><p class="leftalign">Scheduled Press: A1-T<select name="Press_used" style="float:right"><option selected>---</option><option value ="A1-T">A1-T</option><option value ="A1-M">A1-M</option><option value ="A1-B">A1-B</option><option value ="A2-T">A2-T</option><option value ="A2-B">A2-B</option><option value ="A3-T">A3-T</option><option value ="A3-M">A3-M</option><option value ="A3-B">A3-B</option><option value ="A4-T">A4-T</option><option value ="A4-B">A4-B</option><option value ="A5-T">A2-T</option><option value ="A5-B">A2-B</option><option value ="A6">A6</option><option value ="A7-T">A7-T</option><option value ="A7-B">A7-B</option><option value ="A8-T">A8-T</option><option value ="A8-B">A8-B</option><option value ="A9-T">A9-T</option><option value ="A9-B">A9-B</option><option value ="A10-T">A10-T</option><option value ="A10-B">A10-B</option></select></p><p class="leftalign">Cure time: 180<span class="rightalign">Cure left: -10</span></p><input type="submit" value="Loading" ></fieldset></form>
Это было полезно?

Решение

First

You have to change this

echo "<input type=\"hidden\" name=\"".$partnum."\" value=\"".$partnum."\">";

to this

echo "<input type=\"hidden\" name='partnum' value=\"".$partnum."\">";

Since you have one submit button inside of each form, they will submit the form they are inside of. You don't have to worry about submitting the right form. There won't be any other input field submitted which is not inside of that form, you are submitting from

Second

You don't need to echo the partnum inside every select

Change this

echo "<select name=\"Press_used".$partnum."\"style=\"float:right\">";

into this

echo "<select name=\"Press_used\"style=\"float:right\">";

Thirdly, the sql

$sql="INSERT INTO loaded (Cure, Time, Tire, Press)
VALUES ('53',now(),'$_POST[$partnum]','$_POST[Press_used]')";

$sql="INSERT INTO loaded (Cure, Time, Tire, Press)
VALUES ('53',now(),'".$_POST['partnum']."','".$_POST["Press_used"]."')";

I think that's all

Update:

This:

echo "<form action=\"loadpost.php\" name=\"".$row['Part_Number']."\">";

Into this:

echo "<form action=\"loadpost.php\" name=\"".$row['Part_Number']."\" method='POST'>";

Другие советы

Please have a look at your $_Post-Array in Loadpost.php.

I know part of my problem is giving values unique names

Correct. Alter the name properties of the input/select fields in your form. Use only static names.

echo "<input type=\"hidden\" name=\"partnum\" value=\"".$partnum."\">";

and

echo "<select name=\"Press_used\"style=\"float:right\">";

Loadpost.php:

Now you can access the values in $_POST:

$_POST['partnum']
$_POST['Press_used']
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top