In MySQL I have created database "mydata" with the table "contacts". The fields are as follow:

ID (Primary Key) (INT(10)
Name VARCHAR(45)
Title VARCHAR(45)
Address VARCHAR(45)
Comments TEXT

Based on info at http://www.webmasterforums.com/php-development/1683-php-form-entering-data-mysql-database.html I have created following two files:

new_record.php

<? 
    $usr = "myusername"; 
    $pwd = "mypassword"; 
    $db = "mydata"; 
    $host = "localhost"; 

    # connect to database 
    $cid = mysql_connect($host,$usr,$pwd); 
    mysql_select_db($db); 
    if (mysql_error()) { print "Database ERROR: " . mysql_error(); } 

?> 
<html> 
<head> 
<title>Insert New Record</title> 
</head> 
<body bgcolor="#ffffff"> 

<h2>Insert New Record</h2> 

<? 
    # this is processed when the form is submitted 
    # back on to this page (POST METHOD) 
    if ($_SERVER['REQUEST_METHOD'] == "POST")  
    { 
        # escape data and set variables 
        $Name = addslashes($_POST["Name"]); 
        $Title = addslashes($_POST["Title"]); 
        $Address = addslashes($_POST["Address"]); 
        $NOTES = addslashes($_POST["Comments"]); 

        # setup SQL statement 
$sql  = " INSERT INTO contacts "; 
$sql .= " (Name, Title, Address, Comments) VALUES "; 
$sql .= " ('$Name','$Title','$Address','$Notes') "; 

        #execute SQL statement 
        $result = mysql_query($sql, $cid); 

        # check for error 
        if (mysql_error()) { print "Database ERROR: " . mysql_error(); } 

print "<h3><font color=red>New Job Added  - View it <a href=show_last_record.php>HERE</a></font></h3>"; 
} 

?> 

    <form name="fa" action="new_record.php" method="POST">      
    <table>                                                                                                         
    <tr><td><b>Name: </b> </td><td><input type="text" name="Name" size=30></td></tr>  
    <tr><td><b>Title: </b> </td><td><input type="text" name="Title" size=30></td></tr>  
    <tr><td><b>Address: </b> </td><td><input type="text" name="Address" size=20></td></tr> 
    <tr><td valign=top><b>Comments: </b> </td><td> <textarea name="Comments" rows=4 cols=30></textarea></td></tr> 
    <tr><th colspan=2><p><input type="submit" value="Add Record"></p></th></tr>  
    </table> 
    </form>                                       

</body> 
</html>

and

show_last_record.php

<? 
    $usr = "myusername"; 
    $pwd = "mypassword"; 
    $db = "mydata"; 
    $host = "localhost"; 

    # connect to database 
    $cid = mysql_connect($host,$usr,$pwd); 
    mysql_select_db($db); 
    if (mysql_error()) { print "Database ERROR: " . mysql_error(); } 

?> 
<html> 
<head> 
<title>This is Your New Record</title> 
</head> 
<body> 
<?php 

function drawtable($qr) { 
    // $qr = results of a mysql_query 
    $rows = mysql_num_rows($qr); 
    $toreturn = "<table style=\"float: left;\" border=\"2\">\n"; 
    $toreturn .= "<tr>\n"; 
    for ($i=0; $i<mysql_num_fields($qr); $i++) {  
        $toreturn .= "\t<th>".mysql_field_name($qr,$i)."</th>\n"; 
        } 
    $toreturn .= "</tr>\n"; 
    for ($i=0; $i<$rows; $i++) {  
        $row = mysql_fetch_row($qr); 
        $cols = sizeof($row); 
        $toreturn .= "<tr>\n"; 
        for ($x=0; $x < $cols; $x++) { 
            if ($row[$x]==NULL){ 
                $row[$x]='&nbsp;'; 
                } 
            $toreturn .= "\t<td>$row[$x]</td>\n"; 
            } 
        $toreturn .= "</tr>\n"; 
        } 
    $toreturn .= '</table>'; 
    return $toreturn; 
    } 
$q = stripslashes($_POST['q']); 
if (empty($q)) { 
    $q = 'select Name, Title from contacts order by Name desc limit 1;'; 
    } 
mysql_select_db("mydata"); 
$result = mysql_query($q); 

print "<b><font color=red>Your New Record Has Been Added</font></b>\n"; 

echo drawtable($result); 
echo '<table> 
</table> 
</body> 
</html>';

I use IIS 7 and IIS 6 and put both of these files to C:\inetpub\wwwroot. The form displays correctly in IE8. However, clicking "Add Record" button, makes data typed into the fields disappear with nothing else happening. No lastly added record shows up, and worst of all, no record is being entered into database at all. There are no errors either.

What do I do wrong? I am quiet new to MySQL and PHP.

Member Avatar for Rahul47

I think when your php code is executing on loading of page.
To make it execute after clicking Add Record:

if(isset($_POST['submit']) && !empty($_POST['submit']))
        {
            // Statements to be executed on button click.
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.