1

I've been trying things for over an hour to get this to insert into my database correct. I can't figure this out for the life of me even though queries exactly like work just fine.

No errors are being thrown on the page, but it is not inserting the data into the database.

PHP Code (The variable are posting correctly to the page):

<?php
error_reporting(E_ALL);
//start session
session_start();

//include database connection
include('../db_connect.php');

//import info
$client = $_POST['client'];
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$address3 = $_POST['address3'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$country = $_POST['country'];
$vehicle = $_POST['vehicle'];
$msrp = $_POST['msrp'];
$saleprice = $_POST['saleprice'];
$date = date("Y-m-d H:i:s");
$status = "Pending";
$notes = "Test notes are great!";


$sql = "INSERT INTO sales (salePrice, saleDate, saleStatus, saleNotes, saleName, saleCompany, salePhone, saleEmail, saleAddress1, saleAddress2, saleAddress3, saleCity, saleState, saleZipcode, saleCountry, clientFK) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

if ($stmt = $mysqli->prepare($sql)) {

/* Bind our params */
$stmt->bind_param('dssssssssssssssi', $saleprice , $date , $status , $notes , $name , $company , $phone , $email , $address1 , $address2 , $address3 , $city , $state , $zip , $country , $client);

/* Execute the prepared Statement */
$stmt->execute();

/* Echo results */
echo "Inserted user information into database.\n";

/* Close the statement */
$stmt->close();
}else{
    /* Error */
printf("Prepared Statement Error: %s\n", $mysqli->error);
}
?>

Here is the layout of this table in my database:

CREATE TABLE IF NOT EXISTS `sales` (
  `idsale` int(11) NOT NULL AUTO_INCREMENT,
  `salePrice` double NOT NULL,
  `saleDate` datetime NOT NULL,
  `saleStatus` varchar(50) NOT NULL,
  `saleNotes` varchar(100) DEFAULT NULL,
  `saleName` varchar(70) DEFAULT NULL,
  `saleCompany` varchar(50) DEFAULT NULL,
  `salePhone` varchar(20) DEFAULT NULL,
  `saleEmail` varchar(255) DEFAULT NULL,
  `saleAddress1` varchar(35) DEFAULT NULL,
  `saleAddress2` varchar(35) DEFAULT NULL,
  `saleAddress3` varchar(35) DEFAULT NULL,
  `saleCity` varchar(20) DEFAULT NULL,
  `saleState` varchar(50) DEFAULT NULL,
  `saleZipcode` varchar(15) DEFAULT NULL,
  `saleCountry` varchar(50) DEFAULT NULL,
  `createdBy` varchar(30) DEFAULT NULL,
  `createdDate` datetime DEFAULT NULL,
  `modifiedBy` varchar(30) DEFAULT NULL,
  `modifiedDate` datetime DEFAULT NULL,
  `clientFK` int(11) NOT NULL,
  PRIMARY KEY (`idsale`),
  KEY `clientFK_INDEX` (`clientFK`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

Any help will be appreciated! Thanks for your help ahead of time.

5
  • What happens? Do you get any errors? Commented Apr 22, 2013 at 3:23
  • No the page is blank and doesn't throw an error. But when I go to check for the data it is not inserted. It's weird because I have echo'd the variables and they are all pulling correctly. Commented Apr 22, 2013 at 3:24
  • Is your PHP error reporting turned on? Commented Apr 22, 2013 at 3:32
  • Hm, that's odd. Try enclosing it in a try/catch block, and see if you can echo out any errors. Commented Apr 22, 2013 at 3:40
  • please set display_errors=on in your php.ini Commented Apr 22, 2013 at 4:44

2 Answers 2

1

Your query is not binding the variables properly because you are passing 16 placeholders in your query and passing 17 values in the bind function.

(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  //16 values...

$stmt->bind_param('dssssssssssssssi', $saleprice , $date , $status , $notes , $name , $company , $phone , $email , $address1 , $address2 , $address3 , $city , $state , $zip , $country , $client); //17 variables

Due to this your query is throwing an error. Enable errors in your php.ini to see the error message.

Sign up to request clarification or add additional context in comments.

3 Comments

I can not see 17 values. it is 16
@Amir No, Jay's correct. There are more values being bound than are supplied in the query.
@Descartes how you can count it. it is 16 not 17
0

This seems really stupid now, but where it states:

//include database connection
include('../db_connect.php');

It was not working until I changed it to:

//include database connection
include('db_connect.php');

It's the weirdest thing how ../ can make all the difference. Thank you all for your help.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.