AJAX Event with INSERT INTO statement doesn't work

[LEFT][SIZE=13px]I want to run an AJAX event in a form so that when a field is clicked it copies the contents of two fields in the current form into another table. I used the Scriptcase macro to do this, but no data is ever sent to the other table. What am I doing wrong? Please see below. My goal is to send the content of two fields in the current form (event_id and username), into two corresponding fields (event_id_requested and username_requested) that are in a table called (requested_events). I’m sure this is child’s play to most of you, but it has me stumped…[/SIZE][/LEFT]

[LEFT][SIZE=13px]/**[/SIZE][/LEFT]
[LEFT][SIZE=13px] * Insert a record on another table[/SIZE][/LEFT]
[LEFT][SIZE=13px] */[/SIZE][/LEFT]

[LEFT][SIZE=13px]// SQL statement parameters[/SIZE][/LEFT]
[LEFT][SIZE=13px]$insert_table = ‘requested_events’; // Table name[/SIZE][/LEFT]
[LEFT][SIZE=13px]$insert_fields = array( // Field list, add as many as needed[/SIZE][/LEFT]
[LEFT][SIZE=13px] ‘event_id’ => “‘event_id_requested’”,[/SIZE][/LEFT]
[LEFT][SIZE=13px] ‘username’ => “‘username_requested’”,[/SIZE][/LEFT]
[LEFT][SIZE=13px] );[/SIZE][/LEFT]

[LEFT][SIZE=13px]// Insert record[/SIZE][/LEFT]
[LEFT][SIZE=13px]$insert_sql = ‘INSERT INTO ’ . $insert_table[/SIZE][/LEFT]
[LEFT][SIZE=13px] . ’ (’ . implode(’, ‘, array_keys($insert_fields)) . ‘)’[/SIZE][/LEFT]
[LEFT][SIZE=13px] . ’ VALUES (’ . implode(’, ', array_values($insert_fields)) . ‘)’;[/SIZE][/LEFT]

[LEFT][SIZE=13px]sc_exec_sql($insert_sql);[/SIZE][/LEFT]

Hallo LEW,

there seem to be several problems in your code.

Let’s make it as easy as possible (different syntax):

$insert_sql = "INSERT INTO requested_events SET event_id_requested = " . {event_id} . “, username_requested = '” . {username} . "’ ";
sc_exec_sql($insert_sql);

I’m assuming that event_id_requested is of type INT and username_requested is of any type STRING.

If you want to use your code (Scriptcase sample):
The main issue is in the array you’re using in your code:

  • No comma after the second field please.
  • Assign the new content (for example {event_id}) to the field in the database (for example event_id_requested) and not the other way.

Have a great week !

Gunter

your fields should be in {}

Swith debug mode to “On” under settings and the SQL should be displayed in a message box when the AJAX event fires. Then copy your SQL and run it against the DB (SQL Builder > Run) to see if it is valid.

That worked perfectly! You are awesome, and I appreciate it so much!!

Thanks to both GunterEibl and Steph!!