kayla Posted December 8, 2009 Share Posted December 8, 2009 Hi, As part of a uni project we have to create a system where users can enter their registration details into a form and they are then saved onto a database. We've did something very similar before in class for adding cars to a database, which worked fine. However, when we have came to edit this coding so it reflects our project, it just doesn't seem to work. We've tried debugging it by adding echo statements and found that the code goes through until the end. Heres the code that used on the registration form: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Registration</title> <link href="mystyles.css" rel="stylesheet" type="text/css" /> </head> <body> <form method="get" action="add_student_to_database.php"> <center> <table width="500" border=2 bgcolor="#CDCDCD" cellpadding="4" cellspacing="0"> <tr> <td colspan="2" bgcolor="#6495ED"><p><b>New Student</b></td> </tr> <tr> <td align="left"><p>Forename:</td> <td> <input name="StudentForename" size="20" maxlength="20"> </td> </tr> <tr> <td align="left"><p>Surname:</td> <td> <input name="StudentSurname" size="20"> </td> </tr> <tr> <td align="left"><p>Email Address:</td> <td> <input name="StudentEmail" size="50"> </td> </tr> <tr> <td align="left"><p>Password:</td> <td> <input name="StudentPassword" size="20"> </td> </tr> <tr> <td colspan=2 align="right"> <input type="submit" value="Register"> </td> </tr> </table> </center> <input name="addStudent" type="hidden" value="1"> </form> </body> </html> And this is the code that we are using to add it to the database: <?php //read form values $Fforename = $_GET['StudentForename']; $Fsurname = $_GET['StudentSurname']; $Femail = $_GET['StudentEmail']; $Fpassword = $_GET['StudentPassword']; $db_host='127.0.01'; $db_database='registration_81646'; $db_username='student'; $db_password='college'; //create connection $connection= mysql_connect($db_host, $db_username, $db_password); if(!$connection){ die ("Could not connect to the database: <br />".mysql_error()); } //select database $db_select = mysql_select_db($db_database); if (!$db_select) { die ("Could not select the database: <br />".mysql_error()); } //declare the SQL statement that will query the database $query = "INSERT INTO cars (forename, surname, email, password) VALUES ('$Fforename', '$Fsurname', '$Femail', '$Fpassword')"; //execute query mysql_query( $query); //close connection mysql_close($connection); ?> If anybody could help it would be much appreciate! <3 Link to comment https://forums.phpfreaks.com/topic/184380-adding-data-onto-a-db-from-a-form/ Share on other sites More sharing options...
trq Posted December 8, 2009 Share Posted December 8, 2009 You should at least check your query. if (!mysql_query( $query)) { echo mysql_error() . "<br />" . $query; } Link to comment https://forums.phpfreaks.com/topic/184380-adding-data-onto-a-db-from-a-form/#findComment-973327 Share on other sites More sharing options...
kayla Posted December 8, 2009 Author Share Posted December 8, 2009 Thanks. I'll try that. Could it have anything to do with the structure of the table in the db? Since I have an extra id field which is automated and a usertype field which the admin will edit. Link to comment https://forums.phpfreaks.com/topic/184380-adding-data-onto-a-db-from-a-form/#findComment-973330 Share on other sites More sharing options...
kayla Posted December 8, 2009 Author Share Posted December 8, 2009 Omg. I did something really noobish and had it inserting into the wrong table. Haha, sorry! It works now but when I enter data, it goes in twice. This anything to do with my code? Link to comment https://forums.phpfreaks.com/topic/184380-adding-data-onto-a-db-from-a-form/#findComment-973335 Share on other sites More sharing options...
kayla Posted December 8, 2009 Author Share Posted December 8, 2009 FIXED IT Thanks anyways. I was just being a noob. Link to comment https://forums.phpfreaks.com/topic/184380-adding-data-onto-a-db-from-a-form/#findComment-973337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.