写入数据库时​​出错-Moodle

Error writing to database-Moodle

我在 Moodle 中有一个页面,用户可以通过 JQuery 添加连续的行,每次单击“保存”按钮时,表单中输入的数据都会写入数据库,但我正在这些错误消息:

Debug info: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near order) VALUES('1','Introduction Audio 1','1')' at line 1

INSERT INTO mobile (week,title,order) VALUES(?,?,?)
[array (
0 => 1,
1 => 'Introduction Audio 1',
2 => '1',
)]

追加连续行的表格:

 <form method="post" action="processMobileApp.php">
<br/>
<span class="add-week" style="float:right;"><input type='button' value="Add a New Week" style="font-weight:bold; font-size:17px;"/>&nbsp;</span>
<span class="add-row" style="float:right;"><input type='button' value="Add New Row" style="font-weight:bold; font-size:17px;"/>&nbsp;</span>
<span style="float:right;"><input type='submit' value="Save" style="font-weight:bold; font-size:17px;"/></span>
<br/><br/>
<table class="row-list" cellspacing="1" cellpadding="0">
<br/>
    <tr>
        <th>Week 1 Title</th><th>Order</th><th>Edit</th><th>Delete</th><th>Upload</th>
    </tr>
    <tr>
        <td>
            <input type="text" name="title0" />
        </td>
        <td>
            <input type="text" name="order0" />
        </td>
        <td>
            <a href="editapp.php"><u>Edit</u></a>
        </td>
        <td>
            <a href="deleteapp.php"><u>Delete</u></a>
        </td>
        <td>
            <a href="uploadapp.php"><u>Upload</u></a>
        </td>
    </tr>
</table>
</form>

JQuery代码:

<script>
jQuery(function(){
    var counter = 1;
    jQuery('span.add-row').click(function(event){
        event.preventDefault();
        counter++;
        var newRow = jQuery('<tr><td><input type="text" name="title' +
            counter + '"/></td><td><input type="text" name="order' +
            counter + '"/></td><td><a href="editapp.php"><u>Edit</u></a></td></td><td><a href="deleteapp.php"><u>Delete</u></a></td><td><a href="uploadapp.php"><u>Upload</u></a></td></tr>');
        jQuery('table.row-list').append(newRow);
    });

    var count = 2;
    jQuery('span.add-week').click(function(event){
        event.preventDefault();
        counter++;
        var newWeek = jQuery('<tr><th>Week ' + (count++) + ' Title</th><th>Order</th><th>Edit</th><th>Delete</th><th>Update</th></tr><tr><td><input type="text" name="title' +
            counter + '"/></td><td><input type="text" name="order' +
            counter + '"/></td><td><a href="editapp.php"><u>Edit</u></a></td><td><a href="deleteapp.php"><u>Delete</u></a></td><td><a href="uploadapp.php"><u>Upload</u></a></td></tr>');
        jQuery('table.row-list').append(newWeek);
    });

});

</script>

正在处理表单数据并提交到数据库的页面:

<?php

    require_once(dirname(dirname(dirname(__FILE__))) . '/config.php');

    global $DB;

    $countWeek = 1;
    $counter = 0;
    $week = $countWeek++;
    $title = required_param("title0", PARAM_TEXT);
    $order = required_param("order0", PARAM_TEXT);

    $counter+1;

    $record2 = new stdClass();
    $record2->week = $week;
    $record2->title = $title;
    $record2->order = $order;
    $record2->displayorder = '10000';

    // Insert one record at a time.

    $lastinsertid2 = $DB->insert_record('mobile', $record2);

    if(!$lastinsertid2)
    {
        echo "Could not insert";
    }
    else
    {
        echo "Successful";
    }

?>

这是包含表单的页面的屏幕截图:

有什么帮助吗?谢谢。

Order 和 week 是保留字。 https://dev.mysql.com/doc/refman/5.5/en/keywords.html

需要反引号。

INSERT INTO mobile (`week`,title,`order`) VALUES(?,?,?)

您应该使用 xmldb 编辑器创建 install.xml 文件,这些文件会为您的本地插件创建 tables。这将检查 table 和字段名称中的保留字 - https://docs.moodle.org/dev/XMLDB_editor

可通过站点管理 -> 开发 -> xmldb 编辑器获取。

避免使用反引号,因为它们依赖于数据库。 Moodle 支持多个数据库,而不仅仅是 mysql。最好继续使用$DB->insert_record()函数