PHP Problem Inserting HTML form

Problem Inserting HTML form data with php

Page 1 of 1

6 Replies - 1790 Views - Last Post: 13 December 2007 - 10:48 PM Rate Topic: -----

#1 dv_evan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-December 07

PHP Problem Inserting HTML form

Posted 12 December 2007 - 08:28 AM

Dear All,
I am new to PHP, I am following an online tutorial in developing an html form to submit data to a php file to insert into a database.
This problem is when I click submit on the form it brings up the php codes in the browser and nothing is inserted into the database.

Here are my codes for the HTML form and the insert.php, both are stored in the htdoc folder.


 
<html>

<head>

</head>

<body>
<form action="insert.php" method="post">
First Name: <input type="text" name="first" size="20"><br>
Last Name: <input type="text" name="last" size="20"><br>
Phone: <input type="text" name="phone" size="20"><br>
Mobile: <input type="text" name="mobile" size="20"><br>
Fax: <input type="text" name="fax" size="20"><br>
E-mail: <input type="text" name="email" size="20"><br>
Web: <input type="text" name="web" size="20"><br>
<input type="Submit">
</form>



</body>

</html>








<?
$username="username";
$password="password";
$database="db";
$table= "aTable";

$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 $table VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query);

mysql_close();
?>




any help will be greatful, thanks!
Dave

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Problem Inserting HTML form

#2 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 84
  • View blog
  • Posts: 3,564
  • Joined: 31-March 03

Re: PHP Problem Inserting HTML form

Posted 12 December 2007 - 08:45 AM

From what you're saying it sounds like your host doesn't support PHP.
Was This Post Helpful? 0
  • +
  • -

#3 dontKnowJava  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 221
  • Joined: 29-September 07

Re: PHP Problem Inserting HTML form

Posted 12 December 2007 - 08:47 AM

maybe your server is setup with a different extension something like .php4. check with your hosting company. just a few tips on php its a good idea to verify info submitted before inserting it into your table. only allow users to enter a-z and numbers. cause some idiots can put in scripts into ur db and mess it up.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5380
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: PHP Problem Inserting HTML form

Posted 12 December 2007 - 08:51 AM

Some things that I would check
1.) Is insert.php in the same directory as your html code?
2.) Does the database exist?
3.) Can you perform manually, what you are trying to accomplish in the php code?
Was This Post Helpful? 0
  • +
  • -

#5 dv_evan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-December 07

Re: PHP Problem Inserting HTML form

Posted 12 December 2007 - 09:23 AM

Thanks guys for your responses.

For your information:
when I tested other php codes such as phpinfo.php or a echo helloworld, these php codes worked.

Both the html form and the php code file are stored in the same directory.

I am running php/Mysql on my PC (localhost) with apache, I checked the config file apache and it has the correct AddType for .php.

What else can I do?

thanks
Dave
Was This Post Helpful? 0
  • +
  • -

#6 DilutedImage  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 14
  • View blog
  • Posts: 646
  • Joined: 20-November 06

Re: PHP Problem Inserting HTML form

Posted 12 December 2007 - 01:03 PM

- Ensure that you're accessing it via Apache (http://127.0.0.1/targetFile.php)
- Try replacing "<?" with "<?php", in case your system is not properly configured for the shorthand.
Was This Post Helpful? 0
  • +
  • -

#7 TheKing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 13-December 07

Re: PHP Problem Inserting HTML form

Posted 13 December 2007 - 10:48 PM

Try this all on one page:



<?php
if(isset($_POST['submit']))
{
$username="username";
$password="password";
$database="db";
$table= "aTable";

$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 $table VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query);

mysql_close();
}
?>

<html>
<head>
</head>
<body>
<form action="<? echo $_SERVER["REQUEST_URI"] ?>" method="post">
First Name: <input type="text" name="first" size="20" /><br />
Last Name: <input type="text" name="last" size="20 /"><br />
Phone: <input type="text" name="phone" size="20" /><br />
Mobile: <input type="text" name="mobile" size="20" /><br />
Fax: <input type="text" name="fax" size="20" /><br />
E-mail: <input type="text" name="email" size="20" /><br />
Web: <input type="text" name="web" size="20" /><br />
<input type="submit" name="submit" value=" Submit" />
</form>

</body>

</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1