View Full Version : Help with inserting data with php forms into DB
Doggonit
08-18-2005, 10:42 PM
I am using WAMP and I have these two files in my C:\wamp\www\test folder (wrong location?)
First: Contacts.html
<form action="insert.php" method="post">
First Name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
Phone: <input type="text" name="phone"><br>
Mobile: <input type="text" name="mobile"><br>
Fax: <input type="text" name="fax"><br>
E-mail: <input type="text" name="email"><br>
Web: <input type="text" name="web"><br>
<input type="Submit">
</form>
Second: insert.php
<?
$username="XXXXXXX";
$password="XXXXXXXXXX";
$database="test";
$first=$_POST['first'];
$last=$_POST['last'];
$phone=$_POST['phone'];
$mobile=$_POST['mobile'];
$fax=$_POST['fax'];
$email=$_POST['email'];
$web=$_POST['web'];
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query);
mysql_close();
?>
Now, my problem is that first of all, I would like the data that is written in the form to go into the existsing "contacts" table WITHIN the "test" DB in the MySQL server, how do I specify that?
Next, are the two files, Contacts.html and insert.php in the right place in the file system?
Last, but not least, when I try the html page and click submit I am asked if I want to save the insert.php file or not... what the hell? I thought the script was supposed to just pop the stuff in the DB and that's it. I thought insert.php is just called on and utilized, not overwritten or whatever the hell this trash heap is trying to do.
I feel like a total idiot with MySQL and PHP.
Hi Doggonit,
first of all: your setup. WAMP means Windows, Apache, MySQL, PHP. This is a commonly used term, so there are many downloads combining everything you need. It seems everything is installed. Most of the times, a special folder called 'ht_docs' or 'public_html' is created with the install. This is the folder you should put you *.php files in. What happens in your case, is the browser spitting out a php file. But the browser does not know php, so it asks you if you want to save it.
This indicates a php file being in the wrong folder. You should read the manual that came with your WAMP package to see where to put your php files.
Also, with a package, Apache and MySQL can be installed as services. If you decided so (during the setup), you should be sure the right services have started with Windows. Maybe your package came with a control-screen that allows you to view the status of your services. Both Apache and MySQL should be started. If not, try the following:
Click Start, Run: ( or hit Windows + R)
type 'services.msc' and hit Enter
Both services should be started. If not so, choose to 'Start Service'.
If both services are started, point your browser to:
'http://localhost/'
This should work. If not, re-view your setup.
Second comment: as soon as you located the right directory, i suggest you try a phpinfo file. Create a new file called phpinfo.php and fill it with this:
<?php
phpinfo();
?>
This gives you a lot of info about your setup. For instance, if 'short_open_tag' is set to 'on'. If so, you can start your phpfiles with '<?'. If not, you should start your php files with '<?php' or they won't work.
About the right directory: once you figured out where to put your .php files, you can put both of your files in the same directory and it should work out just fine. Your code can be improved, but for now it should work.
Good luck!
Doggonit
08-19-2005, 04:10 PM
Hi Doggonit,
first of all: your setup. WAMP means Windows, Apache, MySQL, PHP. This is a commonly used term, so there are many downloads combining everything you need. It seems everything is installed. Most of the times, a special folder called 'ht_docs' or 'public_html' is created with the install. This is the folder you should put you *.php files in. What happens in your case, is the browser spitting out a php file. But the browser does not know php, so it asks you if you want to save it.
This indicates a php file being in the wrong folder. You should read the manual that came with your WAMP package to see where to put your php files.
Also, with a package, Apache and MySQL can be installed as services. If you decided so (during the setup), you should be sure the right services have started with Windows. Maybe your package came with a control-screen that allows you to view the status of your services. Both Apache and MySQL should be started. If not, try the following:
Click Start, Run: ( or hit Windows + R)
type 'services.msc' and hit Enter
Both services should be started. If not so, choose to 'Start Service'.
If both services are started, point your browser to:
'http://localhost/'
This should work. If not, re-view your setup.
Second comment: as soon as you located the right directory, i suggest you try a phpinfo file. Create a new file called phpinfo.php and fill it with this:
<?php
phpinfo();
?>
This gives you a lot of info about your setup. For instance, if 'short_open_tag' is set to 'on'. If so, you can start your phpfiles with '<?'. If not, you should start your php files with '<?php' or they won't work.
About the right directory: once you figured out where to put your .php files, you can put both of your files in the same directory and it should work out just fine. Your code can be improved, but for now it should work.
Good luck!
Yeah, I know how to start both services, in fact, they are always on, so no worries about that. I have gotten other pages to work with this setup but for some reason this won't and I got very unsure about everything that I did in the past.
My question is why the html file isn't using the php file as it is supposed to, or is it? Why is it asking me if I want to save it rather than just use the damn code and get the hell on with it.
Also, I asked in specific about how I ca designate WHICH table in a certain DB to put the data into. Because as you know there usually are multiple tables in DB's...
[QUOTE=Doggonit]...
My question is why the html file isn't using the php file as it is supposed to, or is it?
Also, I asked in specific about how I ca designate WHICH table in a certain DB to put the data into....[QUOTE]
First of all: good to hear your services are always on. Now, we can eliminate a lot of possibilities.
Let me start with your second remark. in your start post, you stated the following:
$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query);
It's not that hard: the query start with 'INSERT INTO'. Guess what comes next... your tablename. See, it wasn't that hard?
Now for the first remark. How do you point your browser to the contacts.html ?
There are two possibilities:
1: You are using "c:\documents and settings\your folder\your subfolder\contacts.html"
2: You are using "http://localhost/contacts.html".
Be sure to use number 2! Your server can only parse your .php file with the address "http://localhost/..." or "http://your_computer_name/...", but NOT the documents and settings stuff.
Second: you say you have used the same setup before. So i presume your server knows the .php extension? Have you recently reinstalled and forgotten to add it to your httpd.conf maybe?
Let me know,
Jaap
vBulletin® v3.8.2, Copyright ©2000-2009, Jelsoft Enterprises Ltd.