Jump to content

idiot needs help


sammyconn

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

Archived

This topic is now archived and is closed to further replies.

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