Forum Moderators: coopster

Message Too Old, No Replies

How to Autofill text box from mysql database?

         

imagus

12:18 am on Apr 13, 2003 (gmt 0)

10+ Year Member



Hi guys,

I need a php script to automatically retrieve data from a database and fill in a form.
This form is for a product and will include - product_code, product_name, price, weight etc.
all the data is in the database and I already have a script to search and return the results.
I just don't know how to generate a form that will be automatically filled from the search.

I hope you can help!
cheers

DrDoc

12:22 am on Apr 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome [webmasterworld.com] to Webmaster World!

I already have a script to search and return the results

Well, since you know how to display the data, all you have to do is echo the values inside the value attribute:

<input type="text" name="something" value="<?php echo $something;?>">
<textarea name="example" rows="10" cols="35"><?php echo $example;?></textarea>

So, just query the database to have some values, and then do something like the above example :)

imagus

11:09 am on Apr 13, 2003 (gmt 0)

10+ Year Member



thanx Doc,

I've tried the code example you posted but keep getting a parse error!

here is some of the code I am currently usin to display the data:

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$product_code = $row["product_code"];
$type = $row["type"];
$product_name = $row["product_name"];

echo "Product Code) &nbsp $product_code <br>" ;
echo "Type) &nbsp $type <br>";
echo "Product Name) &nbsp $product_name <br>";
$count++ ;
}

I need these variables to be displayed in text boxes.
Thanx for your help :)

wruk999

11:45 am on Apr 13, 2003 (gmt 0)

10+ Year Member



Hi imagus,

Here is what I use for textareas:


<textarea name="desc" rows=5 cols=30><? echo $myrow["description"]?></textarea>

and for normal text fields:


<input type="TEXT" name="title" value="<?php echo $myrow["name"]?>" size=30>

I don't set them into $variables, and just echo them from the select statement:


 $sql = "SELECT * FROM products WHERE id=$theid";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);

Let me know if this type of code works?

Regards,
wruk999

imagus

12:20 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



here it is

<html>
<head>
<title>Product Search</title>
</head>

<body>

<form name="form" action="search3.php" method="get">
<input type="text" name="q" size="16"/> <br>
<input type="submit" name="Submit" value="Search" />

</form>

<?php

// Get the search variable from URL

$var = @$HTTP_GET_VARS['q'] ;
$trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=1;

// check for an empty string and display a message.
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}

// check for a search parameter
if (!isset($var))
{
echo "<p>We dont seem to have a search parameter!</p>";
exit;
}

//connect to database
mysql_connect("localhost","root","mypassword"); //(host, username, password)

//specify database
mysql_select_db("mydb") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from product where type like \"%$trimmed%\" or product_name like \"%$trimmed%\" or product_code like \"%$trimmed%\"
order by product_name";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);

// If we have no results, offer a google search as an alternative

if ($numrows == 0)
{
echo "<h4>Results</h4>";
echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";

// google
echo "<p><a href=\"http://www.google.com/search?q="
. $trimmed . "\" target=\"_blank\" title=\"Look up
" . $trimmed . " on Google\">Click here</a> to try the
search on google</p>";
}

// next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}

// get results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die("Couldn't execute query");

// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";

// begin to show results set
echo "Results <br>";
$count = 1 + $s ;

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$product_code = $row["product_code"];
$type = $row["type"];
$product_name = $row["product_name"];
echo " &nbsp <b> $product_code " ;
echo " &nbsp $product_name </b><br>";
echo "Product Type: &nbsp <b> $type </b><br>";
$count++ ;
}
$currPage = (($s/$limit) + 1);

//break before paging
echo "<br />";

// next we need to do the links to other results
if ($s>=1) { // bypass PREV link if s is 0
$prevs=($s-$limit);
print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
Prev </a>&nbsp&nbsp;";
}

// calculate number of pages needing links
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}

// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

