Write Data in Custom Table

Hello,

I created a database table with the name modx_mails.

Now I created a snippet with that code:

<?php
define('MODX_CONFIG_KEY','config');

$test_msid = "11111";
$test_name = "Duck";
$test_vorname = "Donald";
$test_email = "test@test.com";
$test_status = "not";



$sql = "INSERT INTO modx_mails (msid, name, vorname, email, status) VALUES (:msid, :name, :vorname, :email, :status)";
$query = $modx->query($sql);

$pdo->prepare($query)->execute([$test_msid, $test_name, $test_vorname, $test_email, $test_status]);

But I get an Error 500. I think there ist something wrong with the SQL-Stuff. Do have someone a hint?

Try using $modx instead of $pdo.

Ah, yes the Error 500 does not appear then. But it don’t write the data to the table.

I don’t think you need the query. Use the SQL statement directly with prepare:

$modx->prepare($sql)->execute(...)

You also need to use an associative array

$modx->prepare($sql)->execute(['msid' => $test_msid, 'name' => $test_name, ... ])

maybe better to create a proper xPDO package and use the modx/xPDO api methods

1 Like

Hm,

it’s still don’t write the data:

$sql = "INSERT INTO modx_mails (msid, name, vorname, email, status) VALUES (:msid, :name, :vorname, :email, :status)";
$query = $modx->query($sql);

$modx->prepare($query)->execute(['msid' => $test_msid, 'name' => $test_name, 'vorname' => $test_vorname, 'email' => $test_email, 'status' => $test_status]);

I don’t think you need the query . Use the SQL statement directly with prepare :slight_smile:
What do you mean with that?

maybe better to create a proper xPDO package and use the modx/xPDO api methods

Yes, that might be a better solution. But for a “not every day programmer” like me it is far to overload.
And the doc is not really “not-every-day-programmer” friendly :slight_smile:

$sql = "INSERT INTO modx_mails (msid, name, vorname, email, status) VALUES (:msid, :name, :vorname, :email, :status)";
$modx->prepare($sql)->execute(['msid' => $test_msid, 'name' => $test_name, 'vorname' => $test_vorname, 'email' => $test_email, 'status' => $test_status]);
1 Like

@ halftrainedharry:

Thank you. That is working.

The ClassExtender extra can help you generate classes that xPDO can use to read and write to your custom table. A new version of the extra is due out soon.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.