jkost Posted February 10, 2010 Share Posted February 10, 2010 i'm trying to build a contact form so people can reach me and i want to save the data to mySQL database, but i'm receiving the following message when trying to use the contact form and submit the data. "Could not connect: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)" <?php // Pick up the form data and assign it to variables $name = $_POST['name']; $email = $_POST['email']; $topic = $_POST['topic']; $comments = $_POST['comments']; // Build the email (replace the address in the $to section with your own) $to = 'contact@mysite.com'; $subject = "New message: $topic"; $message = "$name said: $comments"; $headers = "From: $email"; // Open database connection $conn = mysql_connect('mysql3.freehostia.com', '****', '****'); mysql_select_db('****_contact'); if (!$conn) { die('Could not connect: ' . mysql_error()); } // Insert data $query = "INSERT INTO submissions (name, email, topic, comments) ... VALUES ('$name', '$email', '$topic', '$comments')"; mysql_query($query); // Close connection mysql_close($conn); // Redirect header("Location: success.html"); there a table named "submissions" which i created by doing this¨ CREATE TABLE submissions ( name VARCHAR( 100 ) NOT NULL, email VARCHAR( 255 ) NOT NULL, topic VARCHAR( 255 ) NOT NULL, comments TEXT NOT NULL ); Quote Link to comment Share on other sites More sharing options...
mars_rahul Posted February 10, 2010 Share Posted February 10, 2010 Hello, If your database is hosted in the same Domain then you can try: $conn = mysql_connect('localhost', '****', '****'); instead of: $conn = mysql_connect('mysql3.freehostia.com', '****', '****'); . Else code is fine and i dont think any error there. Thanks.. Quote Link to comment Share on other sites More sharing options...
jkost Posted February 11, 2010 Author Share Posted February 11, 2010 mars_rahul thanks for you answer! the problem was at the login details.. but still there is issue, i can't write any entries to the database and there is no error message. i have tried to delete the database then create a new one and make the table manualy witout the above script but it wasn't helpful Quote Link to comment Share on other sites More sharing options...
sader Posted February 11, 2010 Share Posted February 11, 2010 select databse after u login mysql_select_db("database_name"); Quote Link to comment Share on other sites More sharing options...
jkost Posted February 12, 2010 Author Share Posted February 12, 2010 select databse after u login mysql_select_db("database_name"); hi could you please explain what do you mean by that? if you check the code above can see that i'm defining the name of the database. Quote Link to comment Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 Oh realy sorry I misted it Quote Link to comment Share on other sites More sharing options...
jkost Posted February 12, 2010 Author Share Posted February 12, 2010 i have no knowledge on php and i'm trying to figure these out all alone because i want to make a simple contact form so people can reach me... please somebody who knows a little bit more than i do to help here!!!! so after a bit of searching for a line of code than can handle an error i found and added the following: or die ("Failure Reason: $query. " . mysql_error());] and it gave me this result: Failure Reason: INSERT INTO submissions (name, email, topic, comments) ... VALUES ('test', 'test ', 'test', 'test!!!!!!!'). 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 '... VALUES ('test', 'test ', 'test', 'test!!!!!!!')' at line 1 but still i don't understand what kind of changes i need to do to my code! Quote Link to comment Share on other sites More sharing options...
sader Posted February 12, 2010 Share Posted February 12, 2010 oh u need to remove ... from your query it goes like INSERT INTO table (a,b,c,d) VALUES ('','','','') Quote Link to comment Share on other sites More sharing options...
jkost Posted February 12, 2010 Author Share Posted February 12, 2010 nice! thank you sader! now i can put entries in the database!!! but i've noticed that if my contact form contains words like "who's "it's" "what's" then i get similar error as above! why it doesn't accept the ' character? "Failure Reason: INSERT INTO submissions (name, email, topic, comments) VALUES ('test', 'test', 'test', 'what's'). 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 's')' at line 1" Quote Link to comment Share on other sites More sharing options...
jkost Posted February 12, 2010 Author Share Posted February 12, 2010 any help? Quote Link to comment Share on other sites More sharing options...
fenway Posted February 16, 2010 Share Posted February 16, 2010 Use mysql_real_escape_string(). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.