0

I am very new to PHP . I was asked to create a very simple form in PHP using OOPS . I managed to create a single page form with 2 text fields (name and ID) but the issue I am facing now is that when user clicks on submit button only the name gets stored in the database but not the ID . Could you please help me out in this . Below is the sample code Thank You in advance

PERSON.PHP

<?php
class person
{
    var $id;
    var $name;
    function set_id($new_id)
    {
        $this->id=$new_id;
    }

    function get_id()
    {
        return $this->id;
    }

    function set_name($new_name)
    {
        $this->name=$new_name;
    }

    function get_name()
    {
        return $this->name;
    }
}
?>

DBINPUT.PHP

 <?php
 include 'person.php';
 ?>
 <html>
 <body>
 <form action="dbinsert.php" method="post">
  NAME<input type="text" name="name" >/
  ID<input type="text" name="ID" />
  <input type="submit" value="submit">
  </form>
  </html>
    </body>

DBINSERT.PHP

 <?php
    include("person.php");
    $con = mysql_connect("localhost", "cgiadmin", "cgi");
    if (!$con)
    {
        die("couldn't connect ".mysql_error());
    }
    mysql_select_db("oops", $con);

    $person = new person;
    $person->set_name($_POST['name']);

    $person1 = new person;
    $person1->set_id($_POST['id']);


    $sql="INSERT INTO smallprogramusingoops (NAME ,ID ) VALUES ('".$person->get_name()."','".$person1->get_id()."')";
    if (!mysql_query($sql, $con))
    {
    die('Error in inserting '.mysql_error());
    }

    ?>

Could you please rectify my mistake ?

3
  • @lanzz I'm sorry . Its OBJECT ORIENTED PROGRAMMING Commented Jun 6, 2012 at 6:54
  • 1
    Where do you get the S from? :D Commented Jun 6, 2012 at 6:55
  • @Niko Maybe S stands for SUBJECT :D ;) Commented Jun 6, 2012 at 6:58

2 Answers 2

3

The name attribute of your "id" field is in Block letters, while, when you access it via dbinsert.php, you use "id" instead of "ID".. Variables/indexes are case sensitive.

4
  • Ahh I knew I was making a silly mistake . Thank you very much Ayush . It worked perfectly fine . Now I can go ahead and create a few more pages . Thank you Commented Jun 6, 2012 at 7:00
  • Also.. use single object for both the data members instead of creating two different objects for the two members. OOP would make more sense then ;) Commented Jun 6, 2012 at 7:02
  • Frankly speaking , that is what I did in the first place , but that did not seem to work so I tried creating two objects .Thanks Ayush :) Commented Jun 6, 2012 at 7:07
  • That will work now, since you know the issue was case-sensitivity and not some fault in OOP concept. Commented Jun 6, 2012 at 7:09
-1

You create two objects. Not sure how PHP handles that, it might overwrite your existing variables.

Testrun this code:

<?php
include("person.php");
$con = mysql_connect("localhost", "cgiadmin", "cgi");
if (!$con)
{
    die("couldn't connect ".mysql_error());
}
mysql_select_db("oops", $con);

$person = new person();
$person->set_name($_POST['name']);
$person1->set_id($_POST['id']);


$sql="INSERT INTO smallprogramusingoops (NAME ,ID ) VALUES ('".$person->get_name()."','".$person1->get_id()."')";
if (!mysql_query($sql, $con))
{
die('Error in inserting '.mysql_error());
}

?>
1
  • ya its work perfect...good work and try to give best of you..thanks. Commented Oct 20, 2012 at 11:28

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.