Failed to update MySQL database

I am trying to learn mysql and have some problems updating / adding data to a table

this is my code and after starting this page, when I switch to phpmyadmin to find out if new data has appeared, I donโ€™t see it there.

<?php $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(!$conn) { die("Could not connect"); } $dbname = "test"; mysql_select_db($dbname, $conn); mysql_query("INSERT INTO 'test'.'table1' ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') VALUES ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')"); mysql_close($conn); ?> 

Can someone tell me what happened with this?

+4
source share
4 answers

The problem is quotes around field names. MySQL does not allow single-quoted field names. They must either be bare (A1) or within the backticks (`A1`), so your request should be rewritten as:

 INSERT INTO table1 (A1, A2, A3, A4, A5, A6, A7, A8) VALUES ('test1', 'test2', 'test3', 'test4', etc.....); 
+2
source

Be sure to do an error checking to see if there is a problem connecting mysql:

 if(!mysql_select_db($dbname, $conn)) { echo mysql_error($conn); }else { if(!mysql_query("INSERT INTO 'test'.'table1' ('A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8') VALUES ('test1', 'test2', 'test3', 'test4', 'test5', 'test6', 'test7', 'test8')")) { echo mysql_error($conn); } } 
+2
source

First of all, make sure that the database is selected in the order:

 mysql_select_db($dbname, $conn) or die('Could not select the database'); 

There is a possibility that there is some error in your query, the reason why no data was added, try adding or die(mysql_error()) after this mysql_query function to see a possible error:

 mysql_query("your query....") or die(mysql_error()); 

There is also no need to specify a database name:

 'test'.'table1' 

Just use table1 in your query.

It is also useful to include an error report:

 ini_set('display_errors', true); error_reporting(E_ALL); 
0
source

Here's a piece of advice in general when working with MYSQL (or any SQL DB). If you do not know how to do something, or you are mistaken. Use the provided GUI tools to complete the task, and then check the resulting code.

Type PHPMyAdmin, paste, and then copy and paste the code to see what you are doing wrong.

0
source

All Articles