Am trying to insert data from a php form using OOPs in PHP.
Am getting the error that ->
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"username','email','password") VALUES ("Bob','bob@example.com','$1$RL4.GJ4.$FX4D' at line 1
Am failing in this as i am new to oops with php.
Here's the code for that handles the class of database in php register.class.php ->
<?php
class DataBase {
public $connection;
private $hostName;
private $userName;
private $pasWord;
private $db;
public function connect($host, $user, $pass, $dtb) {
$this->hostName = $host;
$this->userName = $user;
$this->password = $pass;
$this->db = $dtb;
return $this->connection = mysqli_connect($host, $user, $pass, $dtb)
or die('Could Not Connect.');
}
public function insert($fields, $data, $table) {
try {
$queryFields = implode("','", $fields);
$queryValues = implode("','", $data);
$insert = 'INSERT INTO '.$table.'("'.$queryFields.'") VALUES ("'.$queryValues.'")';
if (mysqli_query($this->connection, $insert)) {
return true;
} else {
die(mysqli_error($this->connection));
}
} catch (Exception $ex) {
echo "Some Exception Occured " . $ex;
}
}
}
?>
Here's the code of the form register.php ->
<?php
include 'register.class.php';
$con = new DataBase();
$con->connect('localhost', 'root', '', 'test');
if (isset($_POST['submit'])) {
$uName = $_POST['userName'];
$email = $_POST['email'];
$pass = $_POST['passWord'];
$passHash = crypt($pass);
$fields = array('username', 'email', 'password');
$values = array($uName, $email, $passHash);
$res = $con->insert($fields, $values, 'users');
if ($res) echo "1 Record Inserted";
}
?>
<form action="register.php" method="POST" enctype="multipart/form-data">
Username: <input type="text" name="userName" required>
<br />
Email: <input type="email" name="email" required>
<br />
Password: <input type="passWord" name="passWord" required>
<br />
<input type="submit" name="submit" value="Register">
</form>