Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

form to database mysql problem

New Here ,
Jan 01, 2010 Jan 01, 2010

I am trying to set up a form to to send info to a database. I have set up my database called contact_form, with 2 fields name and email. I use awardspace.co.uk (free) but i cant seem get it to work. can someone look at both my form and PHP codes and see where  i am going wrong. thanks

FORM CODE

<html>
<head>
<title>Formmail in DB</title>
</head>
<body>
<!-------------begin form------------>
<FORM ACTION="formindb.php" METHOD="POST" NAME="contact_form">
<TABLE>
<TR>
  <TD><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Your Name:</font></TD>
  <TD> <input type=text name="name"></TD>
</TR>
<TR>
  <TD><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Email Address:</font></TD>
  <TD><input type=text name="email"></TD>
</TR>
<TR>
  <TD><input type="submit" value="Submit" name="Submit"></TD>
  <TD><input type="reset" value="Reset" name="Reset"></TD> 
</TR>
</TABLE>
</FORM>
<!-------------end form------------>
</body>
</html>


PHP CODE (mysql_connect("(database_name).mysql.aplus.net - i am not sure if this is correct if i want to connect to my awardspace mysql???)

<?php
$con = mysql_connect("(database_name).mysql.aplus.net","MYDatabaseUsername","MYDatabasePassword"); //Replace with your actual MySQL DB Username and Password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("contact_form", $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 contact_form (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);
?>
TOPICS
Server side applications
768
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 01, 2010 Jan 01, 2010

This thread has been moved to the Dreamweaver Application Development forum, which deals with PHP and other server-side issues.

What do you mean when you say it doesn't "seem to work"? Do you get any error messages? Are you testing this on your local computer or on your website? Most hosting companies do not permit remote access to the database. Also, the name of the database server depends on what the hosting company has told you to use. In most cases, it's simply "localhost".

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 01, 2010 Jan 01, 2010

thanks for your reply

error message:

Warning:  mysql_connect(): Unknown MySQL server host '(database_name).mysql.aplus.net' (1) in /home/www/videoadvantage.awardspace.co.uk/Pages/formindb.php on line 2
Could not connect: Unknown MySQL server host '(database_name)fdb2.awardspace.com' (1)

i have uploaded both php and html form to my pages folder via ftp. im using mysql that awardspace provides. i made a table called patient.

details of my database:

#DB HostDB NameDB UserDB VersionSpace QuotaUsed SpaceOptions
1fdb2.awardspace.compatientpatient_contactMySQL 5.110 MB
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 01, 2010 Jan 01, 2010

$con = mysql_connect("fdb2.awardspace.com","patient_contact","password")

  or die(mysql_error());


mysql_select_db('patient', $con);

// rest of script

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 01, 2010 Jan 01, 2010

NEW PHP SCRIPT:

<?php
$con = mysql_connect("fdb2.awardspace.com","patient_contact","password")
  or die(mysql_error(cannot connect));


mysql_select_db('patient', $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 contact_form (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);
?>

NEW ERROR:


Parse error:  syntax error, unexpected T_STRING in /home/www/videoadvantage.awardspace.co.uk/Pages/formindb.php on line 3

thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 01, 2010 Jan 01, 2010

ok i have fixed it forgot the ' '.

now i have

ERROR: no database selected

in my browser, but it is defiently the correct name of db

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 01, 2010 Jan 01, 2010
LATEST

Try changing this:

mysql_select_db('patient', $con);

to this:

mysql_select_db('patient', $con) or die(mysql_error());

That should tell you what happens when you try to select the database.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines