duandan4680 2019-05-15 01:19
浏览 51

无法使用cURL插入数据库

I am not receiving any errors when trying to insert data in the database. But it at the same time there is nothing being entered.

I watched a video that has the same example. I tried a different statement for creating the Mysqli object and still do not work.

//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);
    }
?>

I expect data to be inserted in a database. The database is called testPost and it has two columns called id and myData

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教