Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //
- // Example database connectivity w PHP and MySQL
- // Written by Mikael Hasselmalm / Mid Sweden University 2011
- //
- $dbhost = 'localhost';
- $dbuser = '';
- $dbpass = '';
- $dbname = 'test';
- //
- // Connect to the mysql database
- $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to mysql.");
- $db_connected = mysql_select_db($dbname);
- //
- // Insert tuple into database table
- //mysql_query("INSERT INTO tabell (Name, Comment, Date) VALUES('Mikael Mickel', '060-123456', '2011-02-18' ) ") or die(mysql_error());
- ?>
- </table>
- <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
- <tr>
- <form id="form1" name="form1" method="post" >
- <td>
- <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
- <tr>
- <td width="117">Signatur</td>
- <td width="14">:</td>
- <td width="357"><input name="name" type="text" id="name" size="40" /></td>
- </tr>
- <td valign="top">Meddelande</td>
- <td valign="top">:</td>
- <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
- </tr>
- <tr>
- <td> </td>
- <td> </td>
- <td> <input type="submit" value='submit record'> <input type="reset" name="Submit2" value="Reset" /></td>
- </tr>
- </table>
- </td>
- </form>
- </tr>
- </table>
- <?php
- //
- // Update record
- if (isset($_POST['submit'])){
- $host="localhost"; // Host name
- $username=""; // Mysql username
- $password=""; // Mysql password
- $db_name="test"; // Database name
- $tbl_name="tabell"; // Table name
- mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
- mysql_select_db("$db_name")or die("cannot select DB");
- $name = $_POST['name'];
- $comment = $_POST['comment'];
- $datetime=date("y-m-d h:i:s");
- $sql="INSERT INTO $tbl_name(name, comment, date)VALUES('$name', '$comment', '$datetime')";
- $result=mysql_query($sql);
- }
- //$sql = "INSERT INTO tabell(name, comment, date) VALUES ('$name', '$comment', '$date')";
- //mysql_query("INSERT INTO tabell (Name, Comment, Date) VALUES('$name', '$comment', '$date' ) ") or die(mysql_error());
- // mysql_query("UPDATE tabell SET Name = 'Kalle Karlsson' WHERE Date = '2011-02-18'") or die(mysql_error());
- //
- // Read all records from database
- if ($db_connected) {
- $query = "SELECT * FROM tabell";
- $result = mysql_query($query);
- print "<table><tr><td>ID</td><td>Name</td><td>Number</td><td>Date</td><tr>";
- while ($row = mysql_fetch_assoc($result)) {
- print "<td>" . $row["Name"] . "</td>";
- print "<td>" . $row["Comment"] . "</td>";
- print "<td>" . $row["Date"] . "</td></tr>";
- }
- print "</table>";
- }
- //
- // Close database connection
- mysql_close($conn);
- ?>
Add Comment
Please, Sign In to add comment