-2

Hi i want to write code to insert data in MySQL table and along with it would also insert to API.

I am able to insert the data in MySQL database but when i write the code for push the data in API it will also not insert the data in MySQL table.

$fname = $value['first_name'].' '.$value['last_name'];
$phone = $value["phone_number"];
$email = $value["email"];
$city =  addslashes($value["city"]);
$client_source = "Home Visit";
$source = "Facebook Home Visit";
$z_uid=$value["z_uid"];

$query1="INSERT  INTO amplifon_web_leads(submitted,enquiry_id,yourname,email,phone,preferred_centre,status,region_id,brand_id,source,user_id,lead_handler_name,Priority,client_source) VALUES(date_add(now(),INTERVAL 630 Minute),CONCAT('AMP','$enquiry_code'),'$fname','$email','$phone','$city','16001','1601','16','$source','$user_id','$handler_name','','$client_source')";

mysqli_query($connection,$query1) or die ('Error updating database1');
$query2="DELETE FROM `amplifon_facebook_zipper` WHERE `z_uid`='$z_uid'";
mysqli_query($connection,$query2) or die ('Error updating database2');

$url = "http://APIURL.COM/Campaign/API/Intekill/Enquiry";
$enquiry_codes = CONCAT('AMP','$enquiry_code');
$data = array("Name" => $fname, "InterSkaleEnquiryLeadId" => $enquiry_codes,"Mobile"=>$phone,"Phone"=>$phone,"Email"=>$email,"CenterType"=>$city,"PrefCenter"=>$city, "Enquiry"=> "from cron", "OtherCenter"=> "null", "Pname"=>"null", "Pmobile"=>$phone, "CallDate"=> "null", "CallTime"=> "null","ClientSource"=> "null" );

$ch = curl_init( $url );
# Setup request to send json via POST.
$payload = json_encode( array( "Enquiry"=> $data ) );

curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
# Return response instead of printing.
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
# Send request.
$result = curl_exec($ch);
curl_close($ch);

I want to insert the data in API and MySQL Table.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • what you mean by "also not insert"? does it get not inserted on api as well as well as db? – slepic Aug 06 '19 at 06:28
  • when i add api code it not inserted in db but when i remove api code it will inserted in db so i think i am doing something wrong in api code – Bhushan Patil Aug 06 '19 at 06:54
  • but is it inserted on api? or do you get some error there? – slepic Aug 06 '19 at 06:57
  • Thanks @dharman but currently our primary goal is to push the data in API and db – Bhushan Patil Aug 06 '19 at 09:42
  • @slepic No the data is not inserted in API also we don't get any error we also enable error reporting but no luck – Bhushan Patil Aug 06 '19 at 09:44
  • You said that your data is not inserted to DB. It could be the problem that you are not using prepared statements. My comment was not off-topic. Please switch on MySQLi errors too: https://stackoverflow.com/q/22662488/1839439 – Dharman Aug 06 '19 at 09:45
  • @dharman **Actually as i said when i remove API Code then the data is store in db but when i write the API code then both API and db not the get the data.** – Bhushan Patil Aug 06 '19 at 09:52

1 Answers1

0

Hey guys thanks for the comments now it's work with following code there two mistake that i made.

  1. I use concat() function that will generate error

  2. I change content-type to encoded and it's works.

    $fname = $value['first_name'].' '.$value['last_name'];
    $phone = $value["phone_number"];
    $email = $value["email"];
    $city =  addslashes($value["city"]);
    $client_source = "Home Visit";
    $source = "Facebook Home Visit";
    $z_uid=$value["z_uid"];
    
    
    $query1="INSERT  INTO amplifon_web_leads(submitted,enquiry_id,yourname,email,phone,preferred_centre,status,region_id,brand_id,source,user_id,lead_handler_name,Priority,client_source) VALUES(date_add(now(),INTERVAL 630 Minute),CONCAT('AMP','$enquiry_code'),'$fname','$email','$phone','$city','16001','1601','16','$source','$user_id','$handler_name','','$client_source')";
    
    
    mysqli_query($connection,$query1) or die ('Error updating database1');
    $query2="DELETE FROM `amplifon_facebook_zipper` WHERE `z_uid`='$z_uid'";
    mysqli_query($connection,$query2) or die ('Error updating database2');
    
    
    $enquiry_codes = 'AMP'.'$enquiry_code';
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,"http://ipaddress/Campaign/API/InterSkill/Enquiry");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"Name=".$fname."&InterSkaleEnquiryLeadId=".$enquiry_codes."&Mobile=".$phone."&Phone=".$phone."&Email=".$email."&CenterType=".$city."&PrefCenter=".$city."&Enquiry=FROMcron&OtherCenter=null&Pname=null&Pmobile=null&CallDate=null&CallTime=null&ClientSource=null");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    
    
    // receive server response ...
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $server_output = curl_exec ($ch);
    
    curl_close ($ch);