function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sakthi169Sakthi169 

How to insert the record into salesforce (sandbox) using php

Hi,

I am create the simple page with mandatory field(Firstname,Lastname,email,city,mobileno,source) once i submit form and it directly insert into salesforce. My code for ur reference
<?php
// session_start() has to go right at the top, before any output!
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
<?php
require_once ('soapclient/SforceEnterpriseClient.php');
define("USERNAME", "xxxxxxx");
define("PASSWORD", "xxxxxx");
define("SECURITY_TOKEN", "xxxxxxx");
$mySforceConnection = new SforceEnterpriseClient();
$wsdl = 'soapclient/enterprise.wsdl.xml';
$mySforceConnection->createConnection($wsdl);
$mySforceConnection->login(USERNAME, PASSWORD.SECURITY_TOKEN);
$mySforceConnection->setEndpoint('https://test.salesforce.com/services/Soap/c/30.0');
$LastName=$_POST["name"];
$CustomMobile__c=$_POST["mobileno"];
$query = "insert into 'Account'(LastName,CustomMobile__c )VALUES('$LastName','$CustomMobile__c')";
$result = $mySforceConnection->query($query);
print_r($result);

			$nameErr = $emailErr = $genderErr = $mobilenoErr = $websiteErr = "";
$name = $email = $gender = $comment = $mobileno = $website = "";

if ($_POST['submit']) {
   if (empty($_POST["name"])) {
     $nameErr = "Name is required";
   } else {
     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
       $nameErr = "Only letters and white space allowed"; 
     }
   }
  if (empty($_POST["mobileno"])) {
     $mobileno= "";
   } else {
     $mobileno= test_input($_POST["mobileno"]);
     // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
     if (!preg_match("/^[1-9][0-9]*$/",$mobileno)) {
       $mobilenoErr = "Invalid mobileno"; 
     }
   }

   if (empty($_POST["email"])) {
     $emailErr = "Email is required";
   } else {
     $email = test_input($_POST["email"]);
     // check if e-mail address is well-formed
     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
       $emailErr = "Invalid email format"; 
     }
   }

   if (empty($_POST["website"])) {
     $website = "";
   } else {
     $website = test_input($_POST["website"]);
     // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
     if (!preg_match("/b(?:(?:https?|ftp)://|www.)[-a-z0-9+&@#/%?=~_|!:,.;]*[-a-z0-9+&@#/%=~_|]/i",$website)) {
       $websiteErr = "Invalid URL"; 
     }
   }

   if (empty($_POST["comment"])) {
     $comment = "";
   } else {
     $comment = test_input($_POST["comment"]);
   }

   if (empty($_POST["gender"])) {
     $genderErr = "Gender is required";
   } else {
     $gender = test_input($_POST["gender"]);
   }
}

function test_input($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);
   return $data;
}
?>
<h2>Account Details</h2>
<p><span class="error">* required field.</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
   Name: <input type="text" name="name" value="<?php echo $name;?>">
   <span class="error">* <?php echo $nameErr;?></span>
   <br><br>
Mobile No: <input type="text" name="mobileno" value="<?php echo $mobileno;?>">
   <span class="error">* <?php echo $mobilenoErr;?></span>
   <br><br>
   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
   <span class="error">* <?php echo $emailErr;?></span>
   <br><br>
 
  
   <br><br>
   Gender:
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">Female
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">Male
   <span class="error">* <?php echo $genderErr;?></span>
   <br><br>
   <input type="submit" name="submit" value="Submit"> 
</form>

<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $mobileno;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>

Fatal error: Uncaught SoapFault exception: [soapenv:Client] No operation available for request {urn:enterprise.soap.sforce.com}login in C:\xampp\htdocs\soapclient\SforceBaseClient.php:169 Stack trace: #0 C:\xampp\htdocs\soapclient\SforceBaseClient.php(169): SoapClient->__call('login', Array) #1 C:\xampp\htdocs\soapclient\SforceBaseClient.php(169): SoapClient->login(Array) #2 C:\xampp\htdocs\test2.php(20): SforceBaseClient->login('xxxxxxx...') #3 {main} thrown in C:\xampp\htdocs\soapclient\SforceBaseClient.php on line 169