Log in

View Full Version : Retrieving data from database and displaying in textbox when the button click



rhodarose
01-04-2011, 06:47 AM
I search about the code for retrieving data from database when the retrieve button was click. But I can’t find any codes that similar in my situation.

Here is the flow of my system. I have plt_no and a retrieve button. The retrieve button is to check if the plt_no is already exist or not if it is already exist the data from the database will appear in the text area/textbox and if the plt_no did not exist there’s a message that the id number did not exist and I can input data in the textboxes.


I have an idea of using this code:
This code is to check if the Retrieve button is click and the plt_no is the data which the retrieve depend on. Like for example the user put plt_no 111 and when the user click the Retrieve button, if the 111 is already exist the data will appear in the textboxes but if not there’s a message appear saying the plt no did not exist.



if(isset($_POST['Retrieve']))
$plt_no = $_POST['plt_no'];



And I have search code for retrieving data:




<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data = mysql_query("SELECT * FROM friends WHERE pet='Cat'")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<th>Color:</th> <td>".$info['fav_color'] . "</td> ";
Print "<th>Food:</th> <td>".$info['fav_food'] . "</td> ";
Print "<th>Pet:</th> <td>".$info['pet'] . " </td></tr>";
}
Print "</table>";
?>


My problem is how can I used that code and if that code is the code I need to used for retrieving data.


I attached my full code.

I have an idea but i am luck of knowledge in coding

Any help is highly appreciated I am willing to learn and share

Thank you

VijayKanta
01-04-2011, 10:53 AM
I did not download your provided text file to see complete source code, but as far as my understanding goes, you need to insert data into textarea if data is present in database, otherwise it should simply alert you that there is no record.

It should be fairly simple.



<?php
// All your previous code, plus some editing you must do to comply with following
$present = mysql_num_rows($data);
if(!$present) {
echo "No records found!";
$txtarea = '';
} else {
$txtarea = mysql_fetch_array($data)[0]; // Assuming you only want the first field to be inserted into the textarea.
}
?>

<textarea><?php echo $txtarea; ?></textarea>

rhodarose
01-05-2011, 12:36 AM
Yes its kind a logic that I want to happen.

Honestly, I felt difficulty in combining of codes.
Kindly see my attached file

Thank you

rhodarose
01-05-2011, 01:06 AM
I have the code I want to add for retrieving data:



$query = "SELECT plt_no FROM plt_transact WHERE plt_no= '" . $_POST["plt_no"] . "'";
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$row = mysql_fetch_array($result);
$num_results = mysql_num_rows($result);
if ($num_results > 0) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


Is it that code is right?for retrieving data?
My question is it automatically that when I insert plt_no then i click retrieve button it plt_no is exist the data will appear in the textboxes but I don't know how can I appear it and if the plt_no did not exist the textboxes is blank and theres a message that saying plt no did not exist.



