★ wanayoo — archive 1999 http://www.devshed.com/Server_Side/PHP/User/Nouvelle recherche | Portail wanayoo

Developer Shed
Tuesday, 08.15.2000  
Often DevShed users write their own tutorials that they want to share with the world.  See what tutorials users like youself have written...you can also use DevShed's comments feature to let the author's know how they can improve their tutorials. To submit your own article write <a href=/old?u=http%3A%2F%2Fwww.devshed.com%2FServer_Side%2FPHP%2FUser%2F%26quot%3Bmailto%3Awebmaster%40devshed.com%26quot%3B%26gt%3Bwebmaster%40devshed.com%26lt%3B%2Fa%26gt%3B.&y=1999

HOME > SERVER SIDE > PHP > ARTICLE    PHP SOURCE

Power Search
Free Newsletter:


Client Side
DHTML
Flash
Graphics
JavaScript
XML

Server Side
Administration
JServ
MySQL
Perl
PHP
Python
Roxen
Zend
Zope

Tools
HTML Editors
Graphic Editors
FTP Programs

Dev Talk
Forums
Mailing List
Book Samples
Brain Dump
Developer News
Feedback

ClipScripts
Add script
Edit script

Other Stuff
DevShed RDF
Propaganda!


Advertise
on DevShed
.
User-Contributed: PHP, Mysql, and Images
August 03, 1999

PHP, MySQL, and Images
by Willim Samplonius

Joe's used auto sales, an average sized car yard of around 150 cars has decided to make the big leap into the new millennium. First of all they had to get themselves a nice new computer, with that new computer came the "Internet". After a few weeks of searching the internet they decided to go all out, now they wanted a web site of their own. And it just so happens that Joe's good friend knows a little HTML, three days later Joe's used auto sales was online and ready for business.

It wasn't long before they realized they were not going to get very far with that site. They needed something where they could add, modify and delete listings in a database. Well, to make a long story short they ended up contacting me. And of course I said I could do it. So, that day I had to find out what type of database to use. After asking a few questions I decided to create a database using PHP and MySQL

I didn't know how to create an actual MySQL database, so I contacted support at my hosting company, next thing I know I had a database called "joesauto" and a few basic Telnet and MySQL commands.

Now I needed to open up telnet for the first time and enter my login name and password. Before I could go any further I had to learn a little about MySQL.

OK, now that we all know the basics, its time to connect to the server. If you have any problems email support, they like answering these questions.


shell> mysql -h localhost -u joesauto -p Enter password: ******** Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 459 to server version: 3.22.20a-log Type 'help' for help. mysql>

After successfully connecting to the server we need to access the database (joesauto) that I got support to create for me. If you don't have a database yet now is the time to create one. To see what databases are on this server type show databases; if your database is there select it by typing use joesauto if you can't access the database contact support. This tutorial could take days if your support is slow.


mysql> use joesauto Database changed mysql>

Ok, after a lot of reading I figured out how to create a table for all the important data (year, make, model, price and picture), the picture part took alot of time to figure out, I found out that it was best to just store the picture filename in the database and store the actual picture in a specified directory on my server. Below is what I used to create the table. (I am sure there are other ways of creating this table but, this is what worked for me.)


mysql> CREATE TABLE joesauto(    -> year INT(4),    -> make CHAR(20),    -> model CHAR(20),    -> price CHAR(15),    -> picture_name CHAR(25),    -> );

At this stage we don't need to worry about loading data into the table, we will do that next using PHP and simple HTML forms. You can shut down Telnet now and open up your favorite HTML editor, I would recommend Homesite 4.0.

Now it was time for me to learn the PHP basics, which wasn't that difficult, then I subscribed to the PHP mailing list and took a quick look through the PHP manual. If you can try to find somebody on ICQ that knows PHP, it sure does help having instant support.

The idea here is to be able to insert all the data into the database via the internet using forms, I have created forms several times before so that was no problem at all. I am assuming that you have used forms before, if you have no idea how to create forms, find a tutorial on the net and come back when your ready.

Create a page and call it add_data.php3, and then cut and paste the following.

add_data.php3

<form enctype="multipart/form-data" method="post" action="<?php echo $PHP_SELF ?>"> year <input type="Text" name="year" size="25"> make <input type="Text" name="make" size="25"> model <input type="Text" name="model" size="25"> price <input type="Text" name="price" size="25"> picture <input type="File" name="picture" size="25"> <input type="submit" name="submit" value="Upload"> </form>
First of all we need to add a little PHP in the action attribute, this is used to display all the data once the submit button is pressed.

action="<?php echo $PHP_SELF ?>
The name attribute within the input element will be used to pass the information to PHP, these names should be the same as the columns in the database.

Now its time to add the stuff that makes it all work. I can't really explain what we are using below, so I suggest you go to the PHP website and study the main elements used below, mysql_connect() and mysql_select_db(). It may be a good idea to go to the MySQL site and learn how to insert, delete, modify and anything else you want to fill your brain with.

<?php if ($submit) { $db = mysql_connect("$localhost","$joesauto","$password"); mysql_select_db("$joesauto",$db); $sql = "INSERT INTO joesauto (year,make,model,price,picture_name) VALUES ('$year,$make,$model,$price,$picture_name')"; exec("cp $picture /full/path/to/joesauto/images/$picture_name"); echo "year: $year<br>\n"; echo "make: $make<br>\n"; echo "model: $model<br>\n"; echo "price: $price<br>\n"; echo "temp file: $picture<br>\n"; echo "file name: $picture_name<br>\n"; echo "file size: $picture_size<br>\n"; echo "file type: $picture_type<br>\n"; echo "<br>n"; echo "<img src=/old?u=http%3A%2F%2Fwww.devshed.com%2FServer_Side%2FPHP%2FUser%2Fimages%2F%24picture_name&y=1999><br>\n"; } ?>
After you add the above code to the add_data.php3 page, (right below the closing form tag would work great) you need to change a few things to work with your database. It should all be pretty straight forward, you just need to change $your_username, $your_password and you'll need to specify the full path to where your images directory is. You also need to make sure that the image directory is CHMOD 777.

Now everything should be set and your ready to upload it to the server to see if everything works. The next lesson in this tutorial will show you how to view the data, and I will show you how to do more stuff in lessons to follow.


My name is William Samplonius and I am web designer for e n d u r a d e z i n e. I hope this helped you, don't email me your questions because I know just as much as you do.


What do you think?
Click to post your comments
. 82 comments.


Click here to receive updates on the latest articles.



Visit the PHP Forum!

Printer friendly versionPrinter friendly version

New On DevShed:
• PHP 101 (part 3) - Chocolate Fudge And Time Machines
• MaxSQL Anounced
•  PHP 101 (Part 2) - Shakespeare's Rose
• PHP 101 (Part 1) - Secret Agent Man
• The Search For The Perfect Web Host
• Perl 101 (Part 5) - Sub-Zero Code
• Beginning Perl
•  Submit Your News..

Hot Forum Topics:
• help with table data row
• Default page
• Sending passwords non plaintext
• Can php do this?
• Can php do this?
• PHP & MySQL Host


Adsmart Network ad
Copyright © 1997-2000 ngenuity. All rights reserved. Privacy Policy.