Get error number with oracle database in Codeigniter - php

First of all sorry for my bad English..
I have this function in a model:
function insertUser($data){
$sql = "INSERT INTO USERS VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$query = $this->db->query($sql, array(
$data["uname"],
$data["nome"],
$data["dnuser"],
$data["muser"],
$data["fruser"],
$data["cpuser"],
$data["euser"],
md5($data["passu"])
));
//return $this->db->_error_number();
}
I have one primary key and when data is correct the function works but when is inserted a duplicate key the function doesn’t work. But how can i know that? i mean how can i catch the error ORA-00001 ??
BTW the commented line doesn’t work..
Thank you very much!!

You can use try catch block...
function insertUser($data){
$sql = "INSERT INTO USERS VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
try{
$this->db->trans_start();
$query = $this->db->query($sql, array(
$data["uname"],
$data["nome"],
$data["dnuser"],
$data["muser"],
$data["fruser"],
$data["cpuser"],
$data["euser"],
md5($data["passu"])
));
$this->db->trans_commit();
}
catch(Exception $ex){
$this->db->trans_rollback();
echo "Error:".$ex;
}
}
You can check the following article..
http://ellislab.com/codeigniter/user-guide/database/transactions.html

Related

Insert query is not adding values to database

The value is not adding when executing this query. Can anyone guide me where I am doing mistake.
function addpost($val){
return execute("INSERT INTO `tbl_postcodelist` (`postcode`, `add1`, `add2`, `delivery`,`round`, `status`) VALUES (?, ?, ?, ?, ?,?)", array($val['postcode'],$val['add1'],$val['add2'],$val['price'],$val['round'],$val['status']));
}
function execute($query,$values=array(),$id=false){
$db=new db();
return $db->execute($query,$values,$id);
}

Data Not inserted in Database php, mysql

Can't insert data into DB . When i remove user_id then data is inserted. Please check below my code and help me.
function adddata($data) {
global $db;
if (is_array($data)) {
$stmt = $db->prepare('INSERT INTO `pay` (id, payment, status, itemid, time, user_id) VALUES(?, ?, ?, ?, ?, ?');
$userid = 2;
$stmt->bind_param(
'sdssss',
$data['txn_id'],
$data['payment_amount'],
$data['payment_status'],
$data['item_number'],
date('Y-m-d H:i:s'),
$userid
);
$stmt->execute();
$stmt->close();
return $db->insert_id;
}
return false;
}
It's subtle, but your SQL string is missing a closing bracket:
$stmt = $db->prepare('INSERT INTO `pay` (...) VALUES (?, ?, ?, ?, ?, ?)');
Where the VALUES list was not properly closed.
A lot of problems can be detected and resolved by enabling exceptions in mysqli so mistakes aren't easily ignored. This should show up as a SQL error in your logs.

PDOStatement::execute(): SQLSTATE[HY093] , strange behavour during bulk insert

Can someone help? I am trying to do a bulk insert (400 rows), everytime it shows an error message after inserted 58 rows in the database, I tried different dataset, it behave the same way.
Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
Here is the Code:
$rows = $_POST['myTableArray'];
$nrecuser= $_SERVER['REMOTE_ADDR'];
$ndatetime= date('Y-m-d G:i:s');
$sql = "INSERT INTO dbo.rec(ncode, nname, nprimary, ndate, nyear, nperiod,
nref, ntype, nuser, ncategorycode1, ncategorycode2,
ncategorycode3, namount, ndescription, naccount,
nmajorheadcode, ncheck, nrecuser, ndatetime)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
include_once dirname(__FILE__) . '/includes/db.inc.php';
try
{
$conn->beginTransaction();
$stmt = $conn->prepare($sql);
foreach($rows as $row)
{
array_push($row, $nrecuser, $ndatetime);
$stmt->execute($row);
}
$conn->commit();
exit();
}
catch (PDOException $e)
{
$conn->rollback();
echo $e;
exit();
}
The number of given insert query parameters is not equal values you are passing to insert
It turn out to be a server side issue, all I have to update the php.ini max_input_vars setting. Thanks for the help.

Can't Insert data in MySql Table, but i can retrieve data just fine

I can conect to my database just fine, but whenever I try to insert data I get a "Call to a member function bind_param() on a non-object" error, which I know means that I must have mistyped the name of a slot, or something, but I just can't see it, I'm connecting locally, do I have to set up something in MyPhP to allow data to be added from a php file? Thanks in advance.
<?php
include 'connect.php';
if (isset($_POST['Slot1'])) {
$Slot1 = $_POST['Slot1'];
}
if (isset($_POST['Slot2'])) {
$Slot2 = $_POST['Slot2'];
}
if (isset($_POST['Slot3'])) {
$Slot3 = $_POST['Slot3'];
}
if (isset($_POST['Slot4'])) {
$Slot4 = $_POST['Slot4'];
}
if (isset($_POST['Slot5'])) {
$Slot5 = $_POST['Slot5'];
}
if (isset($_POST['Slot6'])) {
$Slot6 = $_POST['Slot6'];
}
if (isset($_POST['Slot7'])) {
$Slot7 = $_POST['Slot7'];
}
$stmt = $db->prepare("INSERT INTO `tabel` (Slot1, Slot 2, Slot3, Slot4,Slot5,
Slot6, Slot7)
VALUES (?, ?, ?, ?, ?, ?,?");
$stmt->bind_param('sssssss',$Slot1,$Slot2,$Slot3,$Slot4,$Slot5,$Slot6,$Slot7);
$stmt->execute();
$stmt->close();
header("Location: Display.php")
?>
You have missed one end parenthese
Replace
$stmt = $db->prepare("INSERT INTO `tabel` (Slot1, Slot 2, Slot3, Slot4,Slot5,
Slot6, Slot7)
VALUES (?, ?, ?, ?, ?, ?,?");
To
$stmt = $db->prepare("INSERT INTO `tabel` (Slot1, Slot 2, Slot3, Slot4,Slot5,
Slot6, Slot7)
VALUES (?, ?, ?, ?, ?, ?,?)");
Try to change your query to
$stmt = $db->prepare("INSERT INTO `tabel` (Slot1, Slot 2, Slot3, Slot4,Slot5,Slot6, Slot7)VALUES (?, ?, ?, ?, ?, ?,?)");

Get last ID after SQL insert

This code below inserts some data into a MySQL database. I want the page to load to this record after the insert but the variable ($insert) that I'm trying to load is not coming through on the header. can someone help?
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO riders (firstname,secondname,email,mobile,landline,dob,addressline1,town,county,postcode) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
$q = $pdo->prepare($sql);
$q->execute(array($firstname,$secondname,$email,$mobile,$landline,$dob,$streetaddress,$town,$county,$postcode));
$insert = mysql_insert_id();
Database::disconnect();
header("Location:read.php?id=.$insert");
Use $pdo->lastInsertId() for getting last ID after SQL insert.

Categories