if($_POST["enum_plt"] && $_POST["plt_typeno"])
{
$query = "SELECT * FROM plt_type WHERE plt_typeno = '" . $_POST["plt_typeno"] . "'";
$result = mysql_query($query);
if($result)
{
$rexist = 1;


$query = "SELECT plt_no FROM plt_transact WHERE plt_no= '" . $_POST["plt_no"] . "'";
$result = mysql_query($query) or die ("Error in query: $query " . mysql_error());
$row = mysql_fetch_array($result);
$num_results = mysql_num_rows($result);
if ($num_results > 0) {
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

echo "<p>PLT No:<input type='text' name='plt_no' onkeypress='return handleEnter(event,\"plt_date\");' /></br />";
echo "PLT Date:<input type='text' name='plt_date' onkeypress='return handleEnter(event,\"bom0\");' /></p>";
echo "<p><input type='Button' value='Retrieve' id='Retrieve' name='Retrieve' /></p>";
echo "<table>";
$totalrows = mysql_num_rows($result);
$trows = $totalrows - 1;
for($ctr = 0; $ctr < $trows; $ctr++)
{
$tctr = $ctr + 1;
$row = mysql_fetch_array($result);
echo "\n\t<tr>";
echo "\n\t\t<td><input type='text' name='bom[]' id='bom" . $ctr . "' onkeypress='return handleEnter(event,\"qty" . $ctr . "\");' /></td>";
echo "\n\t\t<td>" . $row["plt_chemicals"];
echo "<input type='hidden' name='chem[]' value='" . $row["plt_chemicals"] . "' /></td>";
echo "\n\t\t<td><input type='text' name='qty[]' id='qty" . $ctr . "' onkeypress='return handleEnter(event,\"part" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='part[]' id='part" . $ctr . "' onkeypress='return handleEnter(event,\"lot" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='lot[]' id='lot" . $ctr . "' onkeypress='return handleEnter(event,\"bom" . $tctr . "\");' /></td>";
echo "\n\t</tr>";
}
if($totalrows > 1);
{
$row = mysql_fetch_array($result);
echo "\n\t<tr>";
echo "\n\t\t<td><input type='text' name='bom[]' id='bom" . $ctr . "' onkeypress='return handleEnter(event,\"qty" . $ctr . "\");' /></td>";
echo "\n\t\t<td>" . $row["plt_chemicals"];
echo "<input type='hidden' name='chem[]' value='" . $row["plt_chemicals"] . "' /></td>";
echo "\n\t\t<td><input type='text' name='qty[]' id='qty" . $ctr . "' onkeypress='return handleEnter(event,\"part" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='part[]' id='part" . $ctr . "' onkeypress='return handleEnter(event,\"lot" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='lot[]' id='lot" . $ctr . "' onkeypress='return handleEnter(event,\"b1\");' /></td>";
echo "\n\t</tr>";
}
echo "</table>";

echo "<p>&nbsp;</p>";
echo "<table>";
echo "<tr><td>B1</td><td><input type='text' name='b1' id='b1' onkeypress='return handleEnter(event,\"sc2\");' /></td></tr>";
echo "<tr><td>SC2</td><td><input type='text' name='sc2' id='sc2' onkeypress='return handleEnter(event,\"timein0\");' /></td></tr>";
echo "</table>";
echo "<p>&nbsp;</p>";

$query = "SELECT * FROM trace_operations WHERE plt_typeno = '" . $_POST["plt_typeno"] . "' AND operation_name IN ('Operation1' , 'Operation2' , 'Operation3')ORDER BY operation_name";
$last_operation_name = "";
$result = mysql_query($query);
if($result)
{
$rexist = 1;
echo "<table>";
$totalrows = mysql_num_rows($result);
$trows = $totalrows - 1;
for($ctr = 0; $ctr < $trows; $ctr++)
{
$row = mysql_fetch_array($result);
$tctr = $ctr + 1;
echo "\n\t<tr>";
echo "\n\t\t<td>";
if ($last_operation_name != $row["operation_name"]) echo $row["operation_name"];
$last_operation_name = $row["operation_name"];
echo "<input type='hidden' name='opname[]' value='" . $row["operation_name"] . "' /></td>";
echo "\n\t\t<td><input type='text' name='timein[]' id='timein" . $ctr . "' onkeypress='return handleEnter(event,\"timeout" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='timeout[]' id='timeout" . $ctr . "' onkeypress='return handleEnter(event,\"inqty" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='inqty[]' id='inqty" . $ctr . "' onkeypress='return handleEnter(event,\"outqty" . $ctr . "\");' /></td>";
echo "\n\t\t<td>" . $row["input_unit"];
echo "<input type='hidden' name='iun[]' value='" . $row["input_unit"] . "' /></td>";
echo "\n\t\t<td><input type='text' name='outqty[]' id='outqty" . $ctr . "' onkeypress='return handleEnter(event,\"idno" . $ctr . "\");' /></td>";
echo "\n\t\t<td>" . $row["output_unit"];
echo "<input type='hidden' name='oun[]' value='" . $row["output_unit"] . "'></td>";
echo "\n\t\t<td><input type='text' name='idno[]' id='idno" . $ctr . "' onkeypress='return handleEnter(event,\"mcno" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='mcno[]' id='mcno" . $ctr . "' onkeypress='return handleEnter(event,\"varqty" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='varqty[]' id='varqty" . $ctr . "' onkeypress='return handleEnter(event,\"varplt" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='varplt[]' id='varplt" . $ctr . "' onkeypress='return handleEnter(event,\"dateshift" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='dateshift[]' id='dateshift" . $ctr . "' onkeypress='return handleEnter(event,\"shift" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='shift[]' id='shift" . $ctr . "' onkeypress='return handleEnter(event,\"timein" . $tctr . "\");' /></td>";
echo "\n\t</tr>";
}
if($totalrows > 1);
{
$row = mysql_fetch_array($result);
echo "\n\t<tr>";
echo "\n\t\t<td>";
if ($last_operation_name != $row["operation_name"]) echo $row["operation_name"];
$last_operation_name = $row["operation_name"];
echo "<input type='hidden' name='opname[]' value='" . $row["operation_name"] . "' /></td>";
echo "<input type='hidden' name='opname[]' value='" . $row["operation_name"] . "' /></td>";
echo "\n\t\t<td><input type='text' name='timein[]' id='timein" . $ctr . "' onkeypress='return handleEnter(event,\"timeout" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='timeout[]' id='timeout" . $ctr . "' onkeypress='return handleEnter(event,\"inqty" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='inqty[]' id='inqty" . $ctr . "' onkeypress='return handleEnter(event,\"outqty" . $ctr . "\");' /></td>";
echo "\n\t\t<td>" . $row["input_unit"];
echo "<input type='hidden' name='iun[]' value='" . $row["input_unit"] . "' /></td>";
echo "\n\t\t<td><input type='text' name='outqty[]' id='outqty" . $ctr . "' onkeypress='return handleEnter(event,\"idno" . $ctr . "\");' /></td>";
echo "\n\t\t<td>" . $row["output_unit"];
echo "<input type='hidden' name='oun[]' value='" . $row["output_unit"] . "'></td>";
echo "\n\t\t<td><input type='text' name='idno[]' id='idno" . $ctr . "' onkeypress='return handleEnter(event,\"mcno" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='mcno[]' id='mcno" . $ctr . "' onkeypress='return handleEnter(event,\"varqty" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='varqty[]' id='varqty" . $ctr . "' onkeypress='return handleEnter(event,\"varplt" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='varplt[]' id='varplt" . $ctr . "' onkeypress='return handleEnter(event,\"dateshift" . $ctr . "\");' /></td>";
echo "\n\t\t<td><input type='text' name='dateshift[]' id='dateshift" . $ctr . "' onkeypress='return handleEnter(event,\"shift\");' /></td>";
echo "\n\t\t<td><input type='text' name='shift[]' id='shift" . $ctr . "' onkeypress='return handleEnter(event,\"saveform\");' /></td>";
echo "\n\t</tr>";
}
echo "</table>";
}
}
}
}
}

this code is incomplete, can someone told me what is the missing code to view the retrieve data?and how does the retrieve works.

Thank you