How to automatically get the id field from a parent table to a child table
My scenario;
I have two tables:
first is the "products" table that contains the following:
- productid
- productname
- description
second is the "productusers" that contains the following:
- id
- productid - foreign key from first table
- usertype - a textfield wherein browsers can input any user type of the product (e.g. electricians, nurses, etc.)
i have a php page that displays the product record, and at the same time a form wherein browsers can fill up the user type and click submit button, below is my code for this page:
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$id=$_GET['productid']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}
$result=mysql_query("select productid, productname, description from products where productid=$id ");
if (!$result) {
$message = "Invalid query: " . mysql_error() . "\n";
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
echo $row['productid'] . "\n";
echo "<br>";
echo $row['productname'] . "\n";
echo $row['description'] . "\n";
echo "<br><br><br><br>";
echo "This product is used by:";
echo '<form action="addusers.php" method="post">';
echo 'Product ID <input type="text" name="productid" value="';
echo $row['productid'];
echo '开发者_如何学Go"';
echo ' maxlength="50" disabled="disabled"/>';
echo 'User Type <input type="text" name="proffession" maxlength="50"/>';
echo '<input type="submit" name="formSubmit" value="Submit" />' ;
echo '</form>';
The page that process my form when submit is click is below:
mysql_connect('localhost',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$sql = "INSERT INTO usedby (usedbyid, productid, proffession) VALUES ('','$_POST[productid]','$_POST[proffession]')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "Your Information Was Successfully Posted";
The page ends up with an error:
Notice: Undefined index: productid in D:\chlark\xampp\htdocs\testing\addusers.php on line 22
Error: Cannot add or update a child row: a foreign key constraint fails (`test`.`usedby`, CONSTRAINT `pid` FOREIGN KEY (`productid`) REFERENCES `products` (`productid`) ON DELETE NO ACTION ON UPDATE NO ACTION)
My objective is that the form when submitted must automatically get the corresponding product id where i was filled up for database storage. So that i can perform a query of user types per brand. Example if filled up the form for product id #45 the data that will be stored on mysql will be the values ('','45','the input from form').
how can i exactly do this?
is there a way that i do not need anymore to show this code on my form?
echo 'Product ID <input type="text" name="productid" value="';
echo $row['productid'];
echo '"';
echo ' maxlength="50" disabled="disabled"/>';
and only the
echo 'User Type <input type="text" name="proffession" maxlength="50"/>';
echo '<input type="submit" name="formSubmit" value="Submit" />' ;
can be shown, but the product id will still be saved on the table when submit button is clicked.
i'm a php mysql newbie, thanks.
Yes, change this:
echo 'Product ID <input type="text" name="productid" value="';
echo $row['productid'];
echo '"';
echo ' maxlength="50" disabled="disabled"/>';
to this:
echo '<input type="hidden" name="productid" value="' . $row['productid'] . '"/>';
精彩评论