无法使用cURL插入数据库

时间:2019-05-15 01:19:39

标签: php curl

尝试在数据库中插入数据时,我没有收到任何错误。但同时没有输入任何内容。

我观看了具有相同示例的视频。我尝试使用其他语句创建Mysqli对象,但仍然无法正常工作。

//This part of the code is in another php file
<?php
    $data = array("name"=>"patrick", "age"=>"25");
    $string = http_build_query($data);
    $ch = curl_init("http://localhost//data.php");
    curl_setopt($ch,CURLOPT_POST,true);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$string);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_exec($ch);
    curl_close($ch);
?>

//This part of the code is in the data.php
<?php
    if(isset($_POST['name'], $_POST['age']))
    {
      //$db = mysqli_connect("localhost","root","password","testPost");
        $db = new Mysqli("localhost","root","password","testPost");
        $name = $db->real_escape_string($_POST['name']);
        $age = (int)$_POST['age'];
        $query = "INSERT INTO data SET myData = '$name, $age'";
        $db->query($query);
    }
?>

我希望将数据插入数据库中。该数据库名为testPost,它具有两列,分别为id和myData

0 个答案:

没有答案