【问题标题】:Not able to enter data in database [duplicate]无法在数据库中输入数据[重复]
【发布时间】:2017-01-03 06:55:23
【问题描述】:

这是我的 php 代码:-

<?php
$servername="localhost";
$username="root";
$password="";
$db_name="login";

//Create connection
$conn=new mysqli($servername,$username,$password,$db_name);

$db=mysql_select_db($conn,$db_name);
//check connection

if($db)
{

echo"Conneceted successfully";

$username=$_POST['username'];
$password=$_POST['password'];
$fname=$_POST['First_Name'];
$sname=$_POST['Second_Name'];

$query=mysqli_query($conn,"insert into           accounts(First_Name,Second_Name,email_id,password)        VALUES('".$fname."','".$sname."','".$username."','".$password."')");
 }
else{

echo "notdone".mysql_error($conn);
}


?>

这是我的html代码:-

<html>
<head>

</head>
<body>
<form action="database\signup.php" method="post">
Email id:<input type="text" name="username"><br>
Password:<input type="text" name="password"><br>
First Name:<input type="text" name="First_Name"><br>
Second Name<input type="text" name="Second_Name"><br>
<input type="submit">
</form>

</body>


</html>

当我填写表格并提交时,我得到输出“未完成”,这是我得到的两个警告:-

警告:mysql_error() 期望参数 1 是资源、对象 在第 27 行的 C:\wamp64\www\mywebsite\database\signup.php 中给出

警告:mysql_select_db() 期望参数 1 是字符串,对象 在第 10 行的 C:\wamp64\www\mywebsite\database\signup.php 中给出

请任何人帮助我在这里做错了什么?

【问题讨论】:

标签: php html mysql wamp


【解决方案1】:

您应该完全使用 mysqli_* 函数。不要将 mysql_* 与 mysqli_* 混用

使用以下检查连接:

if($conn->connect_errno > 0){
    die('Unable to connect to database [' . $conn->connect_error . ']');
}

删除

$db=mysql_select_db($conn,$db_name);

因为已经在新的 mysqli 调用中选择了 db。

同时删除

else{

echo "notdone".mysql_error($conn);
}

希望它能解决你的问题

【讨论】:

    【解决方案2】:

    请尝试一次..

    HTML HTML

      <form method="POST">
                Email id:<input type="text" name="username"><br>
                Password:<input type="text" name="password"><br>
                First Name:<input type="text" name="First_Name"><br>
                Second Name<input type="text" name="Second_Name"><br>
                <input type="submit" name="save">
            </form>
    

    PHP

        //Create connection
        $db = mysqli_connect($servername, $username, $password, $db_name);
    
        //check connection
        if ($db) {
            echo"Conneceted successfully";
        }
    
        if (isset($_POST['save'])) {
            $username = $_POST['username'];
            $password = $_POST['password'];
            $fname = $_POST['First_Name'];
            $sname = $_POST['Second_Name'];
    
            $query = "insert into accounts(First_Name,Second_Name,email_id,password) VALUES('" . $fname . "','" . $sname . "','" . $username . "','" . $password ."'";
            $insert = mysqli_query($db, $query);
        }
    
    if($insert){
    echo "Record Inserted";
    }
     else {
            echo "Error";
        }
        ?>
    

    您的查询有错误,请检查是否相同,没有提及主键

    【讨论】:

    • 我已经更新了代码
    • @pawan 请检查一下 ti 一次
    猜你喜欢
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多