Jump to content

[SOLVED] Data not submitting to database, please help :(


chaddsuk

Recommended Posts

Hi im having issues with adding data to my database, as far as i can see everything looks fine but it just isnt working, can someone please take a look at it....

 

<?PHP

if (isset($_POST["submit"])) {

//DB Variables

$user = "root"; #username

$pass = "*****"; #password

$dbname = "test"; #database name

$tablename = "users"; #table name

 

$dbh=mysql_connect ("localhost", "$user", "$pass") or die ('I cannot connect to the database because: ' . mysql_error());

mysql_select_db ("$dbname");

 

$query = "INSERT INTO $tablename VALUES ('','$_POST[name]','$_POST[age]','$_POST[Comments]','$_POST')"; #should be one line

 

mysql_db_query ($dbname, $query, $dbh);

mysql_close ($dbh);

}

?>

 

 

<form method="POST" action="<?PHP echo $_SERVER["PHP_SELF"]; ?>">

Name:<input type="text" name="name">

Age:<input type="text" name="age">

Comments:<input type="text" name="Comments">

Email:<input type="text" name="Email">

<input type="submit" name="submit" value="submit">

</form>

 

Thanks ahead of time for your help!

 

chris

A few things need changing really, give this a shot:

 

<?php

if (isset($_POST["submit"])) {
//DB Variables
$user = "root"; // username
$pass = "*****"; // password
$dbname = "test"; // database name
$tablename = "users"; // table name

mysql_connect ("localhost", $user, $pass) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($dbname);

$query = "INSERT INTO $tablename (id, name, age, comments, email) VALUES ('','".$_POST['name']."','".$_POST['age']."','".$_POST['Comments']."','".$_POST['Email']."')";

mysql_query ($query) or die(mysql_error());
// mysql_close ($dbh); You don't really need to close it as it closes automatically once the scripts finished running
}
?>

<form method="POST" action="<?PHP echo $_SERVER["PHP_SELF"]; ?>">
Name:<input type="text" name="name">
Age:<input type="text" name="age">
Comments:<input type="text" name="Comments">
Email:<input type="text" name="Email">
<input type="submit" name="submit" value="submit">
</form> 

 

I've taken a guess at what your table columns are called, make sure you change them if they're wrong.  Also please use code tags rather than putting everything in red.

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.