oscarotero / simple-crud

PHP library to provide magic CRUD in MySQL/Sqlite databases with zero configuration
MIT License
242 stars 58 forks source link

How to do Like or a full text search? #16

Closed elieobeid7 closed 7 years ago

elieobeid7 commented 7 years ago

How to do

category LIKE '%keyword%'

or full text search

WHERE MATCH (description) AGAINST('keyword1 keyword2')

And an addition question, I just want to know if it's possible, probably not I'd like to ask. suppose I have like 30 input fields. And each input field has the same name in mysql table

Like this i mean:

 <form>
  First name:<br>
  <input type="text" name="firstname"><br>
  Last name:<br>
  <input type="text" name="lastname">
</form>

and the database has

id firstname lastname

And so one, each html input field has the same exact name in a table. Is it possible to insert all of them automatically? Each input field to its correspondent Mysql field? not like this

    ->insert()
    ->data([
        'firstname' => $_Post["firstname"],
        'lastname' => $_Post["lastname"],
    ])
    ->run();

If such feature exists or if it could be implemented it would do a huge help.

oscarotero commented 7 years ago

You can use the where modifier. Example:

$users = $db->users->select()
    ->where('name LIKE :keyword', [':keyword' => '%tom%'])
    ->where('WHERE MATCH (description) AGAINST(:keywords)', [':keywords' => 'keyword1 keyword2'])
    ->run();

And about the form, I recomend use a library to manage forms. Anyway, you can do this:

<input type="text" name="data[firstname]"><br>
<input type="text" name="data[lastname]">
$db->users->insert()
    ->data($_POST['data'])
    ->run();
elieobeid7 commented 7 years ago

Thank you !

elieobeid7 commented 7 years ago
$db->users->insert()
    ->data($_POST['data'])
    ->run();

How to pass multiple parameters? in addition to POST above, I'd like to also insert 'photo' -> $photo

It has to be a variable and not coming from post because I have to verify and resize.

elieobeid7 commented 7 years ago

nevermind fixed it by adding it to the array