send form data to a mysql database using php?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
logiclab
Forum Newbie
Posts: 3
Joined: Fri Aug 14, 2009 3:26 pm

send form data to a mysql database using php?

Post by logiclab »

Hi,

I have a form that collects the user's Name, Email for a future newsletter. I know almost zero PHP and have been using a tutorial at

http://www.apluskb.com/scripts/How_can_ ... r3591.html

but hit the wall.

when I try to add a name and email, I get this error.

Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host '(jvassgal_email).mysql.jvassgallery.com' (1) in /home/jvassgal/public_html/formindb.php on line 2
Could not connect: Unknown MySQL server host '(jvassgal_email).mysql.jvassgallery.com' (1)

the form looks like this

Code: Select all

 
    <!-------------begin form------------>
<FORM ACTION="formindb.php" METHOD="POST" NAME="contact_form">
<span class="optin">Name</span><br>
<input name="name" type=text size="18">
<br>
 <span class="optin">Email</span><br>
<input name="email" type=text size="18"><br>
<br>
<input class="optin" type="submit" value="Gallery News" name="Submit">
</FORM>
<!-------------end form------------>
 
The MYSQL database I created is named email but phpmyadmin is calling it jvassgal_email so I am confused already.

In the first line below, ($con) I tried replacing jvassgal_email with localhost per the host's suggestion but I don't think I put localhost in the right place.

The php file formindb.php is on the server and the code is

Code: Select all

 
 
<?php
$con = mysql_connect("(jvassgal_email).mysql.jvassgallery.com","mylogin","mypassword"); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("email", $con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO email_data (name,email) VALUES ('$name','$email')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>
 
 
This has me pulling my hair out. Need help, please.

Thanks to all!

logiclab
Last edited by logiclab on Thu Sep 03, 2009 4:37 pm, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Name and Email collection form, MYSQL and the PHP problem

Post by Benjamin »

:arrow: Moved to PHP - Code
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: send form data to a mysql database using php?

Post by jackpf »

(jvassgal_email).mysql.jvassgallery.com
That's really your mysql server?
logiclab
Forum Newbie
Posts: 3
Joined: Fri Aug 14, 2009 3:26 pm

Re: send form data to a mysql database using php?

Post by logiclab »

Need help!

I dont know php

and am trying to follow a tutorial on sending form data to a mysql db using php. I have provided everything I have but am not sure if all is set up correctly.

If someone sees a major problem, please tell me and I can contact the host with the question.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: send form data to a mysql database using php?

Post by jackpf »

You need to find out what your mysql server is called.
logiclab
Forum Newbie
Posts: 3
Joined: Fri Aug 14, 2009 3:26 pm

Re: send form data to a mysql database using php?

Post by logiclab »

I found out the name of the mysql server is localhost. I tried using localhost but get this error

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'MYLOGIN'@'localhost' (using password: YES) in /home/jvassgal/public_html/formindb.php on line 2
Could not connect: Access denied for user 'MYLOGIN'@'localhost' (using password: YES)

- - -
this is the php code. I replaced my login/pswd.

Code: Select all

 
<?php
$con = mysql_connect("localhost","MYLOGIN","MYPSWD"); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("email", $con); //Replace with your MySQL DB Name
$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$email=mysql_real_escape_string($_POST['email']); //This value has to be the same as in the HTML form file
$sql="INSERT INTO email_data (name,email) VALUES ('$name','$email')"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
 die('Error: ' . mysql_error());
}
echo "The form data was successfully added to your database.";
mysql_close($con);
?>
 
thanks for your help.

logiclab
Last edited by Weirdan on Sat Aug 15, 2009 7:27 am, edited 1 time in total.
Reason: added [code=php] tags
User avatar
swhistlesoft
Forum Newbie
Posts: 9
Joined: Fri Aug 14, 2009 10:53 pm
Location: Ontario

Re: send form data to a mysql database using php?

Post by swhistlesoft »

From the error message:
Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'MYLOGIN'@'localhost' (using password: YES) in /home/jvassgal/public_html/formindb.php on line 2
Could not connect: Access denied for user 'MYLOGIN'@'localhost' (using password: YES)
Your login and password are either incorrect or permissions for that account is not configured properly.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: send form data to a mysql database using php?

Post by jackpf »

In regard to the latest post, yes, you're probably using the wrong username and/or password. No one here can help you with that, you'll have to ask your hosts.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: send form data to a mysql database using php?

Post by Weirdan »

:!: Meta-talk removed. Stay on topic please.
Post Reply