June 4th, 2010 by admin
I have created a php coding to insert data in to the my sql database table. But the records are not inserting.
I have created a form using html and wrote the coding in php to insert records in to the database table in mysql. But the data that I insert using the form are not inserting in to the table. But there is no error message even. Please help?
Here is my coding.
<html>
<head>
</head>
<body>
<table>
<form method="post" action="exe_2.php">
<tr>
<td>User Name</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="paword"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="register"></td>
</tr>
</table>
</form>
</body>
</html>
<?php
error_reporting(0);
$con = mysql_connect("localhost","root","root");
mysql_select_db("bit",$con);
$insert="INSERT INTO student
(StdNo,User Name,Password,Email) VALUES
(‘null’,'$_POST[name]‘,’$_POST[paword]‘,’$_POST[email]‘)";
//if (isset($_POST['submit']))
//{
mysql_query($insert,$con);
echo "your record added";
//}
$view="SELECT * FROM student";
$result=mysql_query($result,$con);
echo "<table border=’1′>
<tr>
<th>StdNo</th>
<th>User Name</th>
<th>Password</th>
<th>Email</th></tr>"
;
while ($display=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $display['StdNo'] . "</td>";
echo "<td>" . $display['User Name'] . "</td>";
echo "<td>" . $display['Password'] . "</td>";
echo "<td>" . $display['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Below you will find code that should help you solve problems with your original. Because I was unsure of your table definition, I based the code on the following schema:
CREATE TABLE student(
StdNo BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
Email VARCHAR(50),
User_Name VARCHAR(50),
Password VARCHAR(50),
last_update TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP);
In the above schema StdNo an auto incrementing field. I replaced the space with an underscore in the field name "User Name" because MySQL must operate in a special mode in order to understand field names with spaces. I also added a timestamp field so that you would know when the record was created or last upaded.
I made the registration form a function that you can call at any point in order to display the form.
If you have the correct schema in your database, you can load the code below into a single php page and it should work.
Note that I do echo the function mysql_error() to the user when an error is ecountered with the SQL call. This is not recommended for production as hackers might find important info from the displayed error.
********** THE CODE **********
<?php
function showRegForm() {
// *** This funtion displays a registration form.
$pageName = $_SERVER['PHP_SELF'];
echo("<html>
<head>
</head>
<body>
<table>
<form method=\"post\" action=\"$pageName\">
<tr>
<td>User Name</td>
<td><input type=\"text\" name=\"name\"></td>
</tr>
<tr>
<td>Password</td>
<td><input type=\"password\" name=\"paword\"></td>
</tr>
<tr>
<td>Email</td>
<td><input type=\"text\" name=\"email\"></td>
</tr>
<tr>
<td></td>
<td><input type=\"submit\" name=\"cmdSubmit\" value=\"register\"></td>
</tr>
</table>
</form>
</body>
</html>");
return true;
}
if (isset($_POST["cmdSubmit"])) {
// *** Submit button was pressed.
// *** Process registration.
// *** Connect to database or quit.
$con = mysql_connect("localhost","root","root") Or exit("<p><b>ERROR:</b> Could not connect to datbase.</p>\n");
// *** Select database.
mysql_select_db("bit",$con) Or exit("<p><b>ERROR:</b> Could not select target datbase (bit).</p>\n");
// *** Build registration query.
$userName = mysql_real_escape_string ($_POST["name"], $con);
$password = mysql_real_escape_string ($_POST["paword"], $con);
$email = mysql_real_escape_string ($_POST["email"], $con);
$insert = "INSERT INTO student (User_Name, Password, Email) VALUES (‘$userName’,'$password’, ‘$email’)";
// *** Run the query.
$result = mysql_query($insert, $con);
if (!$result) {
// *** Error creating record. Inform user and halt.
exit("<p><b>ERROR: Could not create record.</b> " . mysql_error() . "</p>");
} else {
echo("<p>Your record added.");
// *** Now output list of records.
$view = "SELECT * FROM student";
$result = mysql_query($view, $con);
if (!$result) {
// *** Error getting records. Inform user and halt.
echo("<p><b>ERROR: Could not retrieve list of records.</b> " . mysql_error() . "</p>");
} else {
// *** Show record list.
// *** Note: A bad idea to show this to users.
echo("<table border=’1′> \n
<tr> \n
<th>Created</th> \n
<th>StdNo</th> \n
<th>User Name</th> \n
<th>Password</th> \n
<th>Email</th></tr> \n");
while ($display = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td> " . $display['last_update'] . "</td>";
echo "<td> " . $display['StdNo'] . "</td>";
echo "<td> " . $display['User_Name'] . "</td>";
echo "<td> " . $display['Password'] . "</td>";
echo "<td> " . $display['Email'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
// *** Show registration form in case they want to register again.
echo("<h5>Register Another?</h5>");
$showForm = showRegForm();
}
// *** Close the db connection.
mysql_close($con);
} else {
// *** Submit button was NOT pressed, so show registration form.
$showForm = showRegForm();
}
?>
Technorati Tags: How to make a webpage in HTML, html, HTML Coding, PHP, PHP code, PHP Coding, php platform, website on php, your site sample
How to make a webpage in HTML, html, HTML Coding, PHP, PHP code, PHP Coding, php platform, website on php, your site sample