Parse error: syntax error, unexpected ''; ' (T_CONSTANT_ENCAPSED_STRING) in F:\xampp\htdocs\project\dbconfig.php on line 22 - insert

$sql = $pdodbcon->prepare("INSERT INTO complaint ('id', 'username', 'email', 'mobiletype', 'mobilemodel', 'mobilenumber', 'complaint')
VALUES ('$id', '$username', '$email', '$mobiletype', '$mobilemodel', '$mobilenumber', '$complaint')");
$pdodbcon->execute([$id, $username, $email, $mobiletype, $mobilemodel, $mobilenumber, $complaint]);

Related

Laravel - Trying to get property 'email' of non-object while trying to send notification

In my Laravel-5.8, I want to send email notification to a particular hod:
public function show($id)
{
if (! Gate::allows('leave_request_review')) {
return abort(401);
}
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$leaverequest = HrLeaveRequest::findOrFail($id);
$departmentId = $leaverequest->department_id;
$hodId = DB::table('hr_departments')->select('dept_head')->where('id', $departmentId)->first()->dept_head;
$hodEmail = DB::table('hr_employees')->select('email')->where('id', $hodId)->where('company_id', $userCompany)->first()->email;
die(var_dump($departmentId));
$hoddetails = [
'subject' => 'Leave for : '.$employeeFirstName,
'greeting' => 'Dear '.$hodFirstName . ' '. $hodFirstName . ',',
'body' => 'Please note that you have a Leave request awaiting your approval (' . $employeeFirstName . ' ' .$employeeLastName. ') ',
'line1' => 'Please note that you have a Leave request awaiting your approval (' . $employeeFirstName . ' ' .$employeeFirstName. ') ',
'thanks' => 'Thank you!',
'hod_email' => $hodEmail,
'hod_id' => $hodId,
'notification_type' => 'HOD Leave Approval',
];
Notification::route('mail', $hoddetails['hod_email'])
->notify(new \App\Notifications\Leave\LineManagerToHod($hodetails));
return View::make('hr.employee_leaves.show')
->with('leaverequest', $leaverequest);
}
Then I got this error:
Trying to get property 'email' of non-object
When I
dd($hodId);
in
$hodId = DB::table('hr_departments')->select('dept_head')->where('id', $departmentId)->first()->dept_head;
I found that dept_name is null and that makes $hodEmail to be null so this triggers the error.
How do I resolve this? Or even terminate the the action or show a message instead of the error?
You can simply check for $hodEmail exists operation like
$hodEmail = DB::table('hr_employees')->select('email')->where('id', $hodId)->where('company_id', $userCompany)->first();
if($hodEmail){
$hodEmail=$hodEmail->email;
}else{
$hodEmail="somedefault#email.com";
}
Also you can simple use cascading like
$hodEmail = DB::table('hr_employees')->select('email')->where('id', $hodId)->where('company_id', $userCompany)->first()->email??"somedefault#email.com";

Laravel SQLSTATE[HY000] error on validation

Laravel gived error when I added a unique:citizens validation to my code
I run
DB::select("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;")
in artisan tinker and it shows that table "citizens" exists
public function store(Request $request)
{
$data = request()->validate([
'name' => 'required | unique:citizens',
'phone' => 'alpha_dash | unique:citizens | nullable',
'organization_id' => '',
'description' => '',
]);
Citizen::create($data);
return redirect(route('citizen.index'));
}
SQLSTATE[HY000]: General error: 1 no such table: citizens (SQL: select >count(*) as aggregate from "citizens " where "phone" = 123-1234)
Remove spaces before and after |.
There can't be any characters in there, otherwise, they'll be considered as parts of table names or rules.

Array to string conversion error in migration

In my laravel 5.8 I set json field :
Schema::create('vote_categories', function (Blueprint $table) {
$table->increments('id');
$table->string('meta_description', 255)->nullable();
$table->json('meta_keywords')->nullable();
$table->timestamp('created_at')->useCurrent();
and some init data in seeder :
DB::table( 'vote_categories' )->insert([
'id' => 1,
'name' => 'Classic literature',
'slug' => 'classic-literature',
'active' => true,
'in_subscriptions' => true,
'meta_description' => '',
'meta_keywords' => ['Classic literature'],
]);
and in model :
class VoteCategory extends MyAppModel
{
protected $table = 'vote_categories';
protected $primaryKey = 'id';
public $timestamps = false;
protected $casts = [
'meta_keywords' => 'array'
];
But running migration I got error :
$ php artisan migrate
Migration table created successfully.
...
Migrating: 2018_07_13_051201_create_vote_categories_table
ErrorException : Array to string conversion
at /mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Support/Str.php:353
349|
350| $result = array_shift($segments);
351|
352| foreach ($segments as $segment) {
> 353| $result .= (array_shift($replace) ?? $search).$segment;
354| }
355|
356| return $result;
357| }
Exception trace:
1 Illuminate\Foundation\Bootstrap\HandleExceptions::handleError("Array to string conversion", "/mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Support/Str.php")
/mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Support/Str.php:353
2 Illuminate\Support\Str::replaceArray("?", [], "insert into `vt2_vote_categories` (`id`, `name`, `slug`, `active`, `in_subscriptions`, `meta_description`, `meta_keywords`) values (?, ?, ?, ?, ?, ?, ?)")
/mnt/_work_sdb8/wwwroot/lar/votes/vendor/laravel/framework/src/Illuminate/Database/QueryException.php:56
Please use the argument -v to see more details.
Why error ? I supposed that $casts array must be used in ->insert methods, but it looks not like so.
How to fix it ?
Thanks!
You are trying to insert an array in the JSON column datatype, hence the error, try changing it in json before inserting:
DB::table( 'vote_categories' )->insert([
'id' => 1,
'name' => 'Classic literature',
'slug' => 'classic-literature',
'active' => true,
'in_subscriptions' => true,
'meta_description' => '',
'meta_keywords' => json_encode(['Classic literature']),
]);

Php code working on local server but giving syntax error on live server

Code:
function getUserId($userName) {
$myModel = new MY_Model();
$requiredFields = "id";
$whereClause = [ // line number 35
"is_active" => 'ACTIVE',
"user_name" => $userName
];
$userId = $myModel->select("users", $requiredFields, $whereClause);
return $userId;
}
Error:
Parse error: syntax error, unexpected '[' in /home/com/public_html/_apis/application/helpers/custom_helper.php on line 35
A PHP Error was encountered
Severity: Parsing Error
Message: syntax error, unexpected '['
Filename: helpers/custom_helper.php
Line Number: 35
Backtrace:
Replace Where Clause with this (Hope this help) :
$whereClause = array( // line number 35
"is_active" => 'ACTIVE',
"user_name" => $userName
);
Try this
$whereClause = array();
$whereClause['is_active'] = 'ACTIVE';
$whereClause['user_name'] = $userName;

Zend Framework 2: 'ORA-00911: invalid character' when passing string parameter

I'm trying to execute a query with Zend Framework 2 Db Adapter in this way:
$db = new Adapter(array(
'driver' => 'oci8',
'username' => 'myusername',
'password' => 'mypassword',
'connection_string' => 'myconnectionstring',
'charset' => 'utf8',
));
$sql = 'SELECT lastname FROM accounts WHERE username = ?';
$statement = $db->createStatement($sql, array('luca'));
$result = $statement->execute();
But PHP gives me this message:
[23-May-2013 10:23:30 Europe/Rome] PHP Fatal error: Uncaught
exception 'Zend\Db\Adapter\Exception\RuntimeException'
with message 'ORA-00911: invalid character' in
/var/www/zend/server/library/ZF2-.2.0/library/Zend/Db/Adapter/Driver/Oci8/Statement.php:261
Stack trace:
#0 /var/www/html/secure/zendAuth2.php(92):
Zend\Db\Adapter\Driver\Oci8\Statement->execute()
#1 {main}
thrown in /var/www/zend/server/library/ZF2-2.2.0/library/Zend/Db/Adapter/Driver/Oci8/Statement.php on line 261
The invalid character in the SQL seems to be the '?'. What am I doing wrong? Reading on Framework manual it seems to be correct (http://framework.zend.com/manual/2.2/en/modules/zend.db.adapter.html#query-preparation-through-zend-db-adapter-adapter-query) but I still get this error.
If instead I use
$sql = 'SELECT lastname FROM accounts WHERE username = :username';
$statement = $db->createStatement($sql, array(':username' => 'luca'));
$result = $statement->execute();
it works fine.
Thanks for the help.

Resources