Click here to Skip to main content
15,849,996 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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
<?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";
        }
    ?>


HTML
<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>
Posted

1 solution

Try the following changes:

$queryFields = implode(",", $fields);

$queryValues = implode('","', $data);

$insert = 'INSERT INTO '.$table.'('.$queryFields.') VALUES ("'.$queryValues.'")';
 
Share this answer
 
Comments
MaddyDesigner 14-Dec-13 7:30am    
Thanks A Lot Dude, You Made My Day.. I Was Trying To Solve This From 3 Days And Today Was The 4th Day. So I Am Grateful To You. Thanks Once Again.
Peter Leow 14-Dec-13 7:35am    
You are welcome.
Nirmal89 4-Jun-14 0:14am    
i tried to convert this program in mysql but i have an this type of error below.

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\work\oops\insert\register.class.php on line 26

How can i convert this program in mysql?

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900