Hi...All...
I need some advice regarding inserting data from a form to a mysql table. I am unable to get the data posted into the table.

Following are my two files :
First file generates dynamic input fields and stores in a array (i hope my syntax is correct). Then upon submit i want the values to be inserted into the table.

firstfile.php

<form name="items" method="get">
<input name="number" type="text" id="number" size="6">
</p>
<input type="submit" name="Submit" value="Submit"> 
 </form>
<?
	echo "<table>";
	echo "<tr>";
	echo "<td width=90> Item No </td>";
	echo "<td width=310> Description </td>";
	echo "</tr>";
	echo "</table>";

?>
<?
echo "<form name=insertitem method=post action='secondfile.php'>";
	echo "<table>";
$number=$_GET["number"];
if ($number>15)
	{
	$number=15;
	echo "<font color=red>You are allowed to enter maximum 15 items at a time </font>";
	}
	else 
	{
	$number=$_GET["number"];
	}

for ($i=1; $i<=$number; $i++)
{
	echo "<tr>";
	echo "<td> <input type=text name=itemno[$i] size=5> </td>" ;
	echo "<td> <input type=text name=description[$i] size=50> </td>" ;
	echo "</tr>";
}
	echo "</table>";
	echo "<input type=submit name=submit value=Submit>";
	echo "</form>";

?>

</body>

secondfile.php

<?
include("link.php");

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

  $itemnos = $_POST['itemno'];
  $descriptions = $_POST['description'];
$query = "INSERT INTO table_name VALUES('','$itemnos','$descriptions')";
mysql_query($query);

mysql_close();
?>

All suggestions are appreciated.
Thanks in advance.

    First, add the "or die(...)" to the connect and query.

    Second, print the error message from mysql along with your own message, eg:
    @mysql_select_db($database) or die( "Unable to select database " . mysql_error());

    Third, without knowing the table's columns I can't guess if the INSERT is right, but I doubt it is. If you have an autoincrement column you don't pass that column as one of the values. Maybe this will work:

    $query = "INSERT INTO table_name (itemno, description) VALUES('$itemno','$description')";
    mysql_query($query) or die(mysql_error());
    

    As far as $itemnos and $descriptions arrays, you'll need to loop through them with a for loop and execute a query for each.

      Hi... I tried your suggestion... but it is not doing what is needed. I get value "0" in the itemno field and "Array" in the Description field.

      The other thing I want to clarify is does the
      $_Post['itemno'] ;
      etc . also to be inside a "for loop" ...?

      I tried to put it in a for loop like below and it does not add anything to the table :

      <?
      include("link.php");
      
      mysql_connect(localhost,$username,$password) or die("Unable to connect".mysql_error());
      @mysql_select_db($database) or die( "Unable to select database".mysql_error());
      
      $number=$_GET[number];
      for($i=1; $i<=number; $i++)
      {
        $itemnos = $_POST['itemno'];
        $descriptions = $_POST['description'];
      
      $query = "INSERT INTO Schedule_of_Bid (ItemNo, Description) 
      VALUES ('$itemnos','$descriptions')";
      mysql_query($query) or die(mysql_error());
      }
      mysql_close();
      ?>
      

      Thanks for your time and suggestion.
      Rgds

        I simplified your script a little so you can see what's going on with the arrays.

        First, display a form. Notice that the input elements named itemno and description have the [] brackets but do not have subscripts:

        <body>
        <form name=insertitem method=post action='secondfile.php'>
        <table><th>Item</th><th>Description</th>
        <?
        $number = 3;
        for ($i=1; $i<=$number; $i++)
        {
        	echo "<tr>";
        	echo "<td> <input type=text name=itemno[] size=5> </td>" ;
        	echo "<td> <input type=text name=description[] size=50> </td>" ;
        	echo "</tr>";
        }
        ?>
        </table>
        <input type=submit name=submit value=Submit>
        </form>
        </body>
        

        Now display what was posted. The $POST variable will contain arrays for itemno and description:

        <body>
        <?
        // store all posted intemnos and descriptions in local arrays
        $itemnos = $_POST['itemno'];
        $descriptions = $_POST['description'];
        
        // loop through array
        $number = count($itemnos);
        for ($i=0; $i<=$number; $i++)
        {
            // store a single item number and description in local variables
            $itno = $itemnos[$i];
            $desc = $descriptions[$i];
        
        // this is where your insert should be (instead of the echo),
        // insert the single values in $itnm and $desc
        
        if ($itemnos[$i] <> '') {
        echo "Item: " . $itno . "   Description: " . $desc . "<p>";
        }
        }
        ?>
        </body>
        

          Thanks a LOOOOOOOOOOTTTTTTTTTT...
          IT works....

          Just one question...why is that there is one more row is inserted with NULL values? Any clues...

          I overcame it by assigning another variable name

          // loop through array 
          $num = count($itemnos); 
          $numb = $num-1;
          for ($i=0; $i<=$numb; $i++) 
          { 
          ....etc..
          
          }
          

          Regards....and Happy New Year to you....

            for ($i=0; $i<=$numb; $i++)

            should probably be

            for ($i=0; $i<$num; $i++)

            But your fix works too.

              Write a Reply...