试图获取非对象的属性:使用SELECT

时间:2016-01-21 12:33:11

标签: php html mysql database

我的代码没有什么问题......因为我尝试从数据库SELECT开始,然后INSERT将一些值转移到另一个表,但是来自w3school的普通代码 我收到了错误

  

尝试获取非对象的属性

这是我的代码:

<?php 
    session_start();

    function connectionDB(){
        $host = "localhost";
        $username = "root";
        $password = "";
        $db_name = "project";

        $conn = new mysqli($host, $username, $password, $db_name);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        } 
        return $conn;
    }
    function getID(){
        $id = $_GET['id'];

        return $id;
    }
    $login =$_SESSION['login'];
    $conn =connectionDB();
    $idCar = getID();
    echo $idCar;
    $sqluser = "SELECT ID_USER FROM login_table WHERE LOGIN = $login";

    if($result->num_rows > 0)
    $result = $conn->query($sqluser);
    {
        while($row = $result->fetch_assoc())
        {
            $sql = "INSERT INTO cart (id_user) VALUES ('".$row['ID_USER']."')";
        }
    }else echo"error";
?>

这是带产品的代码

1 个答案:

答案 0 :(得分:1)

请参阅IF循环附近的更改

    $sqluser = "SELECT ID_USER FROM login_table WHERE LOGIN = $login";
    $result = $conn->query($sqluser);  //check result first

    if($result->num_rows > 0)  //get number of rows
    {
        while($row = $result->fetch_assoc())
        {
            $sql = "INSERT INTO cart (id_user) VALUES ('".$row['ID_USER']."')";
        }
    }else echo"error";