FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


printing out using PHP?





aningbo
i'm looking for a script that enters some data like name, email, phone, address which is pretty much the same as any normal guestbook.

but the difference being, it should be able to search for anything in the database (say the name) and "print" that specific profile.

have you come across any such scripts? plz do let me know.
thanx.
rickylau
Do you mean send a page of record to user's printer? I don't think it (sending to printer) can be accomplished by PHP since PHP do the works on server but not client. But there is a method print() in JavaScript window object. You can just use PHP to query the database and display the page of record as usual, and call JavaScript method print() when ready.
cr3ativ3
Code:

<?php
// Connect to database server, and select database.
mysql_connect('localhost', 'mysqlUsername', 'mysqlPassword');
mysql_select_db('mysqlDbName');

// Query the databse for a profile, using the name given to us.
$query = mysql_query("SELECT * FROM `profiles` WHERE `name` = '".$_POST['name']."'");
$row = mysql_fetch_array($query);

// Loop through all the columns in row returned.
foreach ($row as $key => $value)
{
     echo $key . ": " . $value . "<br />";
}
exit;
?>
<form method="post" action="">
<input type="text" name="name" />
</form>



That script I quickly typed up for you, does a few things the HTML form at the bottom. Takes a value in this instance "name". It posts it to the same page, where the PHP at the top, connects to the database, using your credentials (you'll need to replace mysqlUsername, mysqlPassword, mysqlDbName with your appropriate values). Then it queries the database, searching the profiles table, which you may also need to change to the appropriate table name. Checks for a row with a column of name matching the name that the user entered into the form. Then it loops through the row that was returned, displaying all the information stored in that profile.

Hope this helps!

Edit: This is not a very secure solution, alot more things should be added to make it secure from people attacking it.
rvec
you might want to use a LIKE %...% in your sql for a search function, and you might ant to put an if(isset($_POST['name'])) around the whole php part (to not search and to not exit when no search is given).

And of course sanitize the data like cr3ativ3 said. This should be done before using the data to search and before displaying the data from the database. I wouldn't clear out all the html before sending data to the database, only before it needs to be displayed.
cr3ativ3
rvec wrote:
you might want to use a LIKE %...% in your sql for a search function, and you might ant to put an if(isset($_POST['name'])) around the whole php part (to not search and to not exit when no search is given).

And of course sanitize the data like cr3ativ3 said. This should be done before using the data to search and before displaying the data from the database. I wouldn't clear out all the html before sending data to the database, only before it needs to be displayed.


Good point on the if isset part, usually I always add that but in this case forgot. I c that this script would quickly display nothing if you were to run it Razz
Related topics
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
Checking if ports are up using PHP
Hot to change "Reset Ownership" using php?
Using php to change content of a .txt
How to get the tree structure done using PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.