我如何从请求表中获取数据并将其放入用户表中

时间:2018-07-09 07:57:39

标签: php

我正在建立一个用户注册请求系统。

可以帮我将accept.php文件转换为mysqli吗?因为我使用的是mysqli,而这个使用的是pdo。

这是我针对用户请求的代码。

<?php
         $query = "select * from `requests`;";
         if(count(fetchAll($query))>0){
             foreach(fetchAll($query) as $row){
                 ?>


             <h1 class="jumbotron-heading"><?php echo $row['name'] ?></h1>

               <p>
                 <a href="accept.php?id=<?php echo $row['id'] ?>" class="btn btn-primary my-2">Accept</a>
                 <a href="reject.php?id=<?php echo $row['id'] ?>" class="btn btn-secondary my-2">Reject</a>
               </p>
             <small><i><?php echo $row['DateRegister'] ?></i></small>
     <?php

我在accept.php中的代码

<?php
include "core/init.php";
include "functions.php";

        $id = $_GET['id'];
        if(count(fetchAll($sql)) > 0){
        foreach(fetchAll($sql) as $row){
        $username = $_POST['username'];
        $password = $_POST['password'];
        $unitid = $_POST['unitid'];
        $worknum = $_POST['worknum'];
        $homenum = $_POST['homenum'];
        $userlevel =$_POST['usertype'];
        $empname = $_POST['empname'];
        $position = $_POST['position'];
        $comment = $_POST['comment'];
        $sql = "INSERT INTO users (id,username, password, usertype,DateRegister, name, unit_id, phone_work, phone_home, comment, position,LastUpdated) VALUES (NULL,'$username','$password','$userlevel','$currentdate','$empname','$unit_id','$worknum','$homenum','$comment','$position','$currentdate,$currentTime')";
    }
    $sql .= "DELETE FROM `requests` WHERE `requests`.`id` = '$id';";
    if(performQuery($sql)){
        echo "Account has been accepted.";
    }else{
        echo "Unknown error occured. Please try again.";
    }
}else{
    echo "Error occured.";
}
}

我的functions.php

<?php
define('DBINFO','mysql:host=localhost;dbname=arenew');
define('DBUSER','root');
define('DBPASS','');

function performQuery($query){
    $con = new PDO(DBINFO,DBUSER,DBPASS);
    $stmt = $con->prepare($query);
    if($stmt->execute()){
        return true;
    }else{
        return false;
    }
}

function fetchAll($query){
    $con = new PDO(DBINFO, DBUSER, DBPASS);
    $stmt = $con->query($query);
    return $stmt->fetchAll();
}

?>

单击接受后,它指示错误或未定义的索引。

1 个答案:

答案 0 :(得分:0)

您不应在SQL变量中将id定义为NULL。 它说:“嘿,数据库,我希望该行的id = NULL!”。由于id是主键,因此SQL显示错误。

INSERT INTO users (username, password, usertype,DateRegister, name, unit_id, phone_work, phone_home, comment, position,LastUpdated)
VALUES ('$username','$password','$userlevel','$currentdate','$empname','$unit_id','$worknum','$homenum','$comment','$position','$currentdate,$currentTime')