Unable to access an array index in a PHP Query [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
Please I need help identifying the problem with my query here:
So this is my hard-coded data because I am trying to test my PHP script on its own with out the android application that usually sends data to it.
I have this array of hardcoded data.
$register_data = array(
'username' => 'david',
'password' => 'david',
'first_name' => 'david',
'last_name' => 'david',
'email' => 'david#yahoo.com'
);
This is my problematic query:
"SELECT * FROM `user_info` WHERE `email` = '$register_data['email']' OR `username` = '$register_data['username']'";
And this is the error i received:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /home1/ifedavid/public_html/androidconnect/register.php
Please is there something I'm doing wrong? Thanks for the help in advance.

$register_data = array(
'username' => 'david',
'password' => 'david',
'first_name' => 'david',
'last_name' => 'david',
'email' => 'david#yahoo.com'
);
$query = "SELECT * FROM 'user_info' WHERE 'email' = '".$register_data['email']."' OR 'username' = '".$register_data['username']."'";
Then execute the query with the var $query
Hope it helps

Related

syntax error, unexpected identifier enum laravel

I am using LARAVEL 9. I am create enum in Enum folder and access in model. But when i am adding data i am getting this error
syntax error, unexpected identifier "GenderEnum"
Here is my code
GenderEnum.php
<?php
namespace App\Enum;
enum GenderEnum:string
{
case MALE = 'male';
case FEMALE = 'Female';
}
AdminSeeder.php
$data = [
'first_name' => 'Rishab',
'last_name' => 'goyal',
'email' => 'RISHABGOYAL#yopmail.com',
'mobile_number' => '123',
'role' => '1',
'gender' => 'male',
'password' => '123',
'profile_photo' => '',
];
Admin::addEdit($data);
Admin.php (Model)
protected $casts = [
'gender' => GenderEnum::class
];
There's nothing wrong with your code even in the namespace. The problem is your environment setup, maybe you are still running PHP 8.0 or below instead of PHP 8.1
Enums is a new syntax introduced in PHP 8.1, and not
supported in older PHP versions.
Parse error: syntax error, unexpected identifier
Please check 'use App\Enum' namespace is imported properly
I had the same problem before.
My solution is to update your PHP version to 8.1.

Showing Notice: Undefined property: wpdb::$insertid in D:\wamp\www\wordpress-4.7.1\wordpress\wp-includes\wp-db.php on line 684 [duplicate]

This question already has answers here:
How to get last inserted row ID from WordPress database?
(7 answers)
Closed 4 years ago.
I want to get last inserted id to update table based on this id. For that I used $wpdb->insert_id for getting the last inserted id. But get this issue
Notice: Undefined property: wpdb::$insertid in D:\wamp\www\wordpress-4.7.1\wordpress\wp-includes\wp-db.php on line 684
this is my code. Someone please help
$parent_id=$template_load_data['id'];
$wpdb->insert( 'wp_rxl_templates', array(
'template_name' => $_POST['template_name_custom'],
'template_content' => $post_content,
'created_date' => current_time( 'mysql' ),
'status' => 'active',
'default_template'=>'false',
'parent_template_id'=>''.$parent_id.'',
));
$result_id = $wpdb->insertid;
$result_data = "select wp_rxl where status ='active' NOT (id = '$result_id')";
You have wrote it as wrong. Check below:
You have to use $wpdb->insert_id instead $wpdb->insertid.
$parent_id=$template_load_data['id'];
$wpdb->insert( 'wp_rxl_templates', array(
'template_name' => $_POST['template_name_custom'],
'template_content' => $post_content,
'created_date' => current_time( 'mysql' ),
'status' => 'active',
'default_template'=>'false',
'parent_template_id'=>''.$parent_id.'',
));
$result_id = $wpdb->insert_id;
$result_data = "select wp_rxl where status ='active' NOT (id = '$result_id')";

Laravel faker generate gender from name

What is the optimum way of generating gender using faker, having generated a name so that the gender matches the name
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'username' => $faker->userName,
'phone' => $faker->phoneNumber,
'gender' => $faker->randomElement(['male', 'female']),//the gender does not match the name as it is.
'address' => $faker->address,
'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
'password' => bcrypt('secret')
];
Looking at the documentation and an issue raised on the their Github issues section, your solution seems to be the best. Some methods allow you to specify the gender for a name so you could do like this:
$gender = $faker->randomElement(['male', 'female']);
return [
'name' => $faker->name($gender),
'email' => $faker->safeEmail,
'username' => $faker->userName,
'phone' => $faker->phoneNumber,
'gender' => $gender,
'address' => $faker->address,
'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
'password' => bcrypt('secret')
];
Hopefully this fits your requirement.
to do it without additional variable, do it like this
return [
'gender' => $faker->randomElements(['male', 'female']),
'name' => $faker->name(function (array $user) {return $user['gender'];})
]
hope it helps
The method randomElements is gonna return an array with one single element, so if you want to get 'female' or 'male', don't forget to add at the end of the first line this: [0]. You need the first element (index 0) of the resulting array (that only has one element).
$gender = $faker->randomElements(['male', 'female'])[0];
One more thing. In order to obtain exactly what you want, you need to use firstName instead of name. This way the first name will be according to the gender. Do it this way:
return [
'name' => $faker->firstName($gender),
'email' => $faker->safeEmail,
'username' => $faker->userName,
'phone' => $faker->phoneNumber,
'gender' => $gender,
'address' => $faker->address,
'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
'password' => bcrypt('secret')
];
One last thing: If you use 'Male' and 'Female', instead of 'male' and 'female', this is NOT gonna work!!
Actually all answers did not really work for me. It returned female or male only.
I had almost the same issue. I needed a random element out of three genders 👍😀. It always gave me this error message:
Illuminate\Database\QueryException : Array to string conversion (SQL: insert
into `users` (`name`, `gender`, `email`, `admin`, `author`, `password`,
`remember_token`) values (Margaret Robel I, male, azieme#example.com, 1, 0,
dummylogin, gwKdVN7zYv))
at /Users/mangrove/Desktop/php-workspace/laravel-
mangrove/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
660| // If an exception occurs when attempting to run a query, we'll
format the error
661| // message to include the bindings with SQL, which will make this
exception a
662| // lot more helpful to the developer instead of just the
database's errors.
663| catch (Exception $e) {
> 664| throw new QueryException(
665| $query, $this->prepareBindings($bindings), $e
666| );
667| }
668|
Exception trace:
1 ErrorException::("Array to string conversion")
/Users/mangrove/Desktop/php-workspace/laravel-
mangrove/vendor/laravel/framework/src/Illuminate/Database/
MySqlConnection.php:80
2 PDOStatement::bindValue()
/Users/mangrove/Desktop/php-workspace/laravel-
mangrove/vendor/laravel/framework/src/Illuminate/
Database/MySqlConnection.php:80
After a look at the documentation for Faker v.1.8.0
This worked for me:
public function run()
{
$faker = Faker::create();
foreach(range(1,10) as $index){
// Returns always random genders according to the name, inclusive mixed !!
$gender = $faker->randomElement($array = array('male','female','mixed'));
DB::table('users')->insert([
'name' => $faker->name($gender),
'gender' => $gender,
'email' => $faker->unique()->safeEmail,
'admin' => $faker->numberBetween($min = 0, $max = 1),
'author'=> $faker->numberBetween($min = 0, $max = 1),
'password' => 'dummylogin',
'remember_token' => str_random(10),
]);
}
}
It turns out mixed genders will always have different names, because you can
be either way 😄⚧

