Solved

php and data base

Posted on 2003-11-11
2
293 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
The purpose of this video is to demonstrate how to manually back up a WordPress Database. This will be demonstrated using a Windows 8 PC. The Host used will be IPage.com Log into your Hosting account. IPage will be used for demonstration : Locat…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

617 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