无法将用户的输入数据添加到数据库PHP MYSQL

时间:2017-03-05 07:10:28

标签: php mysql

我的PHP代码中出现此错误:

  

警告:mysql_select_db()期望参数1为字符串,资源   在第144行的C:\ xampp \ htdocs \ WEBDEV \ save.php中给出表单提交   失败!

这是我的代码:

    <?php
            $connect = mysql_connect ("localhost", "root");
            $db = mysql_select_db ($connect, "signup");

            $idnum = $_POST ['idnum'];
            $fname = $_POST ['fname'];
            $lname = $_POST ['lname'];
            $course = $_POST ['course'];
            $yrlvl = $_POST ['yrlvl'];
            $pass = $_POST ['pass'];

            $Query = mysql_query ("insert into admin (idnum, fname, lname, course, yrlvl, pass) 
                                values ('$idnum', '$fname', '$lname', '$course', '$yrlvl', '$pass')", $connect);
            if ($Query)
            {
                print "Form submitted successfully! <br>";`e`enter code here`nter code here`
            }

            else
            {
                print "Form submitted failed! <br>";
            }   
        ?>

1 个答案:

答案 0 :(得分:0)

哟需要在数据库链接标识符前面使用“ your_db_name ”。 假设"signup"是您的数据库名称:

 $db = mysql_select_db ("signup", $connect);

警告: 不推荐使用mysql_connect()mysql_select_db()并从php 7.0中删除。请改用mysqli_connect()。例如:

$connection = mysqli_connect('localhost', 'username', 'password', 'database');

类似地,mysql_select_db()可以实现为:

mysqli_select_db($link, "signup");