[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2439
  • Last Modified:

PHP Variable to prefill text box

I want to be able to take a textbox and prefill it with the variable from mysql query.

I want to use the variable from this section; and prefill a textbox
$ifs = mysql_query("SELECT final_sale
FROM oetable_backup WHERE upper($field)='$find'");

Textbox;
<input type="text" name="sales" value="<?php echo $ifs; ?>">
      Ship Date:

How do I do this??

Thanks,
Jeff
<?php
// Turn off all error reporting
error_reporting(0);
 
include("include/phpgrid.php");
 
 
 
 
$hostName = "localhost";
$userName = "jmichels";
$password = "hug344";
$dbName	  = "thstew_orders";
?>
<html>
<head>
<title>Invoice</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
 
<body>
<?php
 
$idresult = mysql_query("SELECT id
FROM oetable_backup WHERE upper($field)='$find'");
 
$ifs = mysql_query("SELECT final_sale
FROM oetable_backup WHERE upper($field)='$find'");
 
 
 
 
 
$dg = new C_DataGrid($hostName, $userName, $password, $dbName);
 
// We preform a bit of filtering
$find = mysql_real_escape_string($_POST['find']);
$field = mysql_real_escape_string($_POST['field']);
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim ($find);
 
 
$dg -> set_gridpath     ("include/");
$dg -> set_sql          ("SELECT
entry_date,territory,principle,customer,po,part_no,qty,initial_sale,due_to_ship,ship_date,qty_shipped,comm,invoice,comm_due,type_ind,id
FROM oetable_backup WHERE upper($field)='$find'");
 
 
 
$dg -> set_sql_table    ("oetable_backup");
 
// change column titles
//$dg -> set_col_title    ("EmployeeId", "Employee ID");
//$dg -> set_col_title    ("LastName", "Last Name");
//$dg -> set_col_title    ("FirstName", "First Name");
 
// set background and mouse over color
$dg -> set_alt_bgcolor  ("#ffffff, #e9eff2");
$dg -> set_onmouseover  ("lightyellow");
 
// hide a column
$dg -> set_col_hidden   ("Notes");
 
// display URL as hyperlink
$dg -> set_col_link    ("ReportsTo",                         
						"/query.php?EmployeeId=",                         
						"EmployeeId",                         
						"target='_new'"); 
 
// set page size
$dg -> set_page_size(10);
 
// sum up columns
$dg -> set_col_sum_enabled(true);
$dg -> set_col_sum      ("initial_sale");
$dg -> set_col_sum      ("final_sale");
$dg -> set_col_sum      ("qty");
$dg -> set_col_sum      ("comm");
 
$dg -> set_theme("sweet");
 
// make the datagrid editable
$dg -> set_allow_actions(true);
 
$dg -> set_action_type ("V,E");
 
// turn on inline-editing with Ajax enabled
//$dg -> set_inlineedit_enabled(true, true);
 
//$dg -> set_ok_showcredit(true);
$dg -> set_allow_export(true);
$dg -> set_toolbar_enabled(FALSE);
 
$dg -> debug();
 
$dg -> display();
echo "<b>Searched For:</b> " .$ifs;
?>
 
 
<h2 align="center"> </h2>
<h2 align="center"> </h2>
<h2 align="center"><strong><font color="#006600"> </font></strong></h2>
<h2 align="center"><strong><font color="#006600"> </font></strong></h2>
<h2 align="center"><strong><font color="#006600"><img src="http://www.thstewart.com/images/invoice.gif" alt="" title="">TH Stewart Co.</font></strong><strong><font color="#006600"><font size="3"></font></font></strong></h2>
<div align="center">
<form action="new.php" method="post">
<div> </div>
<div align="left">
  <p align="center">Final Sale:
 
 
      <input type="text" name="sales" value="<?php echo $ifs; ?>">
      Ship Date:
        <input type="text" name="sdate"> 
        Qty Shipped:
        <input type="text" name="qtys"> 
        ID:
        <input type="text" name="tid">
        
        </p>
 
  <p align="center"> Invoice#:
		    <input type="text" name="seek">
   
        
 
        <br>
        <br>
</p>
  </div>
<div> </div>
<div> </div>
<div>
<input type="hidden" value="yes" name="searching">
<input type="submit" value="Invoice" name="search"></div></form></div>

Open in new window

0
jseaman12
Asked:
jseaman12
  • 2
1 Solution
 
jodibean28Commented:
This should work...

$ifs = mysql_query("SELECT final_sale
FROM oetable_backup WHERE upper($field)='$find'"); 
 
if ($row = mysql_fetch_assoc($ifs))
     $finalsale = $row[0];
 
<input type="text" name="sales" value="<?=$finalsale?>"> 

Open in new window

0
 
Ray PaseurCommented:
Jeff: First of all, turn on ALL error reporting.  You can't write error free code without seeing the errors!

Next, understand that mysql_query() returns a resource or FALSE on error.  You MUST TEST the return from these functions.  In your post above, your resource name is $ifs

Then once you know the query worked, you MUST TEST to see if there is anything in the result set.

If there is anything in the result set, you might find that you need to iterate over the result set (multiple results occurred that satisfied the query).  However since you are looking for the "upper" you may expect one result.  

Best practices, and best of luck going forward! ~Ray

Be careful of the variable names - final_sale is not the same as finalsale.
// TESTING WHETHER THE QUERY WORKED OR FAILED
if (!$ifs = mysql_query("SELECT final_sale FROM oetable_backup WHERE upper($field)='$find'")) { die mysql_error(); }
 
// TESTING WHETHER THE RESULT SET EXISTS
if (!mysql_num_rows($ifs)) { die('NOTHING IN RESULT SET'); }
 
// GETTING THE RELEVANT DATA FROM THE RESULT SET INTO A LOCAL VARIABLE
if (!$row = mysql_fetch_assoc($ifs)) { die('ERROR FETCHING RESULT SET'); }
$finalsale = $row["final_sale"]; // NOTE finalsale !== final_sale
 
// WRITING THE FORM INPUT STATEMENT INTO THE HTML
echo "<input type=\"text\" name=\"sales\" value=\"$finalsale\" /> \n";

Open in new window

0
 
jseaman12Author Commented:
It worked. I just had to place my php code in the front and end because that part of the page is HTML. But I been trying to figure this out for months. This is the best news. Thanks a lot...

<?php
  echo "<input type=\"text\" name=\"sales\" value=\"$finalsale\" /> \n";
  ?>
0
 
Ray PaseurCommented:
Great!  Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now