PHP mysql insert query not working but not giving any error messages

He all,
I am new in php. i try to make a admin panel by php. I make a php code for insert query is not working and it’s not show any error massage. Please help me.
Thanks.
Ibrahim

We can’t see your screen. Post your code. Nobody is going to be able to help you with what you provided.

please can you share the source code

public function insert($table, $data){

	if (!empty($data) && is_array($data)) {
		$keys = '';
		$values = '';
		$i = 0;
		$keys = implode(',', array_keys($data));
		$values = ":".implode(', :', array_keys($data));
		$sql = "INSERT INTO ".$table." (".$keys.") VALUSE (".$values.")";
		$query = $this->pdo->prepare($sql);
		foreach ($data as $key => $val) {
			$query->bindValue(":$key", $val);
		}

		$insertdata = $query->execute();
		if ($insertdata) {
			$lastId = $this->pdo->lastInsertId();
			return $lastId;
		} else {
			return false;
		}
		
	}
}

Do you see a problem here?

1 Like

Yes i fix it not still same problems here.

Your code needs error handling for the prepare/execute statements. The easiest way of adding error handling, so that you are not adding conditional logic at each statement that can fail, is to use exceptions for errors and in most cases let php catch and handle the exception where it will use its error related settings to control what happens with the actual error information (database errors will get displayed or logged the same as php errors.) You would set the error mode to exceptions when you make the database connection. You also need to echo out the dynamically built $sql statement for debugging purposes so that you can see what it is.

If you are dynamically building the sql query statement from submitted form information, the array keys, being put directly into the sql query statement, are not secure. You should instead be building the sql query statement by getting the keys/columns from an internal data definition, and only getting the data values from the submitted form data.

Next, assuming you are doing this in a secure way, the table name and column names can be anything and should be enclosed in back-ticks so they can’t accidentally break the sql query syntax.

$pdo = new PDO('mysql:host=localhost;dbname=someDatabase', 'username', 'password', [
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

I use this code.

<?php /** * Database class */ class Database{ private $hostdb ="localhost"; private $userdb ="root"; private $passdb =""; private $namedb ="db_coustomar"; private $pdo; public function __construct() { if (!isset($this->pdo)) { try { $link = new PDO("mysql:host=".$this->hostdb.";dbname=".$this->namedb,$this->userdb,$this->passdb); $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $link->exec("SET CHARACTER SET utf8"); $this->pdo = $link; } catch (PDOException $e) { die("Faild to connect with Database".$e->getMessage()); } } } //Read Data public function select($table, $data = array()){ $sql = 'SELECT '; $sql .= array_key_exists('select', $data)?$data['select']:'*'; $sql .=' FROM '.$table; if (array_key_exists("where", $data)) { $sql .=' WHERE '; $i = 0; foreach ($data['where'] as $key => $value) { $add = ($i > 0)?' AND ':''; $sql .= "$add"."$key=:$key"; $i++; } } if (array_key_exists('order_by', $data)) { $sql .= ' ORDER BY '.$data['order_by']; } if (array_key_exists('start', $data) && array_key_exists("limit", $data) ) { $sql .=' LIMIT '.$data['start'].','.$data['limit']; }elseif (!array_key_exists('start', $data) && array_key_exists("limit", $data) ){ $sql .=' LIMIT '.$data['limit']; } $query = $this->pdo->prepare($sql); if (array_key_exists("where", $data)) { foreach ($data['where'] as $key => $value) { $query->bindValue(':$key',"{$value}%"); } } $query->execute(); if (array_key_exists("return_type", $data)) { switch ($data['return_type']) { case 'count': $value = $query->rowCount(); break; case 'single': $value = $query->fetch(PDO::FETCH_ASSOC); break; default: $value = ''; break; } }else{ if ($query->rowCount() > 0) { $value = $query->fetchAll(); } } return !empty($value)?$value:false; } //Insert Data public function insert($table, $data){ if (!empty($data) && is_array($data)) { $keys = ''; $values = ''; $i = 0; $keys = implode(',', array_keys($data)); $values = ":".implode(', :', array_keys($data)); $sql = "INSERT INTO ".$table." (".$keys.") VALUES (".$values.")"; $query = $this->pdo->prepare($sql); foreach ($data as $key => $val) { $query->bindValue(":$key", $val); } $insertdata = $query->execute(); if ($insertdata) { $lastId = $this->pdo->lastInsertId(); return $lastId; } else { return false; } } } //Update Data public function update(){ } //Delete Data public function delete(){ } } ?>

If you edit your post above, you can add bbcode [ code ] [ /code ] (without the spaces) tags so that the code is readable.

Are you even making an instance of the class and calling the insert() method? Do you have php’s error related settings set up so that php would either display or log all errors?

Sponsor our Newsletter | Privacy Policy | Terms of Service