Updating SQL columns from forms


#1

Hi all,

Excuse my lack of php knowledge.

I’m simply trying to update an sql column with data that is entered in a form.

I have looked over
https://getkirby.com/docs/cookbook/database
and
https://getkirby.com/docs/developer-guide/toolkit/database

The part I’m stuck with is getting the data from the form and updating the database

I can run the following code directly on a page which updates 000718669X with status, 1.

$update = $books->where(‘book’, ‘=’, ‘000718669X’)->update(array( ‘status’ => ‘1’, ‘date’ => ‘NOW()’));

But ideally I need it to be run on form submit, and CODE = submission from the form

$update = $books->where(‘book’, ‘=’, ‘CODE’)->update(array( ‘status’ => ‘1’, ‘date’ => ‘NOW()’));

<tr>
 <td valign="top">  <label for="code">Code</label> </td>
 <td valign="top">  <input type="text" name="code" maxlength="50" size="30"> </td>
</tr>

<tr>
 <td valign="top">  <label for="name">Name</label> </td>
 <td valign="top">  <input type="text" name="name" maxlength="50" size="30"></td>
</tr>

<tr>
 <td valign="top">  <label for="notes">Notes</label> </td>
 <td valign="top">  <textarea  name="notes" maxlength="1000" cols="25" rows="6"></textarea> </td>
</tr>
<tr>
 <td colspan="2" style="text-align:center">
  <input type="submit" value="Submit"> 
 </td>
</tr>

I guess what I’m asking is what would go into the action php file?

I’m using this on the page itself as the c::set in config/config.php was not working with db::select

$db = new Database(array(
‘type’ => ‘mysql’,
‘host’ => ‘127.0.0.1’,
‘database’ => ‘kirbycms’,
‘user’ => ‘xxxx’,
‘password’ => ‘xxxx’
));


#2

I recommend you use the Kirby Uniform plugin to handle your form submission. It takes off some of the heavy lifting for you. All you need to get started is in the documentation.

I don’t know if you just missed posting it, but there is a form tag missing around your inputs. And as you will find in the docs to set up the form, don’t forget the CSRF input. for secure form submission.

If you don’t want to use a plugin, here is an example of a basic contact form that you can adapt to your purposes: https://gist.github.com/bastianallgeier/c396df7923848912393d However, you should add a CSRF token check to that example.

On a side note: I wouldn’t recommend using table markup to create a form.