Jump to content

Recommended Posts

i know nothing about php or mysql so keep any answers simple please! ???

 

I built a website for my missus and want to collect submitted form data to a mysql database provided by the hosting company. Can't access the database unless online. I created a table in the database called contact_us with seven fields.

 

I inserted this code before any html code:

 

<?php
$db_host = "localhost";
$db_user = "";
$db_pwd = "";
$db_name = "";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);
?>

 

obviously using my own username and password between the " "

 

 

in the body of the html, i inserted this code for my form:

 

<?php
if (!isset($_POST['submit'])) {
?>
<form action="" method="post">
Name: <input type="text" name="name"><br>
Address 1: <input type="text" name="address1"><br>
Address 2: <input type="text" name="address2"><br>
Postcode: <input type="text" name="postcode"><br>
Telephone: <input type="text" name="telephone"><br>
Email: <input type="text" name="email"><br>
Comments: <input type="textarea" name="comments"><br>
<input type="submit" name="submit" value="Submit!">
</form>
<?php
} else {
$name = $_POST['name'];
$address1 = $_POST['address1'];
$address2 = $_POST['address2'];
$postcode = $_POST['postcode'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
mysql_query("INSERT INTO `contact_us` (name, address1, address2, postcode, telephone, email, comments) VALUES ('$name', '$address1', '$address2','$postcode','$telephone','$email','$comments',)");
echo "Thank you, your information has been added.";
}
?>

 

needless to say it doesn't work. Can anybody point me in the right direction? once the form is working, how do i then view information submitted to the contact us table?

 

any help much appreciated. ;D

Link to comment
https://forums.phpfreaks.com/topic/38221-idiot-needs-help/
Share on other sites

make sure you connect to MySQL in your second code block. If you are not connected to MySQL the query will fail to work as there is no connection.

 

What you should do is save your mysql connection code in a file called db.inc.php. Then include this file when you need to connect to the database like so:

include 'db.inc.php';

Link to comment
https://forums.phpfreaks.com/topic/38221-idiot-needs-help/#findComment-183053
Share on other sites

You also need to make sure that the mysql extension is actually loaded. In PHP5 it isn't by default. I'm not sure about PHP4. Check your php.ini to see if extension=php_mysql.dll (Windows) or extension=php_mysql.so (Linux - not sure if it has the php_ prefix) is present (without a semi-colon in the front).

Link to comment
https://forums.phpfreaks.com/topic/38221-idiot-needs-help/#findComment-183058
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.