I have a MySQL database table named login which stores user information and login details. The table has been working well (inserting, updating and selecting) but from yesterday,the table is not inserting data from my php codeigniter scripts but its selecting and updating.
Also, when I insert a row manually (in phph Myadmin), its inserting well. The table has only 470 rows and its using InnoDB ingine and its 192KB. What could be the problem? The database is on a shared host. Here is the php code
function insertUser($pfNo, $fName, $mName, $lName, $email, $username, $encPassword, $userType,$preuniYear, $salt, $creationDate){
$data = array(
'pfNo' => $pfNo,
'fName' => $fName,
'mName' => $mName,
'lName' => $lName,
'email' => $email,
'username' => $username,
'password' => $encPassword,
'userType' => $userType,
'year' => $preuniYear
);
$query1 = $this->db->insert('login', $data);
$data = array(
'username' => $pfNo,
'creationDate' => $creationDate
);
$query3 = $this->db->insert('systemdetails', $data);
if ($query1 && $query3) {
return true;
} else {
return false;
}
}`
Related
I'm currently creating API for multiple inputs using laravel. The data will be stored into two tables : Order and Detail_Order. One order can have many detail orders.
But now, the data only stored into Order table, and got an error: ErrorException: Invalid argument supplied for foreach() in file. Does anyone know how? Thank you.
Here's my code :
public function createDetail($total_passenger, $id_trip, $id_users, Request $request){
$trip = Trip::where(['id_trip' => $id_trip])->get();
$seat = $request->id_seat;
if(Detail_Order::where(['id_trip' => $id_trip, 'id_seat' => $seat])
->where('status', '!=', 5)
->exists()) {
return $this->error("Seat has been booked");
}else{
$order = new Order();
$order_select = Order::select('id_order');
$order_count = $order_select->count();
if ($order_count === 0) {
$order->id_order = 'P1';
}else{
$lastrow=$order_select->orderBy('created_at','desc')->first();
$lastrow_id = explode('P', $lastrow->id_order);
$new_id = $lastrow_id[1]+1;
$order->id_order = 'P'.$new_id;
}
$order->id_trip = $id_trip;
$order->id_users = $id_users;
$order->date_order = date('Y-m-d H:i:s');
$order->id_users_operator = 'O4';
$order->save();
foreach($request->passenger_name as $key => $value){
Detail_Order::create([
'id_trip' => $order->id_trip,
'id_seat' => $request->id_seat[$key],
'id_order' => $order->id_order,
'passenger_name' => $request->passenger_name[$key],
'gender' => $request->gender[$key],
'departure' => $request->departure[$key],
'destination' => $request->destination[$key],
'phone' => $request->phone[$key],
'status' => 1
]);
}
return response()->json([
'status' => true,
'message' => "Successfully saved data",
'data' => $order
]);
}
I am trying to insert $product, $pric and $user to database table cart. Following is the function that I have created in SiteController.php.
public function actionCartadd($id)
{
$product = Additem::find()
->select('product')
->where(['id' => $id])
->one();
$pric = Additem::find()
->select('price')
->where(['id' => $id])
->one();
$user = Yii::$app->user->identity->username;
$connection = Yii::$app->getDb();
$result = Yii::$app->db->createCommand()
->insert('cart', [
'product' => '$product',
'price' => '$pric',
'user' => '$user',
])->execute();
if ($result)
return $this->render('custstore');
}
However, this end up in error. Can anyone suggest any fix
Try this following code
$result = Yii::$app->db->createCommand()->insert('cart', [
'product' => $product->product,
'price' => $pric->price,
'user' => $user
])->execute();
Look at this fragment:
'product' => '$product',
'price' => '$pric',
'user' => '$user',
Use double quotes in values or just variables without quotes
Also, better way to fetch product and price:
$productItem = Additem::findOne($id);
if ($productItem instanceof Additem) {
$product = $productItem->product;
$pric = $productItem->price;
}
I have a 2 DB connection, db and db2. I want to get all data from table claim in db2 connection. Below is my sql and it did not read from db2 connection. How to solved this?
$sql = "SELECT * FROM claim where provider_id = xx ";
$sql_count = "SELECT COUNT FROM ( $sql ) AS count ";
$totalCount = Yii::$app->db2->createCommand($sql_count)->queryScalar();
$dataProvider = new SqlDataProvider([
'sql' => $sql,
'totalCount' => $totalCount,
'sort' =>false,
'pagination' => [
'pageSize' => 10,
],
]);
return $dataProvider;
First you need to configure your databases like below:
return [
'components' => [
'db1' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db1name', //maybe other dbms such as psql,...
'username' => 'db1username',
'password' => 'db1password',
],
'db2' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=db2name', //maybe other dbms such as psql,...
'username' => 'db2username',
'password' => 'db2password',
],
],
];
Then you can simply :
//to get from db1
Yii::$app->db1->createCommand((new \yii\db\Query)->select('*')->from('tbl_name'))->queryAll()
//to get from db2
Yii::$app->db2->createCommand((new \yii\db\Query)->select('*')->from('tbl_name'))->queryAll()
If you are using an active record model, in your model you can define:
public function getDb() {
return Yii::$app->db1;
}
//Or db2
public function getDb() {
return Yii::$app->db2;
}
Then:
if you have set db1 in getDb() method, result will be fetched from db1 and so on.
ModelName::find()->select('*')->all();
I used phpass to hash my password on Insert as follows
public function addAdmin(){
$this->load->library('phpass');
$this->load->database();
$psw = 'admin1234';
$hashed = $this->phpass->hash($psw);
$now = date("Y-m-d H:i:s");
$data = array(
'userid' => 'admin_user' ,
'userfname' => 'Admin' ,
'userlname' => 'Admin',
'userdname' => 'Admin' ,
'useraddress' => '20/72,Vidarshana Mawatha, Galawilawatta, Homagama' ,
'usercountry' => 'Sri Lanka',
'usercontactno' => '0112-892199' ,
'userlastlog' => $now ,
'userpassword' => $hashed ,
'userpermission' => '1',
'useremail' => 'dilukshanmahendra#gmail.com'
);
$this->db->insert('ecom-user', $data);
echo "Successfully Added!";
}
But when I input the same userid & password (Correct UserID and Password) at the Login where it validate by matching them with the stored ones, it returns '0' for the following where I expcted '1'
public function validateLogin($userid,$userpass){
$this->load->library('phpass');
$this->load->database();
$hashed = $this->phpass->hash($userpass);
$this->db->select('*');
$this->db->from('ecom-user');
$this->db->where('userid', $userid);
$this->db->where('userpassword', $hashed);
$result = $this->db->get();
echo $this->db->last_query();
echo '<br/>'.$result->num_rows();
}
Please some one help me to solve this
Better have a look here
, meanwhile, trim all input fields you've got for login and registering new users!
I have a MySQL table that needs to be synchronized with a SQL Server table. So the data from MySQL moves to SQL Server. It is done by simple SELECT * and INSERT INTO queries. However, I ran into problems with migrating some BLOB data to a varbinary field.
So I have this code:
$db_mysql = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => '<user>',
'password' => '<password>',
'dbname' => '<db>',
'charset' => 'utf8'
));
$db_mssql = new Zend_Db_Adapter_Pdo_Mssql(array(
'pdoType' => 'sqlsrv',
'host' => '<host>',
'username' => '<user>',
'password' => '<password>',
'dbname' => '<db>'
));
$rows = $db_mysql->fetchAll("SELECT Id, Picture FROM Rosters");
foreach ($rows as $row) {
$update_sql = "UPDATE Rosters SET Picture = :picture WHERE Id = :id";
$stmt = new Zend_Db_Statement_Pdo($db_mssql, $update_sql);
$stmt->bindValue(":picture", bin2hex($row['Picture']), Zend_Db::PARAM_LOB);
$stmt->bindValue(":id", $row['Id'], Zend_Db::PARAM_INT);
try {
$stmt->execute();
} catch (Exception $e) {
echo $e->getMessage();
var_dump($e);
die();
}
}
This gives me the incredible not-so-useful error message: PDOException: SQLSTATE[HY000]: General error: 257 General SQL Server error: Check messages from the SQL Server [257] (severity 16) [(null)]
The MySQL Blob field is defined as BLOB with the attribute BINARY, the SQL Server field is defined as (varbinary(max), null). I think it's just a simple mistake, but I can't figure it out. Can anyone help me out with this?
Ok. I found it out on my own, this is the working code:
$db_mysql = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => '127.0.0.1',
'username' => '<user>',
'password' => '<pass>',
'dbname' => '<db>',
'charset' => 'utf8'
));
$db_mssql = new Zend_Db_Adapter_Pdo_Mssql(array(
'pdoType' => 'odbc',
'host' => '<host>',
'username' => '<user>',
'password' => '<pwd>',
'dbname' => 'Roosters'
));
$rows = $db_mysql->fetchAll("SELECT Id, Picture FROM Rosters");
foreach ($rows as $row) {
$update_sql = "UPDATE Rosters SET Picture = CONVERT(varbinary(max), :picture, 2) WHERE Id = :id";
$stmt = new Zend_Db_Statement_Pdo($db_mssql, $update_sql);
$stmt->bindValue(":picture", bin2hex($row['Picture']));
$stmt->bindValue(":id", $row['Id'], Zend_Db::PARAM_INT);
try {
$stmt->execute();
} catch (Exception $e) {
echo $e->getMessage();
var_dump($e);
die();
}
}
?>