Jump to content

Simple PHP scripts which connects to DB & enteres Info entered into form


Guest vinod
 Share

Recommended Posts

Hey guys, i am a beginner, i have made three scripts, First one contains form with 3 fields username, email & password & i have set the form action to insert.phpCode is here:<html><head><title>Registration Form</title><script type="text/JavaScript"><!--function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document); if(!x && d.getElementById) x=d.getElementById(n); return x;}function MM_validateForm() { //v4.0 var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments; for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args); if (val) { nm=val.name; if ((val=val.value)!="") { if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@'); if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- '+nm+' must contain a number.\n'; if (test.indexOf('inRange') != -1) { p=test.indexOf(':'); min=test.substring(8,p); max=test.substring(p+1); if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n'; } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; } } if (errors) alert('The following error(s) occurred:\n'+errors); document.MM_returnValue = (errors == '');}//--></script></head><style type="text/css"><!--.style1 {color: #FFFFFF}--></style><form id="form1" name="register form method="post" action="insert.php"> <table width="500" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#99CC33"> <tr> <td width="203"><span class="style1">Username</span></td> <td width="291"><input name="username" type="text" id="username" /></td> </tr> <tr> <td><span class="style1">Email</span></td> <td><input name="email" type="text" id="email" /></td> </tr> <tr> <td><span class="style1">Password</span></td> <td><input name="password" type="password" id="password" /></td> </tr> <tr> <td> </td> <td><input name="Submit" type="submit" onclick="MM_validateForm('username','','R','email','','RisEmail','password','','R');return document.MM_returnValue" value="Submit" /></td> </tr> </table> </form></html>second one is insert.php the code is<?php$con = mysql_connect("localhost","root","root");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("users", $con);$sql="INSERT INTO users (username, email, password)VALUES('$_POST[username]','$_POST','$_POST[password]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }echo "1 record added";mysql_close($con)?>Third one is here where i ahve to view my entered values using HTMl TD Tag.Code is here :<?php$con = mysql_connect("localhost","root","root");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("users", $con);$result = mysql_query("SELECT * FROM users");echo "<table border='1'><tr><th>username</th><th>email</th><th>password</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['username'] . "</td>"; echo "<td>" . $row['email'] . "</td>"; echo "<td>" . $row['password'] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($con);?>I have created Databse named "users" using PHPmyadmin, contains 1 table "users" which has 3 fileds i mentioned i connect to it successfully but when i try to view the records enetered by me it gives me blank records. It shows nothing entered.What should i do ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...