Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

php - Connection to database seems successful but INSERT INTO sends no data

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...