chaddsuk Posted January 27, 2008 Share Posted January 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/88036-solved-data-not-submitting-to-database-please-help/ Share on other sites More sharing options...
PHP Monkeh Posted January 27, 2008 Share Posted January 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/88036-solved-data-not-submitting-to-database-please-help/#findComment-450405 Share on other sites More sharing options...
chaddsuk Posted January 27, 2008 Author Share Posted January 27, 2008 brilliant, thanks alot mate, worked a treat Quote Link to comment https://forums.phpfreaks.com/topic/88036-solved-data-not-submitting-to-database-please-help/#findComment-450407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.