Guest User

Untitled

a guest
Jan 19th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.77 KB | None | 0 0
  1. <?php
  2. //
  3. // Example database connectivity w PHP and MySQL
  4. //  Written by Mikael Hasselmalm / Mid Sweden University 2011
  5. //
  6.   $dbhost = 'localhost';
  7.   $dbuser = '';
  8.   $dbpass = '';
  9.   $dbname = 'test';
  10.  
  11. //
  12. // Connect to the mysql database
  13.   $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to mysql.");
  14.   $db_connected = mysql_select_db($dbname);
  15.  
  16.  
  17.  
  18. //
  19. // Insert tuple into database table
  20.   //mysql_query("INSERT INTO tabell (Name, Comment, Date) VALUES('Mikael Mickel', '060-123456', '2011-02-18' ) ") or die(mysql_error());  
  21. ?>
  22.  
  23.   </table>
  24. <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
  25. <tr>
  26. <form id="form1" name="form1" method="post" >
  27. <td>
  28. <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
  29. <tr>
  30. <td width="117">Signatur</td>
  31. <td width="14">:</td>
  32. <td width="357"><input name="name" type="text" id="name" size="40" /></td>
  33. </tr>
  34. <td valign="top">Meddelande</td>
  35. <td valign="top">:</td>
  36. <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
  37. </tr>
  38. <tr>
  39. <td>&nbsp;</td>
  40. <td>&nbsp;</td>
  41. <td> <input type="submit" value='submit record'> <input type="reset" name="Submit2" value="Reset" /></td>
  42. </tr>
  43. </table>
  44. </td>
  45. </form>
  46. </tr>
  47. </table>
  48.  
  49.   <?php
  50. //
  51. // Update record
  52.  
  53. if (isset($_POST['submit'])){
  54.  
  55. $host="localhost"; // Host name
  56. $username=""; // Mysql username
  57. $password=""; // Mysql password
  58. $db_name="test"; // Database name
  59. $tbl_name="tabell"; // Table name  
  60.  
  61.  
  62.  
  63. mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
  64. mysql_select_db("$db_name")or die("cannot select DB");
  65.  
  66. $name = $_POST['name'];
  67. $comment = $_POST['comment'];
  68. $datetime=date("y-m-d h:i:s");
  69.  
  70.  
  71.  
  72. $sql="INSERT INTO $tbl_name(name, comment, date)VALUES('$name', '$comment', '$datetime')";
  73. $result=mysql_query($sql);
  74.  
  75.  }
  76.  
  77.  
  78.   //$sql = "INSERT INTO tabell(name, comment, date) VALUES ('$name', '$comment', '$date')";
  79.  
  80.   //mysql_query("INSERT INTO tabell (Name, Comment, Date) VALUES('$name', '$comment', '$date' ) ") or die(mysql_error());  
  81.  
  82.  
  83.  
  84.  // mysql_query("UPDATE tabell SET Name = 'Kalle Karlsson' WHERE Date = '2011-02-18'") or die(mysql_error());  
  85.  
  86.  
  87.  
  88.  
  89. //
  90. // Read all records from database
  91.   if ($db_connected) {
  92.  
  93.     $query = "SELECT * FROM tabell";
  94.     $result = mysql_query($query);
  95.  
  96.     print "<table><tr><td>ID</td><td>Name</td><td>Number</td><td>Date</td><tr>";
  97.     while ($row = mysql_fetch_assoc($result)) {
  98.      
  99.       print "<td>" . $row["Name"] . "</td>";
  100.       print "<td>" . $row["Comment"] . "</td>";
  101.       print "<td>" . $row["Date"] . "</td></tr>";
  102.     }
  103.    
  104.     print "</table>";
  105.   }
  106.  
  107. //
  108. // Close database connection
  109.   mysql_close($conn);
  110.  
  111. ?>
Add Comment
Please, Sign In to add comment