Solved

PHP Variable to prefill text box

Posted on 2008-10-12
4
2,389 Views
Last Modified: 2013-12-13
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
Comment
Question by:jseaman12
  • 2
4 Comments
 
LVL 1

Expert Comment

by:jodibean28
ID: 22697790
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
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22698021
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
 

Author Closing Comment

by:jseaman12
ID: 31505449
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 22703146
Great!  Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now