在mysqli错误中插入php表单数据

时间:2018-05-21 21:46:55

标签: php mysqli

我正在尝试在SQL中创建PHP表单数据插件但是收到错误。 即使我编写相同的代码,但我仍然会收到错误。

<?php

    $un = $_POST['uname'];
    $em = $_POST['email1'];

//with or what out these bellow variables
    $host = "localhost";
   $username = "admin";
   $password = "admin";
   $database = "test1";

    $db = mysqli_connect('$host','$username','$password','$database');
    $query = "INSERT INTO users ('username','password') VALUES ('$un','$em')";
    $rs = mysqli_query($db,$query);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Registration</title>
</head>
<body>
    <form action="server1.php" method="post">
        <label>Name</label>
        <input type="text" name="uname" required="required">
        <label>Email</label>
        <input type="email" name="email1" required="required">
        <input type="submit" name="submit" value="submit">
    </form>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

错误是&#34;&#34;只是倒置了逗号,现在它完美无缺。

     <?php
        $un = $_POST['uname'];
        $em = $_POST['email1'];

        $host = "localhost";
        $username = "admin";
        $password = "admin";
        $database = "test1";

            $con = mysqli_connect ("$host","$username","$password","$database");
            $query = "insert into users (username,email) values ('$un','$em')";
            $run = mysqli_query ($con,$query);

            if ($run=TRUE){
                echo 'Data Submitted Successfuly';
            }
            else {
                echo 'Error';
            }

    ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Form Registration</title>
</head>
<body>
    <form action="server1.php" method="post">
        <label>Name</label>
        <input type="text" name="uname" required="required">
        <label>Email</label>
        <input type="email" name="email1" required="required">
        <input type="submit" name="submit" value="submit">
    </form>

</body>
</html>

答案 1 :(得分:0)

你可以这样试试。

$db = mysqli_connect($host, $username, $password) or die ('Unable to connect');
mysqli_select_db($db, $database) or die('Unable to select DB');