hello i am facing a silly problem. i can connect to my mysql database from my host but when i insert data from a form its not saving into my mysql table. bdhost= localhost bduser=london1_pavel (here london1 is my hosting user name) password=password database name= london1_mydatabase (here london1 is my hosting user name) table name= london1_mydatabase_test (here london1_mydatabase is my database name and test is my table name, so table name is london1_mydatabase_test) i am using the following code: <?php mysql_connect("localhost", "london1_pavel", "password") or die(mysql_error()); echo "Connected to MySQL<br />"; mysql_select_db("london1_mydatabase") or die(mysql_error()); echo "Connected to Database"; $name=$_POST['name']; $add=$_POST['add']; $sql = "INSERT INTO london1_mydatabase_test (name, add) VALUES ('$name', '$add')"; $result=mysql_query($sql); ?> this code connect to my database but there is no input in the table. anyone please can help me?
Try this $sql = "INSERT INTO london1_mydatabase_test (`name`, `add`) VALUES ('$name', '$add')"; Code (markup):
You need to escape your SQL variables: '" . mysql_real_escape_string($name) . "', '" . mysql_real_escape_string($add) . "')" Code (markup): This is necessary when those values contain ' characters and simply to prevent SQL injection attacks. And if that isn't working, add this: if (!$result) { echo 'SQL: ' . $sql . '<br>'; echo 'Error: ' . mysql_error(); die(); } PHP:
sory bro still not workin. i don know wher the problem is. i used ur code but getting the reply: Connected to MySQL Connected to DatabaseSQL: INSERT INTO amarlon1_mysite_test (name, add) VALUES ('aaa', 'sdfdf') Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'add) VALUES ('aaa', 'sdfdf')' at line 1
Oh, you should have this in the SQL: `name`, `add` (as the other user said, because "add" is a special word in mysql) then show the error message again.
lool funny man make sure your database is called london1_mydatabase_test and NOT just test once you have selected your db you dont need to prepend it with every query TRY $sql = "INSERT INTO test (`name`, `add`) VALUES ('$name', '$add')"; Code (markup):