Unexpected '=>' (T_DOUBLE_ARROW) [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 6 years ago.
I am getting the following error and I just cant find the solution to the problem. Might be anyone be able to help me?
DB::table('videos')->insert(
['video_id' => $videos[$i]->title],
['url'] => $videos[$i]->url],
['default_thumb'] => $videos[$i]->default_thumb],
['thumb'] => $videos[$i]->thumb],
['publish_date'] => $videos[$i]->publish_date],
['tags'] => $videos[$i]->tags]
);
The error message is:
FatalErrorException in VideoController.php line 33:
syntax error, unexpected '=>' (T_DOUBLE_ARROW)
This is correct syntax for insert() method:
DB::table('videos')->insert([
'video_id' => $videos[$i]->title,
'url' => $videos[$i]->url,
'default_thumb' => $videos[$i]->default_thumb,
'thumb' => $videos[$i]->thumb,
'publish_date' => $videos[$i]->publish_date,
'tags' => $videos[$i]->tags
]);
Hope it will solve your problem
DB::table('videos')->insert(
['video_id' => $videos[$i]->title,
'url' => $videos[$i]->url,
'default_thumb' => $videos[$i]->default_thumb,
'thumb' => $videos[$i]->thumb,
'publish_date' => $videos[$i]->publish_date,
'tags' => $videos[$i]->tags]
);

Converting undefined indexes to null in PHP

I'm not sure if the title of this question is necessarily the accurate description of what I need to do, but I'll go ahead and ask my question and see what everyone thinks...
Basically, I am receiving data from a source that I have no control over, and I need to transpose it into a suitable format for inserting into my database using CakePHP. So, here's how I'm doing it:
public function submitApp($data) {
$array = array(
'Student' => array(
'name' => $data['name'],
'email' => $data['email'],
'phone' => $data['phone'],
'address' => $data['address'],
'dob' => $data['dob'],
'gender' => $data['gender']
),
'Application' => array(
'course_id' => $data['course_id'],
'question1' => $data['question1'],
'question2' => $data['question2'],
'question3' => $data['question3'],
'question4' => $data['question4'],
),
'ApplicationQualification' => $data['Qualifications']
);
// Logic to save $array goes here
}
The problem is that sometimes not all of the keys in $data will be submitted to my app but I still want my app to work with what it gets.
I know that I can wrap each key in a conditional like this:
if (!isset($data['name'])) { $data['name'] = null; }
...and then building the array, but this seems like a pretty clumsy way of doing it. Is there a more efficient way to do this?
You could use a simple ternary statement
'name' => array_key_exists('name', $data) ? $data['name'] : null
Alternatively, you can set up a default array and then merge the given values in
$defaults = [
'name' => null,
'email' => null,
// etc
];
$data = array_merge($defaults, $data);

Categories