PHP code examples with database connectivity

ASP.TheDemoSite.co.uk

PHP code examples with database connectivity
 PHP.TheDemoSite.co.uk

Just examples of PHP code, linking to your MySQL database and JavaScript.
1. Home | 2. The Database | 3. Add a User | 4. Login | 5. Get your db online

PHP code examples with database connectivity

PHP Form Mailer - phpFormMailer (easy to use and more secure than many cgi form mailers)

Need web hosting try: PHP hosting or need to host several domain names? Try Reseller hosting

PHP MySQL auto members area: phpAutoMembersArea (phpAMA)

PHP Job scheduler: phpJobScheduler

download-the-login-demo-pages FREE Download
complete LOGIN
demo pages
of this website
Size: 27 Kbytes

This page shows the PHP and JavaScript code used in the "Add a User" section.

addauser.php

<?php include("header.inc"); ?>
<?php include("validateform.inc"); ?>
<p align="center"><big><strong>3. Add a User</strong></p></big>
<blockquote>
<blockquote>
<p>Below is the current single record within the database: </p>
</blockquote>
<blockquote>
<blockquote>
<?php include("config.php"); ?>
<?php include("getrecord.inc"); ?>
</blockquote>
</blockquote>
<blockquote>
<p>Add your own username and password, enter your details below and click save.&nbsp; <strong>DO
NOT</strong> use valid details as the information you enter will be displayed above and
the next visitor will be able to view them as you are now.</p>
</blockquote>
</blockquote>

<form
ACTION="savedata.php"
name="saveform" METHOD="POST" align="center">
<div align="center"><center><table border="0" width="93%" cellspacing="0" cellpadding="0">
<tr>
<td width="52%"><div align="center"><center><table border="0" height="59" width="310"
bgcolor="#808080" cellspacing="1" cellpadding="0">
<tr>
<td width="248" height="19" bgcolor="#C0C0C0"><div align="right"><p><font color="#000000"><small>Add
Your test username:</small></font></td>
<td width="123" height="19" bgcolor="#C0C0C0"><div align="left"><p><input NAME="username"
VALUE SIZE="8" MAXLENGTH="16" tabindex="1"></td>
<td width="47" height="19" align="center" bgcolor="#C0C0C0"><div align="center"><center><p><a
href="javascript:alert('The username must be between 4 and 16 characters long.')"><small><small>Help</small></small></a></td>
</tr>
<tr align="center">
<td width="248" height="17" bgcolor="#C0C0C0"><div align="right"><p><font color="#000000"><small>Add
Your test password:</small></font></td>
<td height="17" width="123" bgcolor="#C0C0C0"><div align="left"><p><input type="password"
name="password" size="8" tabindex="2" maxlength="8"></td>
<td width="47" height="17" align="center" bgcolor="#C0C0C0"><a
href="javascript:alert('The password must be between 4 and 8 characters long.')"><small><small>Help</small></small></a></td>
</tr>
<tr align="center">
<td width="248" height="1" bgcolor="#C0C0C0"></td>
<td width="123" height="1" bgcolor="#C0C0C0"><div align="left"><p><input TYPE="button"
NAME="FormsButton2" VALUE="save" ONCLICK="validateForm()" tabindex="3"></td>
<td width="47" height="1" align="center" bgcolor="#C0C0C0"><a
href="javascript:alert('Click to save the details')"><small><small>Help</small></small></a></td>
</tr>
</table>
</center></div></td>
<td width="48%" align="center"><small>When you have added your own username and password <a
href="login.php">move
onto the Login page to test it!</a></small></td>
</tr>
</table>
</center></div>
</form>

<p align="center">&nbsp;</p>

<p align="center"><a
href="addausercode.php">Click
here to view the PHP and JavaScript code</a> used for this page.</p>

<?php include("footer.inc"); ?>

 

validateform.inc

 

 

config.php

<?php

$mysql_database="a_db"; // change to your own
$mysql_username="a_username"; // change to your own
$mysql_password="a_password"; // change to your own

$link = mysql_connect("localhost",$mysql_username,$mysql_password) or die ("Unable to connect to SQL server");
mysql_select_db($mysql_database,$link) or die ("Unable to select database");

?>

 

getrecord.inc

<?php
// version 1.1
$qstr = "SELECT * from members where id = '1' ";
$result = mysql_query($qstr);

if (mysql_num_rows($result)) // check login info is correct
{
$username = mysql_result($result,0, "username");
$password = mysql_result($result,0, "password");
echo "<b>The username:</b> $username<br>";
echo "<b>The password:</b> $password<br>";
}
else echo "ERROR - unable to find current username and password!";

mysql_close();
?>

 

savedata.php

<?php
include("config.php");
$qselect = "Select * from members where id = '1'";

function clean_input($string)
{
$patterns[0] = "/'/";
$patterns[1] = "/\"/";
$string = preg_replace($patterns,'',$string);
$string = trim($string);
$string = addslashes($string);
return preg_replace("/[<>]/", '_', $string);
}


$username = clean_input($_POST['username']);
$password = clean_input($_POST['password']);

$qadd = "
INSERT INTO members
(id, password, username)
VALUES ('1', 'test', 'test') ";
$qupdate = "
UPDATE members
SET password = '$password',
username = '$username'
WHERE id = '1'";

$result = mysql_query($qselect);

// check the single record exists
if (mysql_num_rows($result)) $result = mysql_query($qupdate); // update original record
else $result = mysql_query($qadd); // if not add an initial record

if ($result) mysql_close();
else
{
echo "ERROR - unable to save new username and password!<br>";
$SQLError = "SQL ERROR: ".mysql_errno().". ".mysql_error()."<BR><BR>";
echo "$SQLError";
mysql_close();
}
include("addauser.php");
?>

< Go Back<

UK managed dedicated servers
No hassle managed dedicated servers
No bandwidth limits
www.S4Servers.com















download-the-login-demo-pages FREE Download
complete LOGIN
demo pages
of this website
Size: 27 Kbytes
download-phpFormMailer FREE Download phpFormMailer
including setup details
Size: 4 Kbytes



TheDemoSite.co.uk is a MySQL database and PHP FREE code example site hosted with:
Seiretto website hosting Copyright © 1996-2008 Seiretto Contact us.  All rights reserved.