Arun Kumaresh
Arun Kumaresh

Reputation: 6311

how to insert with php curl?

Im new to webservice and trying to perform the insert the curl i have written a function for insert

function insert($table,$field,$data,$conn)
{
    $field_values=''.implode(",",$field).'';
    $data_values="'".implode("','",$data)."'";

    $sql="INSERT into ".$table." (".$field_values.") VALUES(".$data_values.")";
    $result=$conn->query($sql);
    return $result;

}

im trying to pass the post values through url so i write the following code

<!DOCTYPE html>
<html>
<head>
    <title>Sample Webservice</title>
</head>
<body>
<form method="post">
Enter the  name<input type="text" name="name" id="name">
Enter the  email<input type="text" name="email" id="email">
Enter the  phone_number<input type="text" name="number" id="number">
<input type="submit">
</form>
</body>
</html> 
<?php
if(!empty($_POST['name'])) {
     $name=$_POST['name'];
     $email=$_POST['email'];
      $number=$_POST['number'];
 $url="http://localhost/rest/?name=".$name."&email=".$email."&number=".$number."";

$client=curl_init($url);
curl_setopt($client,CURLOPT_RETURNTRANSFER,1);
$response=curl_exec($client);
$result=json_decode($response);
echo $result->data; 

}
?>

and im trying get the values through GET and i have witten the following code

if(!empty($_GET['name']) && !empty($_GET['email']) && !empty($_GET['number']))
{

 $name=$_GET['name'];
 $email=$_GET['email'];
 $number=$_GET['number'];

$table="sample";
$field_values=array("name","email","phone_number");
$data_values=array($name,$email,$number);
$insert=insert($table,$field_values,$data_values,$conn);


if($insert==TRUE)
{
    d_response(200,"inserted","Successfullyinserted");
}
else
{
d_response(200,"not inserted","notinserted");   
}

}

function d_response($status,$status_message,$data)
{
    $response['status']=$status;
    $response['status_message']=$status_message;
    $response['data']=$data;

    $jresponse=json_encode($response);
    echo $jresponse;
}

?>

when i submit the form it returns the error and the values are not inserted into the database but when i directly call this url http://localhost/rest/?name=".$name."&email=".$email."&number=".$number.""; in the browser the values are inserting into the database

Upvotes: 0

Views: 2490

Answers (1)

Holger
Holger

Reputation: 96

You are confusing two different ways the browser "talks" to the server.

  1. When you type something in the address bar (which works for you) the browser usually performs a HTTP GET request.
  2. When you submit a form, it's typically (and in your case) a HTTP POST request.

So you either have to change the form method to "get" or change the way you access the variables from $_GET['name'] to $_POST['name'] - or you use $_REQUEST['name'], where it doesn't matter if it was a GET or POST request.

Upvotes: 1

Related Questions