使用MySQL的PHP​​更新表

时间:2015-10-17 14:47:07

标签: php mysql

我的数据库中有学生表,我需要在学生页面中创建额外的表格,以便他可以更新详细信息。但是我得到这个错误(警告:mysqli_query()需要至少2个参数,1在第81行的C:\ wamp \ www \ WebInterfaceLogIn \ studentEdit.php中给出)

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);
include ('includes/connection.php');

 // Create connection

if (isset($_POST["submit"]))
{ //Determine if a variable is set and is not NULL.
    if (!empty($_POST['ID']) && !empty($_POST['user']) && !empty($_POST['surr']) && !empty($_POST['course']) && !empty($_POST['mail']) && !empty($_POST['pass']))
    { //Determine if user enters both user name and password.
        $ID = $_POST['ID']; // enters user ID in database
        $user = $_POST['user']; // enters user name in database
        $surr = $_POST['surr']; // enters user surname in database
        $course = $_POST['course']; // enters user course in database
        $pass = $_POST['pass']; // enters password in database
        $mail = $_POST['mail'];

        // $query = mysqli_query($con,"SELECT * FROM students WHERE Student_ID='".$ID."'");  // change to update

        $query = mysqli_query("UPDATE students SET `course` = " . $_POST['course'] . ", `email` = " . $_POST['mail'] . " Student_ID='" . $ID . "'");
        $numrows = mysqli_num_rows($query);
        if ($numrows == 0)
        {
            $sql = "INSERT INTO students(Student_ID,Name,Surname,Course,email,password) VALUES('$ID', '$user','$surr','$course','$mail','$pass')"; // insert user name and password to database
            $result = mysqli_query($con, $sql);

            // Checks does user enters the details

            if ($result)
            {
                echo '<script language="javascript">;
                     alert("Account Successfully Updated");
                        document.location.href="index.php";
                     </script>';
            }
            else
            {
                echo mysqli_error($con);
            }
        }
    }
    else
    {
        echo '<script language="javascript"> 
                                alert("All fields required")
                             </script>';
    }
}

任何人都可以帮忙解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您需要将连接对象传递给函数mysqli_query,如下所示:

mysqli_query($con,"UPDATE students SET `course` = " .$_POST['course']. ", `email` = " .$_POST['mail']. " Student_ID='".$ID."'");

详情请见: http://www.w3schools.com/php/func_mysqli_query.asp

答案 1 :(得分:0)

错误足够明确。

$con作为第一个参数添加到此行的mysqli_query函数中:

$query = mysqli_query("UPDATE students SET `course` = " . $_POST['course'] . ", `email` = " . $_POST['mail'] . " Student_ID='" . $ID . "'");