Jump to content

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


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.

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.