Mysql
 sql >> Datenbank >  >> RDS >> Mysql

So fügen Sie die Textfeldwerte in die Datenbank mysql ein, indem Sie das PHP-Oops-Konzept verwenden, wenn Sie auf die Schaltfläche „Senden“ klicken

Dies ist das HTML-Formular

<body>
<form action="process.php" method="post">
Name : <input type ="text" name = "Name"/>

Number  :<input type ="text" name = "Number"/>
<input type ="submit" value = "submit" name="submit"/>

</form>
</body>

Diese PHP-Datei, die die Klasse enthält, heißt db.php

<?php
class db
{
    public $host;
    public $user;
    public $pass;
    public $data;
    public $con;
    public $table;
    function db()
    {
        $this->host="localhost";
        $this->user="usern";
        $this->pass="passwrd";
        $this->data="dbname";   
    }   
    public function connect()
    {
        $this->con=mysql_connect($this->host,$this->user,$this->pass);
        if(!$this->con)
        {
            echo mysql_error();
        }
        $sel=mysql_select_db($this->data, $this->con);
        if(!$sel)
        {
            echo mysql_error();
        }
    }
    public function insert($name,$number)
    {
        $sql=mysql_query("INSERT INTO tablename(name, number) VALUES('$name', '$number')");
        if(!$sql)
        {
            echo mysql_error();
        }
    }
}
?>

Dieses Skript ist für die PHP-Datei, die Sie im "action"-Attribut Ihres HTML-Formulars angeben. Ich habe es "process.php" genannt.

<?php
    include'db.php';
    $name=$_POST['Name'];
    $num=$_POST['Number'];
    $n=new db();
    $n->connect();
    $n->insert($name,$num);
?>