// not last page so give NEXT link
$news=$s+$limit;

echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next &gt;&gt;</a>";
}

$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing result $b of $numrows</p>";

?>

<p align="center"><font size="1" face="Arial, Helvetica, sans-serif" color="#330099"><a href="main.htm">HOME</a>
¦ <a href="products-main.htm">PRODUCTS</a> ¦ <a href="search2.php">SEARCH</a>
¦ <a href="FAQ.php">FAQ</a> ¦ <a href="help.php">HELP</a>
¦ <a href="login.php">LOGIN</a> ¦ <a href="links.html">LINKS</a> ¦ <a href="contact_details.html">CONTACT</a></font></p>
<p align="center"><font face="Arial, Helvetica, sans-serif" size="1" color="#330099">Copyright
2003 All Rights Reserved!</font><font face="Arial, Helvetica, sans-serif" size="2" color="#330099"><br>
<font size="1">
</a></font></font></p>
</body>
</html>

[edited by: jatar_k at 5:39 pm (utc) on April 13, 2003]
[edit reason] fixed double paste [/edit]

wruk999

12:31 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



hi,

can you paste in the error that you are receiving (with line number).

i can't see a text box or area on your php code, the only one being above the php code as the search box.

where and what is the purpose of the text box in the php script?

lastly, have you tried having the php script on a different page?

wruk999

12:36 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



try changing this part:

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$product_code = $row['product_code']; //use a ' instead of "
$type = $row['type']; //use a ' instead of "
$product_name = $row['product_name']; //use a ' instead of "

} //close this here.

echo " &nbsp <b> $product_code " ;
echo " &nbsp $product_name </b><br>";
echo "Product Type: &nbsp <b> $type </b><br>";
$count++ ;

let me know if this works

<edit>formatted it better</edit>

imagus

2:15 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



the script I've posted is my original script without any text boxes for the results.
It doesn't seem to matter where I place the code for a text box
eg:

<input type="text" name="product_code" value="<?php echo $product_code;?>">
<textarea name="example" rows="10" cols="35"><?php echo $example;?></textarea>

I get a parse error wherever I place either of the above.
I just want each variable to appear in it's own text box, like an auro filled form

thanx for your help so far

wruk999

3:41 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



if you are trying to put the boxes _inside_ the PHP code, then you need to put a \ before the "

for example:

<input type="text" name="product_code" value="<?php echo $product_code;?>">
<textarea name="example" rows="10" cols="35"><?php echo $example;?></textarea>

in PHP would become:

<input type=\"text\" name=\"product_code\" value=\"<?php echo $product_code;?>\">

<textarea name=\"example\" rows=\"10\" cols=\"35\"><?php echo $example;?></textarea>

does this help?
if not...maybe jatar_k, andreas or Birdman maybe able to help ;)

Regards,
william.

willybfriendly

4:19 pm on Apr 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is a LOT of code to read through.

What exactly is the error you're getting?

Also, I find it much easier to follow code, and to debug, if I break out as much of it as possible into functions. (Makes it reusable too) Then you are just trying to work on a small piece of code rather than a whole line. Makes it easier to find those pesky missing semi's and quotes :) Also, you can then echo whatever is returned from the function to help follow the program flow and debug. This has helped me to find many errors in logic that otherwise had me baffled.

WBF

wruk999

4:25 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



As I put in msg#7, and WBF just backed up, it will be easier to help out if you can post the exact error you are getting.

This will help determine which section (function) of your code is not working correctly. ;)

Regards,
wruk999

imagus

4:41 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



sorry guys,

there was no error in the code I posted. I just wanted to return the results from that code in boxes.

thanx William because of your help it now does that.

Your patience is muchly appreciated

imagus :)

wruk999

4:54 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



it's all working as you want it?

Excellent. well done!
:)

imagus

5:10 pm on Apr 13, 2003 (gmt 0)

10+ Year Member



yup
thanx again.

I am well happy
:)