-1

I'm trying to insert some data into a DB on my local machine.

I've opened up the connection, and performed the INSERT INTO for the fields I require. Nothing is showing up in the database but I'm really lost as I'm not getting any errors either, so no guidance as to where it is failing. I have tried scaling it back and adding in echo variables to check how far the script is running but it runs all the way to the end.

To start I set the password and username which is just my local stuff:

// configuration
$dbuser     = "root";
$dbpass     = "";

Then I create the connection

// Create connection
$con=mysqli_connect("localhost",$dbuser,$dbpass,"survey");

Because I was struggling to find out where it was falling over, I have included an error statement. At present it outputs 'successfully connected' so it's not failing to connect...

// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
    echo 'successfully connected';
}

And then I'm preparing the SQL INSERT INTO

// the SQL insert
mysqli_query($con,"INSERT INTO mytablename (MostImportantFeatures,ImportanceOfFreeDelivery)
VALUES ($features,$importance_free_delivery)");
mysqli_close($con);

And I am getting nothing back. No errors, but no submission to the database. I cannot work out where I've gone wrong or how I can debug this as the script runs through. I can log into the database (using Sequel Pro) with the details above just fine.

How would you start to debug this?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Francesca
  • 26,842
  • 28
  • 90
  • 153
  • 1
    You more than likely need to quote these if they're strings `VALUES ('$features','$importance_free_delivery')` – Funk Forty Niner Sep 25 '14 at 16:30
  • 1
    *"How would you start to debug this?* - http://php.net/manual/en/function.error-reporting.php + http://php.net/manual/en/mysqli.error.php - *"No errors, but no submission to the database."* - No errors because you're not checking for them. – Funk Forty Niner Sep 25 '14 at 16:31
  • Dump all the things! – Mihai Sep 25 '14 at 16:32
  • I would run a select count(*) and output the results before and after the attempted insert. – Dan Bracuk Sep 25 '14 at 16:33

1 Answers1

0

To start debugging you probably want to check if you get TRUE or FALSE back from mysqli_query, and check the last error on false.

if(!mysqli_query($con,"INSERT INTO mytablename (MostImportantFeatures,ImportanceOfFreeDelivery) VALUES ($features,$importance_free_delivery)")) {
    echo mysqli_error($con);
}

mysqli_query docs - php.net

mysqli_query docs - php.net

Jeff Horton
  • 904
  • 12
  • 18