Jump to content

PHP contact form can't access mySQL database


jkost

Recommended Posts

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
);

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

 

 

 

 

 

 

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.