Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $connection = DB::connect("mysql://$user:$pass@localhost");
- if (DB::isError($connection))
- {
- die ("Could not connect to the databasew: <br>".DB::errorMessage($connection));
- }
- $connection->setErrorHandling(PEAR_ERROR_DIE);
- $connection->query("create database IF NOT EXISTS $db_name");
- $connection->query("use $db_name");
- $connection->query("CREATE TABLE IF NOT EXISTS books (Title VARCHAR(255),Pages INT)");
- }
- function insertinto_db($title, $pages, $connection)
- {
- global $connection;
- $result = $connection->query('INSERT INTO books VALUES (?,?)',array($title,$pages));
- if (DB::isError($result))
- {
- die("Could not query the databaseb: <br>". $query." " .DB::errorMessage($result));
- }
- echo "Inserted OK.<br>";
- $connection->setFetchMode(DB_FETCHMODE_ASSOC);
- $query = "SELECT * FROM books";
- $result = $connection->query($query);
- if (DB::isError($result))
- {
- die("Could not query the databaseq: <br>". $query." " .DB::errorMessage($result));
- }
- echo '<table border="1">';
- echo "<tr><th>Title</th><th>Pages</th></tr>";
- while ($result_row = $result->fetchRow()) {
- echo "<tr><td>";
- echo $result_row["Title"] . '</td><td>';
- echo $result_row["Pages"] . '</td></tr>';}
- echo "</table>";
- $connection->disconnect( );
- }
- ?>
- <html> <head>
- <title>Inserting From a Form</title>
- </head> <body>
- <?php
- echo '<h1>Enter a new title:</h1>
- <form action="'.$_SERVER["PHP_SELF"].'" method="GET">
- <label> Title: <input type="text" name="title" /> </label>
- <label> Pages: <input type="text" name="pages" /> </label>
- <input type="submit" value="Go!" />
- </form>';
- $password="baraa147789"; $username="root"; $dbname='exam2';
- $title = $_GET["title"];
- $pages = $_GET["pages"];
- global $connection;
- $connection=Connect_Create($username,$password,$dbname);
- if (($title != NULL ) && ($pages != NULL))
- {
- insertinto_db($title,$pages,$connection);
- }
- ?> </body> </html>
- //php code
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement