stevdev1 Posted November 18, 2006 Share Posted November 18, 2006 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><?phprequire_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]<?phprequire_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 More sharing options...
esukf Posted November 18, 2006 Share Posted November 18, 2006 The insert statement should be :-[code]$sql = "INSERT INTO shipments (`quantity_shipped`, `date`) VALUES ('$quantity_shipped', '$date')";[/code]Also check primary key in shipment is set to auto_increment. Link to comment https://forums.phpfreaks.com/topic/27700-so-close-just-need-some-direction/#findComment-126693 Share on other sites More sharing options...
printf Posted November 18, 2006 Share Posted November 18, 2006 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 Link to comment https://forums.phpfreaks.com/topic/27700-so-close-just-need-some-direction/#findComment-126699 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.