0

HI, i'am an newbie and have following question

I have two tables

1. repair_details with fields partnumber, quantity, Pn_id
2. Parts with fields Pn_id, price, partnumber....

I would like to fill in repair_details the Pn_id automaticly after I input the partnumber and quantity.

Any suggestions?
thanks

2
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by biebel
0

Here's a 5 minute code job.

input.php

<?php
$error = $_GET['error'];
?>
<html>
<head>
<title>Input</title>
</head>
<body>
<form action="doit.php" method="post">
<p align="center" style="color:#F00"><strong><?php echo $error; ?></strong></p>
<p>Part number: <input type="text" name="partnumber" /></p>
<p>Quantity: <input type="text" name="qty" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
</body>
</html>

doit.php:

<?php

$host = "localhost";
$user = "username";
$pass = "password";
$db =   "databasename";

mysql_connect($host, $user, $pass) or die("Couldnt connect to database");
mysql_select_db($db) or die("Couldnt select database");

$error = "";
if(!$_POST['qty']) { $error = "MISSING QUANTITY!"; }
if(!$_POST['partnumber']) { $error = "MISSING PARTNUMBER!"; }
$where = "Location: input.php?error=".$error;

if($error!="") {
	header($where);
	die();
}

$sql = "SELECT * FROM `parts` WHERE `partnumber`='".$_POST['partnumber']."' LIMIT 0,1";
$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed: ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_BOTH);

$pnid = $row['Pn_id'];
$price = $row['price'];

$sql = "INSERT INTO `repair_details`(`Pn_id`,`price`,`partnumber`,`quantity`) VALUES ('".$pnid."','".$price."','".$_POST['partnumber']."','".$_POST['qty']."'";
$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed: ' . mysql_error());

$error = "Entry for part number ".$_POST['partnumber']." entered into database.";

$where = "Location: input.php?error=".$error;
header($where);
die();
?>

Not tested, probably not exactly what you wanted, but an idea to work from. Expand and extrapolate. :)

Just my 2¢. Hope this helps.

0

Here's a 5 minute code job.

input.php

<?php
$error = $_GET['error'];
?>
<html>
<head>
<title>Input</title>
</head>
<body>
<form action="doit.php" method="post">
<p align="center" style="color:#F00"><strong><?php echo $error; ?></strong></p>
<p>Part number: <input type="text" name="partnumber" /></p>
<p>Quantity: <input type="text" name="qty" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
</body>
</html>

doit.php:

<?php

$host = "localhost";
$user = "username";
$pass = "password";
$db =   "databasename";

mysql_connect($host, $user, $pass) or die("Couldnt connect to database");
mysql_select_db($db) or die("Couldnt select database");

$error = "";
if(!$_POST['qty']) { $error = "MISSING QUANTITY!"; }
if(!$_POST['partnumber']) { $error = "MISSING PARTNUMBER!"; }
$where = "Location: input.php?error=".$error;

if($error!="") {
	header($where);
	die();
}

$sql = "SELECT * FROM `parts` WHERE `partnumber`='".$_POST['partnumber']."' LIMIT 0,1";
$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed: ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_BOTH);

$pnid = $row['Pn_id'];
$price = $row['price'];

$sql = "INSERT INTO `repair_details`(`Pn_id`,`price`,`partnumber`,`quantity`) VALUES ('".$pnid."','".$price."','".$_POST['partnumber']."','".$_POST['qty']."'";
$result = mysql_query($sql) or die('\"'.$sql.'\" Query failed: ' . mysql_error());

$error = "Entry for part number ".$_POST['partnumber']." entered into database.";

$where = "Location: input.php?error=".$error;
header($where);
die();
?>

Not tested, probably not exactly what you wanted, but an idea to work from. Expand and extrapolate. :)

Just my 2¢. Hope this helps.

thanks. you put me in the right direction

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.