PHP
From TechWiki
PHP by example...
Contents |
[edit]
PHP and MySQL
Example of a small PHP script that inputs data from a form, puts in in a MySQL database and retrieves it.
Test the code at:
[edit]
MySQL
The MySQL commands:
# Create database from command line mysqladmin -u root -p create dbName # Login to MySQL as root mysql -uroot -p #Create user privileges GRANT ALL PRIVILEGES ON dbName.* TO user@localhost IDENTIFIED BY 'password'; flush privileges; # Create table use dbName; CREATE TABLE data (id INT(10) UNSIGNED PRIMARY KEY AUTO_INCREMENT, datum DATETIME, surname VARCHAR(100), forname VARCHAR(100), email VARCHAR(255), misc VARCHAR(100));
[edit]
config.php
<?php // Global Variables $mysql_user = 'user'; $mysql_password = 'password'; $mysql_db = 'dbName'; $mysql_host= 'localhost'; $addr_tbl = 'data'; ?>
[edit]
index.php
The main file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Register</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> </head> <body> <?php if ($_POST['surname']): if (!$_POST['forname']) {echo "Bitte Vorname einfügen..."; exit();} if (!$_POST['email']) {echo "Bitte eMail Adresse einfügen..."; exit();} if (!$_POST['forename'] && !$_POST['email']) {echo "Bitte Vorname und eMail Adresse einfügen..."; exit();} include('config.php'); $dbcnx = @mysql_connect($mysql_host, $mysql_user, $mysql_password); if (!$dbcnx) {exit('<p>No connection to server....</p>');} if (!@mysql_select_db($mysql_db)) {exit('<p>No database available...</p>');} $surname = $_POST['surname']; $forname = $_POST['forname']; $email = $_POST['email']; $misc = $_POST['misc']; $datum = date('Y-m-d H:m:s'); $sql = "INSERT INTO $addr_tbl SET surname='$surname', forname='$forname', email='$email', datum='$datum', misc='$misc';"; if (@mysql_query($sql)) {echo '</br>Daten gespeichert...</br>';} else {echo '<p>' . mysql_error() . '</p>';} ?> <br/><br/> <p><b>Text!</b> <br/><br/> <?php else: ?> <h1>Registrierung</h1 <br> Next event: NY 06/07. <br> <br> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <hr size='1' color=#ffcc00> <h3>Deine Daten:</h3><br> <table> <tr><td><label>Mein Name: </td><td><input type="text" name="surname" value=""/> </label><br /></td></tr> <tr><td><label>Mein Vorname: </td><td><input type="text" name="forname" /></label><br /></td></tr> <tr><td><label>Meine e-Mail Adresse: </td><td><input type="text" name="email" /></label><br /></td></tr> <tr><td></td><td><br /></td></tr> <tr><td><label>Bemerkungen: </td><td><input type="text" name="misc" /></label> <br /></td></tr> <tr><td></td><td><br /></td></tr> <tr><td></td><td><input type="submit" value="Submit" /></td></tr> </table> <br /> <hr size='1' color=#ffcc00> </form> <br /> Alle Daten werden vertraulich behandelt und nicht an Dritte weitergegeben.<br/><br/> <br /> <?php endif; ?> </body> </html>
[edit]
admin.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<title>noonDB - addressBase</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php
include('config.php');
$dbcnx = @mysql_connect($mysql_host, $mysql_user, $mysql_password);
if (!$dbcnx) {
exit('<p>Server not accessible...</p>');
}
if (!@mysql_select_db($mysql_db)) {
exit('<p>Databes not found...</p>');
}
$myaddresses = @mysql_query('SELECT * FROM ' . $addr_tbl . ' order by datum asc;');
if (!$myaddresses) {
exit('<p>Error: ' . mysql_error() . '</p>');
}
$str1 = sprintf("<table id='tbl' border='0' cellpadding='5' cellspacing='5' width=120%% style='border-collapse: collapse'>");
$str1 = sprintf("%s\n <tr><td><b>Id</td><td><b>Datum</td><td><b>Name</b></td><td><b>Vorname</b></td><td><b>eMail</b></td><td><b>Bemerkungen</b></td></tr>",$str1);
while ($myaddress = mysql_fetch_array($myaddresses)) {
$id = $myaddress['id'];
$surname = $myaddress['surname'];
$forname = $myaddress['forname'];
$email = $myaddress['email'];
$datum = $myaddress['datum'];
$misc = $myaddress['misc'];
$str2 = sprintf("%s\n <tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",$str2,
$id, $datum, $surname, $forname, $email, $misc);
}
echo $str1 . $str2 . "</table><br/><br/>";
?>
</body>
</html>
[edit]
Send eMail
Sends email notification. Use
include('http://www.../mail.php');
[edit]
mail.php
<?php
// The message
$message = "http://www.../list.php";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);
$headers = 'From: my@mail.ch' . "\r\n" .
'Reply-To: my@mail.ch' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send
mail('my@mail.ch', '!!!! Text !!!!', $message, $headers);
?>
