Solved

php and data base

Posted on 2003-11-11
2
274 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
2 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 20 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now