Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ?

share|improve this question
What is OOPS? – lanzz Jun 6 '12 at 6:52
@lanzz I'm sorry . Its OBJECT ORIENTED PROGRAMMING – Jathin Jun 6 '12 at 6:54
Where do you get the S from? :D – Niko Jun 6 '12 at 6:55
@Niko Maybe S stands for SUBJECT :D ;) – Jathin Jun 6 '12 at 6:58

closed as not a real question by Michael Petrotta, lanzz, Niko, AVD, Graviton Jun 6 '12 at 7:16

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

up vote 3 down vote accepted

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.

share|improve this answer
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 – Jathin Jun 6 '12 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 ;) – Ayush Chaudhary Jun 6 '12 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 :) – Jathin Jun 6 '12 at 7:07
That will work now, since you know the issue was case-sensitivity and not some fault in OOP concept. – Ayush Chaudhary Jun 6 '12 at 7:09
Yes , It worked :) – Jathin Jun 6 '12 at 7:13

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());
}

?>
share|improve this answer
Thank you . It worked perfectly fine . I really appreciate your help . – Jathin Jun 6 '12 at 7:04
ya its work perfect...good work and try to give best of you..thanks. – pratik Oct 20 '12 at 11:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.