简体   繁体   中英

Error writing to database-Moodle

I have a page in Moodle where a user can add consecutive rows through JQuery and each time the Save button is clicked, the data entered in the form will be written to the database but i'm getting these error messages:

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',
)]

The form to which consecutive rows is being appended:

 <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>

The JQuery codes:

<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>

The page which is handling the form data and submitting it to the database:

<?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";
    }

?>

Here's a screenshot of the page containing the form:

在此处输入图片说明

Any help please? Thanks.

Order and week are reserved words. https://dev.mysql.com/doc/refman/5.5/en/keywords.html

They need to be backticked.

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

You should use the xmldb editor to create the install.xml files which creates the tables for your local plugin. This will check for reserved words in the table and field names - https://docs.moodle.org/dev/XMLDB_editor

It is available via site admin -> development -> xmldb editor.

Avoid using backticks because these are database dependent. Moodle supports several databases not just mysql. It is better to continue to use the $DB->insert_record() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM