使用curl将值插入数据库

This is my index.php file

<?php
   $data = array("name"=>"ohidul","age"=>25);
   $string = http_build_query($data);
   $ch = curl_init("http://localhost/curl/send_data_to_server/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 is my data.php file

<?php 
   if(isset($_POST['name'],$_POST['age'])){
      $db = new Mysql('localhost','root','','postdata');
      $name = $db->real_escape_string($_POST['name']);
      $age = (int) $_POST['age'];
      $query = "INSET INTO data SET mydata='$name,$age'";
      $db->query($query);
   }
?>

Here I want to store the value of index.php into the database. I am using curl method of php. But the values are not insert into the database.

How to fix this?

Your usage of query is wrong. It should be like this.

$query = "INSERT INTO data(name, age) VALUES ('$name','$age')";

You may want to consider using parameters instead of simply putting the variables to query to prevent SQL Injection.

I guess problem is with the way you are trying to post data, you dont need to do http_build_query() instead you need to pass array with CURLOPT_POSTFIELDS option.

see below code:

<?php
   $data = array("name"=>"ohidul","age"=>25);
   $ch = curl_init("http://localhost/curl/send_data_to_server/data.php");
   curl_setopt($ch, CURLOPT_POST,true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
   curl_exec($ch);
   curl_close($ch);
?>