Search code examples
phpmysqlsmartywhmcs

PHP/MySQL error: Could not execute INSERT INTO with PDO


I'm a beginner to PHP/MySQL trying to insert data into a table via a form, but I keep getting this:

Connected successfully ERROR: Could not execute INSERT INTO foo (firstname, lastname, landline, mobile) VALUES ('', '', ', ').

My limited understanding tells me I'm connecting successfully but nothing's getting into the table. Checking the table confirms this.

I'm trying to send the data from a PHP 7.1 WHMCS server to a remote host running MySQL 5.1.73. I'm pulling a user ID from WHMCS and pre-populating the that field with the idea to send that along with the rest of the form data. I had that field set to "hidden" and "text," no luck.

I even copied/pasted the form to a separate html and tried running everything at the root. No luck.

I used this example as my guide.

form.tpl:

<form method="post" action="includes/action.php">
User ID:<input type ="text" name = "userid" value={$d} readonly> //pulls userID from WHMCS
First name:<input type="text" name="firstname">
Last name:<input type="text" name="lastname">
Landline:<input type="text" name="landline">
Mobile:<input type="text" name="mobile">
<input type="submit" value="Submit"></form>

dbconnect.php:

$servername = "fqdn.com";
$username = "few";
$password = "2many";

try {
    $conn = new PDO("mysql:host=$servername;dbname=data_base", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

action.php:

//Open MySql Connection
include "dbconnect.php";

// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO foo (userid, firstname, lastname, landline, mobile) VALUES (:userid, :firstname, :lastname, :landline, :mobile)");
$stmt->bindParam(':userid', $userid);
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':landline', $landline);
$stmt->bindParam(':mobile', $mobile);

// insert a row
$userid = $_POST["userid"];
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$landline = $_POST["landline"];
$mobile = $_POST["mobile"];
$stmt->execute();


    echo "New records created successfully";
} catch(PDOException $e)
    {
        echo "Error: " . $e->getMessage();
    }

if (!$stmt) {
    echo "\nPDO::errorInfo():\n";
    print_r($dbh->errorInfo());
}

$conn = null;

Solution

  • Sorry for the delay. Here's the solution.

    action.php:

    public function insertToDb($data)
        {
            try{
               $sql = "INSERT INTO table_name (column1, column2) VALUES ('" . $data['column1']."','" .  $data['column2']."')";
                $this->con->exec($sql);
               if($this->con->lastInsertId() > 0){
                    return true;
                } else {
                    return "Error: " . $sql . "<br>" . $conn->error;
                }
            } catch (\PDOException $e) {
                return "Insert failed: " . $e->getMessage();
            }
        }
    
        public function getSingleData($d,$c)
        {
            try{
               $sql = "SELECT * FROM table_name  WHERE d='".$d."' AND c='".$c."'";
               $query = $this->con->prepare($sql);
               $query->execute();
               return $query->fetchAll(\PDO::FETCH_ASSOC);
            } catch (\PDOException $e) {
                return "Error: " . $e->getMessage();
            }
        }
    

    Edit: @halfer thanks for pointing out the vulnerability.

    public function insertToDb($data)
        {
            try{
                $insertdata = [
                    'column1' => $data['column1'],
                    'column2' => $data['column2'],
                    'column3' => $data['column3'],
                ];
               $sql = "INSERT INTO table_name (column1, column2,column3) VALUES (:column1,:column2,:column3)";
               $stmt= $this->con->prepare($sql);
               $stmt->execute($insertdata);
               if($this->con->lastInsertId() > 0){
                    return true;
                } else {
                    return "Error: " . $sql . "<br>" . $conn->error;
                }
            } catch (\PDOException $e) {
                return "Insert failed: " . $e->getMessage();
            }
        }