Hello. Are trying to enter information into an html form then pass that to an .php to handle the form, The .php script use include ‘somefile.php’ to establish an data base connection. But it is not working.
Output in web browser:
abc
Fatal error: Uncaught Error: Call to undefined method connect2db::query() in /opt/lampp/htdocs/process2sql.php:8 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/process2sql.php on line 8
The html form is a place holder atm it wil later take useful information. but it is used to trigger the handle script atm.
Any suggestion to avoid error are welcome
Code snippets bellow are in order form.html, handle.php and connect2db.php
File name: phpTest.html
<!DOCKTYPE html>
<html>
<head>
<title>Fun web project</title>
</head>
<body>
<form action="process2sql.php" method="POST">
<label for="text"><a href="https://duckduckgo.com/" target="blank">Type</a> text here: </label>
<input type="text" name="text" id="text" placeholder="Bitty bot">
<input type="submit" name="Submit">
</form>
</body>
</html>
File name: process2sql.php
<?php
include 'dbconnect.php';
echo $_POST['text'];
$pdo = new connect2db();
#$pdo = o->connect();
#var_dump($pdo);
$stmt = $pdo->query('SELECT * FROM registration where fname = \'sofia\'');
#while($row = $stmt->fetch()) {
# echo $row['fname'] . ' ' . $row['lname'] . '<br>';
#}
?>
File name: dbconnect.php
#/opt/lampp/bin/mysql -u root
class connect2db {
private $host;
private $db;
private $user;
private $pass;
private $charset;
private $options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
public function connect() {
$this->host = '127.0.0.1';
$this->db = 'mywebdb';
$this->user = 'webroot';
$this->pass = '1234';
$this->charset = 'utf8mb4';
try {
#public $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
$dsn = 'mysql:host=127.0.0.1;dbname=mywebdb;charset=utf8mb4';
$pdo = new PDO($dsn, $user, $pass, $options);
}
catch (\PDOException $e) {
echo "Error is imenent";
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
return $pdo;
}
}
#$runopt = new connect2db();
?>