Advertisement
Guest User

Untitled

a guest
May 8th, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. $connection = DB::connect("mysql://$user:$pass@localhost");
  2. if (DB::isError($connection))
  3. {
  4. die ("Could not connect to the databasew: <br>".DB::errorMessage($connection));
  5. }
  6. $connection->setErrorHandling(PEAR_ERROR_DIE);
  7. $connection->query("create database IF NOT EXISTS $db_name");
  8. $connection->query("use $db_name");
  9. $connection->query("CREATE TABLE IF NOT EXISTS books (Title VARCHAR(255),Pages INT)");
  10. }
  11.  
  12.  
  13. function insertinto_db($title, $pages, $connection)
  14. {
  15. global $connection;
  16. $result = $connection->query('INSERT INTO books VALUES (?,?)',array($title,$pages));
  17. if (DB::isError($result))
  18. {
  19. die("Could not query the databaseb: <br>". $query." " .DB::errorMessage($result));
  20. }
  21. echo "Inserted OK.<br>";
  22. $connection->setFetchMode(DB_FETCHMODE_ASSOC);
  23. $query = "SELECT * FROM books";
  24. $result = $connection->query($query);
  25. if (DB::isError($result))
  26. {
  27. die("Could not query the databaseq: <br>". $query." " .DB::errorMessage($result));
  28. }
  29. echo '<table border="1">';
  30. echo "<tr><th>Title</th><th>Pages</th></tr>";
  31. while ($result_row = $result->fetchRow()) {
  32. echo "<tr><td>";
  33. echo $result_row["Title"] . '</td><td>';
  34. echo $result_row["Pages"] . '</td></tr>';}
  35. echo "</table>";
  36. $connection->disconnect( );
  37. }
  38. ?>
  39.  
  40.  
  41. <html> <head>
  42. <title>Inserting From a Form</title>
  43. </head> <body>
  44. <?php
  45. echo '<h1>Enter a new title:</h1>
  46. <form action="'.$_SERVER["PHP_SELF"].'" method="GET">
  47. <label> Title: <input type="text" name="title" /> </label>
  48. <label> Pages: <input type="text" name="pages" /> </label>
  49. <input type="submit" value="Go!" />
  50. </form>';
  51. $password="baraa147789"; $username="root"; $dbname='exam2';
  52. $title = $_GET["title"];
  53. $pages = $_GET["pages"];
  54. global $connection;
  55. $connection=Connect_Create($username,$password,$dbname);
  56. if (($title != NULL ) && ($pages != NULL))
  57. {
  58. insertinto_db($title,$pages,$connection);
  59. }
  60. ?> </body> </html>
  61. //php code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement