chaddsuk Posted January 27, 2008 Share Posted January 27, 2008 Hi Im having trouble inserting data into my database using a html form, can someone take a look at where im am going wrong, i think i have my syntax wrong within my table. copied and pasted below cheers chris <?php //Assign contents of form to variables $name = $_POST['name']; $age = $_POST['age']; ?> <center><form action="<?PHP ($_SERVER[DOCUMENT_SELF]) mysql_query ( "INSERT INTO users (name, age) VALUES ('$name','$age')"; ?>" method="post"> <input type="text" name="name" size="20" /> Name<br /> <input type="text" name="age" size="20" /> Age<br /> <input type="submit" value="Store in database" /><input type="reset" value="Reset fields" /> </form></center>[/color] Quote Link to comment https://forums.phpfreaks.com/topic/88033-insert-into-database-through-a-form/ Share on other sites More sharing options...
chaddsuk Posted January 27, 2008 Author Share Posted January 27, 2008 Ok realised i completely messed up the form, its still wrong but am i getting closer? <center><form action="<?PHP (echo $_SERVER[php_SELF]) mysql_query ("INSERT INTO users (name, age) VALUES ('$name','$age')}"?> method="post"> <input type="text" name="name" size="20" /> Name<br /> <input type="text" name="age" size="20" /> Age<br /> <input type="submit" value="Store in database" /><input type="reset" value="Reset fields" /> </form></center> Quote Link to comment https://forums.phpfreaks.com/topic/88033-insert-into-database-through-a-form/#findComment-450398 Share on other sites More sharing options...
therealwesfoster Posted January 27, 2008 Share Posted January 27, 2008 This may help: http://www.w3schools.com/php/php_mysql_insert.asp Quote Link to comment https://forums.phpfreaks.com/topic/88033-insert-into-database-through-a-form/#findComment-450401 Share on other sites More sharing options...
kankaro Posted January 29, 2008 Share Posted January 29, 2008 <?php //Assign contents of form to variables $name = $_POST['name']; $age = $_POST['age']; ?> Replace the code above with this <?php if(isset($_POST['submit'])){ $name = $_POST['name']; $age = $_POST['age']; include ('connect.php'); // this is where you connect to the database the connect.php contents the script to connect to the database. $myQuery = "INSERT INTO users VALUES (null, '$name', $age)"; $result = mysql_query($myQuery); if(mysql_affected_rows($dbc)>0){ echo "Successfully Added"; } else{ echo "Process failed. Error ecounter"; } } ?> <center><form action="<?PHP ($_SERVER[DOCUMENT_SELF]) mysql_query ( "INSERT INTO users (name, age) VALUES ('$name','$age')"; ?>" method="post"> <input type="text" name="name" size="20" /> Name <input type="text" name="age" size="20" /> Age <input type="submit" value="Store in database" /><input type="reset" value="Reset fields" /> </form></center>[/color] and alse this one, you don't have to put query into a form it will drive you crazy. replace it with this one <?php echo $_SERVER['PHP_SELF']; ?> -- this will tell the browser to add to this page.. i hope it will help you.. please read books about MySQL and PHP and also read some online toturials. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <table> <tr> <td>NAme:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Age:</td> <td><input type="text" name="age"></td> </tr> <tr> <td><input type="submit name="submit" value="Add"></td> <td><input type="reset" name="clear" value="Reset"></td> </tr> </table></form> Quote Link to comment https://forums.phpfreaks.com/topic/88033-insert-into-database-through-a-form/#findComment-451999 Share on other sites More sharing options...
kankaro Posted January 29, 2008 Share Posted January 29, 2008 here it goes the whole code. I hope it will help you. <?php if(isset($_POST['submit'])){ $name = $_POST['name']; $age = $_POST['age']; include ('connect.php'); // this is where you connect to the database the connect.php contents the script to connect to the database. $myQuery = "INSERT INTO users VALUES (null, '$name', $age)"; $result = mysql_query($myQuery); if(mysql_affected_rows($dbc)>0){ echo "Successfully Added"; } else{ echo "Process failed. Error ecounter"; } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <table> <tr> <td>Name:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Age:</td> <td><input type="text" name="age"></td> </tr> <tr> <td><input type="submit name="submit" value="Add"></td> <td><input type="reset" name="clear" value="Reset"></td> </tr> </table></form> Quote Link to comment https://forums.phpfreaks.com/topic/88033-insert-into-database-through-a-form/#findComment-452003 Share on other sites More sharing options...
legohead6 Posted January 29, 2008 Share Posted January 29, 2008 are your sure your connect page is connecting? are you getting errors? is it saying proccess failed? is there only 3 spots in the users table? if not you need to make blanks for each other spot.. also that null might have been messing it try this(note 2 single quotes, not 1 double) $myQuery = "INSERT INTO users VALUES ('', '$name', '$age')"; Quote Link to comment https://forums.phpfreaks.com/topic/88033-insert-into-database-through-a-form/#findComment-452023 Share on other sites More sharing options...
kankaro Posted January 29, 2008 Share Posted January 29, 2008 i tried it and it makes work great. that "null" there means that hes/her primary key in the database is auto incremented the "null" and '' are d same there's nothing wrong with it. also that connecting.php there i assume that chaddsuk has know it. This is the contents of that connecting.php <?php DEFINE ('DB_HOST', 'localhost'); DEFINE ('DB_USER', 'root'); DEFINE ('DB_PASSWORD', 'password'); DEFINE ('DB_NAME', 'database name'); //Connect to MySQL and select the database: $dbc = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die ('Dili ma Connect sa MySQL! sayop ang bangag:' . mysql_error()); mysql_select_db(DB_NAME); ?> and also it will say process failed if during the process it will counter an error. also it's not necessary to put a single quote in $age becoz im sure that it is an integer type not a varchar. it will cause error during the process if you put it. A variable that is assigned as a storage of the integer data inputed doesn't need to have a single quote on them except if it is a varchar type. why don't you try it. so that you can see. Try to insert an integer to the database having a single quote it will sure prompt an error. Quote Link to comment https://forums.phpfreaks.com/topic/88033-insert-into-database-through-a-form/#findComment-452092 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.