-2

I recently uploaded a website from my localhost onto webspace. The code on the webspace is working fine but on my localhost I am only able to do SELECT queries; all INSERT queries fail despite the two sets of code being identical. I have checked the permissions in phpMyAdmin and the 'root' user I am logging in with on localhost has all privileges. I cannot figure out why this is happening.

Example. The following code works fine:

mysql_query("SELECT * FROM tbl_note")

but this does not:

mysql_query("INSERT INTO tbl_note(col1, col2) VALUES('$col1', '$col2')") or die("ERROR");

The latter always throws up an error. As I said, the code runs fine my the server but I want to be able to continue development on localhost. I cannot think of anything I have changed to have caused this.

user1640722
  • 79
  • 3
  • 9
  • 1
    And the error it gives you when you try to insert is? Also, are you definitely logging in from your app as root? Also, ***never*** log in from your app as root! – Perception Mar 16 '13 at 22:35
  • What error does the insert query throw? What are the values of `$col1` and `$col2`? – Steven V Mar 16 '13 at 22:35
  • try to run `REPAIR TABLE tbl_note;` on your localhost, make sure you have your `SHOW CREATE TABLE tbl_note;` identical on both localhost and your hosting. – Martina Mar 16 '13 at 22:40
  • Would this be better for serverfault.com? – Chris Moschini Mar 17 '13 at 22:04

1 Answers1

2

Try this, I also, included mysql_error() function, so you will know what is going on, if you code does not work.

mysql_query("INSERT INTO tbl_note(col1, col2) VALUES('".$col1."', '".$col2.'")") or die(mysql_error());

Also, please do not use mysql_* functions, as they are vulnerable to sql injection and are out of date, instead google, PDO and use it to connect to mysql database instead.

samayo
  • 16,163
  • 12
  • 91
  • 106
  • "Failed to read auto-increment value from storage engine" was the error I got. – user1640722 Mar 16 '13 at 22:37
  • @user1640722 Good!, because not you know what the problem is. It may be from your database, or maybe you should check if you have set up `Auto Increment` – samayo Mar 16 '13 at 22:40
  • I have set the first column of the table to auto increment. I don't understand why this error is happening; I insert a new row into the DB with no value for that column because it's set to autoincrement. – user1640722 Mar 16 '13 at 22:43
  • This code fixed it: ALTER TABLE `table_name` AUTO_INCREMENT =1 – user1640722 Mar 16 '13 at 22:46
  • @user1640722 check this answer too http://stackoverflow.com/questions/7346934/mysql-failed-to-read-auto-increment-value-from-storage-engine – samayo Mar 16 '13 at 22:46