Insert data into a table that already exists

159 views Asked by At

I already have a PHP/JS page that works for what I'm trying to create in Wordpress (http://msf.willcannondev.me). The data is being written to the database, which could be viewed by clicking on the CRM button after filling in the fields.

In WP, I have tried the following to insert the data, but not having any success.

In the PHP/JS site, I use the following and it inserts the data into the database:

> $connect = new PDO("mysql:host=xxxxxx;dbname=xxxxxx", "xxxxxx", "xxxxx");
$message = '';
$ip = $_SERVER['REMOTE_ADDR'];
if(isset($_POST["submit"])) {
 $query = "
 INSERT INTO wpbestp_home_purchase
 (salesprice, loan_amount, income, fName, lName, property_street, property_city, property_state, property_zipcode, primary_phone, email, ip, message) VALUES 
 (:salesprice, :loan_amount, :income, :fName, :lName, :property_street, :property_city, :property_state, :property_zipcode, :primary_phone, :email, :ip, :message)";

 $refi_data = array(
  ':salesprice' => $_POST["salesprice"],
  ':loan_amount' => $_POST["loan_amount"],
  ':income' => $_POST["income"],
  ':fName'  => $_POST["fName"],
  ':lName'  => $_POST["lName"],
  ':property_street'   => $_POST["property_street"],
  ':property_city' => $_POST["property_city"],
  ':property_state' => $_POST["property_state"],
  ':property_zipcode' => $_POST["property_zipcode"],
  ':primary_phone'  => $_POST["primary_phone"],
  ':email'   => $_POST["email"],
  ':ip' => $_SERVER['REMOTE_ADDR'],
  ':message' => $_POST["message"]
 );
 $statement = $connect->prepare($query);
 if($statement->execute($refi_data)) {
  $message = '
  <div class="alert alert-success">
  Thank you for getting in touch with us! We will try our best to respond back to you within 24 hours.
  </div>
  ';
 } else {
  $message = '
  <div class="alert alert-error">
  There is an error adding the new record.
  </div>';
 }
}
?>

In WP, I have tried the PHP script and the following, but neither are inserting the data. (The wp-config.php file is where the MySQL connection is located, I believe, so I just require that file):

    <?php
require_once( 'wp-config.php');
global $wpdb;
$message = '';
$ip = $_SERVER['REMOTE_ADDR'];
if(isset($_POST['submit'])) {
  $wpdb->insert( 'wpbestp_home_purchase', array(
 
  'salesprice' => $_POST['salesprice'],
  'loan_amount' => $_POST['loan_amount'],
  'income' => $_POST['income'],
  'fName'  => $_POST['fName'],
  'lName'  => $_POST['lName'],
  'property_street'   => $_POST['property_street'],
  'property_city' => $_POST['property_city'],
  'property_state' => $_POST['property_state'],
  'property_zipcode' => $_POST['property_zipcode'],
  'primary_phone'  => $_POST['primary_phone'],
  'email'   => $_POST['email'],
  'ip' => $_SERVER['REMOTE_ADDR'],
  'message' => $_POST['message'],
  array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' )
 );
 $statement = $connect->prepare($query);
 if($statement->execute($refi_data)) {
  $message = '
  <div class="alert alert-success">
  Thank you for getting in touch with us! We will try our best to respond back to you within 24 hours.
  </div>';
 } else {
  $message = '
  <div class="alert alert-error">
  There is an error adding the new record.
  </div>';
 }
}
?>

If anyone is able to show me what I'm doing incorrectly, I would greatly appreciate it since I'm new to Wordpress.

Thanks in advance.

0

There are 0 answers