Accessing database after inputting login details in a form
Posted: Wed Feb 03, 2010 1:28 pm
Hi all!
I'm David, new to the forum!
I found many tips and solutions in the past here, it was time I opened an account!
Here is what made me finally join:
I'm writing this program, should be veeeery simple I think, it starts with a form where the user can submit his database login info and query their database.
I open it, input my database details (localhost, user, pass)... I submit... and I receive an http error that makes my browser crash... what am I doing wrong?
Thank you very much to anyone that will have the patience to read through this....
David
I'm David, new to the forum!
I found many tips and solutions in the past here, it was time I opened an account!
Here is what made me finally join:
I'm writing this program, should be veeeery simple I think, it starts with a form where the user can submit his database login info and query their database.
I open it, input my database details (localhost, user, pass)... I submit... and I receive an http error that makes my browser crash... what am I doing wrong?
Code: Select all
<!--if there is no content in the POST array... -->
<?php
if (!$_POST) {?>
<!-- ...display the form -->
<form action="<?php echo $_SERVER['$_PHP_SELF']; ?>" method="post">
Host address: <input type="text" name="host" />
Database name: <input type="text" name="dbname" />
User: <input type="text" name="user" />
Password: <input type="text" name="password" />
Database type:<select name="dbtype">
<option value="mysql" >Mysql</option>
<option value="postgresql">Postgresql</option>
</select>
<input type="submit" />
</form>
<!--dumping the db info to check if there is a mistake there... -->
<?php
} else
{
var_dump ($_POST);
$host = $_POST['host'];
$dbname = $_POST['dbname'];
$user = $_POST['user'];
$password = $_POST['password'];
try {
$dsn = "mysql:host=$host; dbname=$dbname";
$dbh = new PDO ($dsn, "$user", "$password");
}
catch (PDOException $e) {
die("ERROR: Cannot connect: " . $e->getMessage());
}
$sql = "SELECT * FROM thoughts";
$rslt = $dbh->query($sql) or die ();
or die($php_errormsg);
<!--this is just a test querying a database on my local machine... -->
$st = $db->query('SELECT * FROM thoughts');
$sql = 'SELECT thought, date FROM thoughts';
$results = $dbh->query($sql);
foreach ($results as $row) {
echo "{$row['thought']}, {$row['date']}";
}
}
?>
Thank you very much to anyone that will have the patience to read through this....
David