-3

I'm studying programming. I have a php File in my XAMPP, this File is use to insert data to my database.

I want to use post method to call php File and execute sql string.

my PHP file:

<?php
$DB_HostName = "localhost"; // ten host
$DB_Name = "QM_TEST";           
$DB_User = "root";          
$DB_Pass = "";              
$DB_Table = "Customer";             

$name = $_GET[@"name"];
$address = $_GET[@"address"];

$con = mysql_connect($DB_HostName,$DB_User,$DB_Pass) or die(mysql_error()); 
mysql_select_db($DB_Name,$con) or die(mysql_error()); 
$sql = "insert into $DB_Table (name, address) values('$name','$address');";
$res = mysql_query($sql,$con) or die(mysql_error());

mysql_close($con);
if ($res) {
    echo "success";
}else{
    echo "faild";
}// end else
?>
Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85

1 Answers1

0

You can do it with [stream_context_create()][1] if you don't want to use cURL.

Here's an example:

    $url = 'http://server.com/path';
$data = array('key1' => 'value1', 'key2' => 'value2');

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data),
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

var_dump($result);

There's a similar question here.

Community
  • 1
  • 1
Alex
  • 4,674
  • 5
  • 38
  • 59