Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

php and data base

Posted on 2003-11-11
2
Medium Priority
?
297 Views
Last Modified: 2013-12-16
hai !!
i'm a novice in web programming , i wanted to build a html form so that the user can input his data and i can store his request in the data base . i have tried using php for catching the variables from the form and  insert these values into my sql database but i was unsuccessful in doing that . can anyone please help me ..here is what i have done

form.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>FORM</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="my.html">
  <p>ID:
    <input name="id" type="text" id="id">
  </p>
  <p>FIRST NAME:
    <input name="first_name" type="text" id="first_name">
  </p>
  <p>LAST NAME:
    <input name="surname" type="text" id="surname">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
    <input name="reset" type="reset">
  </p>
</form>

</body>
</html>

my.html

<html>
      <head>
            <title>Welcome to Adobe GoLive 4</title>
      </head>
      
<?php
      $username = "root";
      $password = "root";
      $hostname = "localhost";      
      //$dbh = mysql_connect($hostname, $username, $password) or

die("Unable to connect to MySQL");
      //print "Connected to MySQL<br>";
      // you're going to do lots more here soon


$id = $HTTP_GET_VARS["id"];
$first_name = $HTTP_GET_VARS["fist_name"];
$surname = $HTTP_GET_VARS["surname"] ;
$selected = mysql_select_db("first_test",$dbh) or die("Could not

select first_test");
//      if (mysql_query("insert into people

values('$id','$first_name','$surname')")) {
        //print "successfully inserted record";
//      }
//      else {
         //print "Failed to insert record";
//      }
//mysql_close($dbh);
print(" id: {0},firstname {1},surname{2}",$id,$first_name,$surname");

?>
<body>
<p> thank you </p>
      </body>
</html>


i also have a database "first_test" with id , first_name and surname as the attributes in the table
people
...i don't know what i'm doing wrong ...any help will be grealty appreciated
thank you

raghu
0
Comment
Question by:Raghunandan44
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 80 total points
ID: 9725189
first you have commented out a lot of the code - that's what // means so for example the mysql_connect isn't being executed. secondly, in order for your php file to be processed, it needs to have the extension .php. also you've made a typo with $HTTP_GET_VARS["fist_name"]. in your form, your method action is "POST", which means that you are sending the information via POST data which is stored in $HTTP_POST_VARS (or $_POST past version 4.1.0). so you need to either change your method to GET in the form or change your variables to $HTTP_POST_VARS FROM $HTTP_GET_VARS. incidentally, if you php version is higher than 4.1.0, it's best to use $_POST, $_GET, $_SESSION etc. instead of $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_SESSION_VARS etc. here's how your my.php should look like, you'll need to change the action of the form to point to my.php instead of my.html

<html>
     <head>
          <title>Welcome to Adobe GoLive 4</title>
     </head>
     
<?php
     $username = "root";
     $password = "root";
     $hostname = "localhost";    
     $dbh = mysql_connect($hostname, $username, $password) or

die("Unable to connect to MySQL");
     //print "Connected to MySQL<br>";
     // you're going to do lots more here soon


$id = $HTTP_GET_VARS["id"];
$first_name = $HTTP_GET_VARS["first_name"];
$surname = $HTTP_GET_VARS["surname"] ;
$selected = mysql_select_db("first_test",$dbh) or die("Could not select first_test");
     if (mysql_query("insert into people values('$id','$first_name','$surname')")) {
       print "successfully inserted record";
     }
     else {
        print "Failed to insert record";
     }
mysql_close($dbh);
print(" id: {0},firstname {1},surname{2}",$id,$first_name,$surname");

?>
<body>
<p> thank you </p>
     </body>
</html>

also, you need to change the three variableshere to the appropriate parameters to connect to your database:

     $username = "root";
     $password = "root";
     $hostname = "localhost";

tell me how it goes - hope you're not too confused by that, feel free to ask any more questions

loz
0
 

Author Comment

by:Raghunandan44
ID: 9727190
hai loz...

thanx a lot ..i have been able to do what i wanted ..thanx  a lot once again ..

raghu
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In this tutorial, I'll explain how to create an animated progress meter in a wireframe prototype developed using Axure RP 7.0 - a leading prototyping tool for designing web sites and software. (For more information about Axure and gett…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question