Jump to content

So Close, Just need some direction


stevdev1

Recommended Posts

I am totally new to PHP, so bear with me. I am creating a form that will update my database with the quantity of all products shipped on a certain date. So my form pulls the Prod_ID and Prod_Name from one table (product) and will insert those 2 fields along with Quantity_Shipped and Date into a second table Shipments.
Here is the code for my form:[code]<form action="update.php" method="POST"> 
<table align="center" width="60%">
  <tr>
    <td width="27%">Product ID </td>
    <td width="15%">Product Name</td>
    <td width="6%">Quantity Shipped </td>
    <td width="6%">Date</td>
  </tr>
<?php
require_once("Connections/gdlabs.php");
mysql_select_db("gdlabs");

$result = mysql_query("SELECT id, prod_name FROM product");
while($row = mysql_fetch_array( $result ))
    {

$id = $row['id'];
?>
  <tr>
    <td>
<input type="hidden" name="id[]" value="<?php echo $row['id']; ?>">
<?=$id?>
</td>
    <td><?php echo $row['prod_name']; ?></td>
    <td><input class="form" type="text" size="3" name="quantity_shipped[]" value="<?=$row['quantity_shipped']; ?>"></td>
    <td><input class="form" type="text" size="3" name="date[]" value="<?=$row['date']; ?>"></td>

  </tr>
<?php
      }
?>
<tr>
<td><input class="form" type="submit" value="Submit"></td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
</form>[/code]

Here is the code for my update.php:[code]<?php
require_once("Connections/gdlabs.php");
mysql_select_db("gdlabs");


if(isset($_POST['id'])) {
    foreach ($_POST['id'] as $k => $id) {

        $quantity_shipped = $_POST['quantity_shipped'][$k];
        $date = $_POST['date'][$k];
               
        $sql = "Insert shipments SET `quantity_shipped`='$quantity_shipped', `date`='$date' ";
        mysql_query($sql) or die(mysql_error());       
    }




  echo "The information was updated! <br>";
  echo "<a href='home.php'>Click here</a> to continue <br>";

}?>[/code]

I think I am close but when I submit the data, I get the following error "Field 'prodid' doesn't have a default value". Prodid is the primary key in the product table. I have been working on this for a week, I can feel the stomach acid creeping up my throat, and I am a noob. Any help and explanations would be appreciated. I want to learn.
Link to comment
https://forums.phpfreaks.com/topic/27700-so-close-just-need-some-direction/
Share on other sites

SET is only used for UPDATE, so this line...

[code]$sql = "Insert shipments SET `quantity_shipped`='$quantity_shipped', `date`='$date' ";[/code]

Should be changed, to something like below, but not knowing what your table looks like, I can't give you the exact format...

[code]$sql = "INSERT INTO shipments (column_one, column_two, column_three) VALUES ('column_one_value', 'column_two_value', 'column_three_value' );";[/code]

Where column_one, column_two, column_three would be changed to those column names, and column_one_value, column_two_value, column_three_value would be changed to values that represent values for each of your column_names!

printf

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.