Jump to content

Adding data onto a db from a form.


kayla

Recommended Posts

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

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.