如何将 HTML 表单连接到数据库 SQL


how to connect a html form to a database sql

im将表单连接到数据库,但PHP正在为问题提供任何建议来帮助我的脚本

                 <?php
              $connection = mysql_connect("localhost", "root", "");
               $db = mysql_select_db("hot_data", $connection);

            if(isset($_POST['submit']))
            {

                   $userid=_$POST['userid'];
                    $first_name=$_POST['fname'];
               $last_name=$_POST['lname'];
               $livingadress=$_POST['ldress'];
                 $telephone=$_POST['phone'];
                  if($userid !=''||$telephone !=''){
//Insert Query of SQL
                    $query = mysql_query("INSERT INTO personal_data(userid,first_name,last_name,living_address,telephone)

                   VALUES('$userid','$first_name','$last_name','$livingaddress','$telephone')");
              echo "<br/><br/><span>Data Inserted successfully...!!</span>";
              }
            else{
                 echo "<p>Insertion Failed <br/> Some Fields are Blank....!! </p>";
                        }
                     }
                           mysql_close($connection);
                  ?>
<?php
try{
    $servername = "localhost";
    $username = "username";
    $password = "password";
    // Create connection
    $conn = new mysqli($servername, $username, $password);
    // Check connection
    if ($conn->connect_error) {
        throw new Exception("Connection failed: " . $conn->connect_error);
    }
    if (mysql_select_db("hot_data")){
    if(isset($_POST['submit']))
                {
                     $userid=$_POST['userid'];
                        $first_name=$_POST['fname'];
                   $last_name=$_POST['lname'];
                   $livingadress=$_POST['ldress'];
                     $telephone=$_POST['phone'];
                      if($userid !=''||$telephone !=''){
    //Insert Query of SQL
                        $query = mysql_query("INSERT INTO personal_data(userid,first_name,last_name,living_address,telephone)
     VALUES('{$userid}','{$first_name}','{$last_name}','{$livingaddress}','{$telephone}')");
                    throw new Exception("Data Inserted successfully...!!");
                  }
                else{
                    throw new Exception("Insertion Failed...!!Some Fields are Blank....!!");
                            }
                         }
    }else {throw new Exception("Could not find database....!! ");}
}catch(exception $ex)
{   echo $ex;
    }
?>

你在这一行有一个错误 -

$userid=_$POST['userid'];

应该是这个——

$userid=$_POST['userid']; // $_POST variable was improperly written