我一定是漏掉了什么,没有任何东西被发送到数据库。我只是想把这个信息发送到2个表中。更新一个并在另一个中插入新行。
我已经测试了数据及其收集,并且仅测试了发送到1个表,但仍然没有发送任何信息。
表格:
<div class="container"><center>
<form action ="<?php echo $_SERVER['REQUEST_URI']; ?>" method ="post">
<table class="form-table" id='student'>
<tbody>
<tr>
<th>Class</th>
<th>Assignment Id</th>
<th>Student Name</th>
<th>Grade</th>
</tr>
<?php foreach($students as $row){ ?>
<tr>
<input type="hidden" name="student_name" size="30" value="<?php echo "$row->student_name" ?>">
<input type="hidden" name="student_id" size="30" value="<?php echo "$row->student_id" ?>">
<td><Center><?php echo "$class"; ?></Center></td>
<td><Center><?php echo "$assign"; ?></Center></td>
<td><Center><?php echo "$row->student_name"; ?></Center></td>
<td><Center><input type="text" name="grade" size="15" placeholder="Points"></Center></td>
</tr>
<?php } ?>
</tbody>
</table>
<p class="submit"><Center><input type="submit" name="submitgrade" id="submitgrade" class="button button-primary" value="Submit" /></center></p> </form>
</center></div>MYSQL查询:
if ($_POST['submitgrade']){
global $wpdb;
$success = $wpdb->insert(
'sodae_grades',
array(
"student_name" => $_POST['student_name'],
"student_id" => $_POST['student_id'],
"class_of" => $class,
"assignment_name" => $assign,
"grade" => $_POST['grade']
)
);
$success2 = $wpdb->update(
'sodae_enrolled',
array(
"grade" => $_POST['grade']
),
array(
"student_id" => $_POST['grade'] + $count
)
);
if($success) {
echo ' Inserted successfully';
} else {
echo 'not';
}
if($success2) {
echo ' Inserted successfully2';
} else {
echo 'not2';
}
}这两个语句都返回为not posted,并且数据库中没有任何内容。
编辑:我已经尝试了define('WP-DEBUG',true);和$wpdb->print_error();没有任何错误,我检查了其他表单,除了我使用wp_insert_user时它们都没有发布,当然这只是用户输入。
发布于 2019-06-18 23:41:45
也许它不能识别table前缀。还应指定数据类型,如字符串(%s)或整数(%d)。尝试如下所示:
global $wpdb;
$table_name = $wpdb->prefix . 'sodae_enrolled';
$wpdb->insert($table_name, array(
"student_name" => $_POST['student_name'],
"student_id" => $_POST['student_id'],
"class_of" => $class,
"assignment_name" => $assign,
"grade" => $_POST['grade']
), array(
"%s",
"%s",
"%s",
"%s",
"%s"
));发布于 2019-06-19 14:28:21
因此,通过使用$wpdb->last_error,我能够修复一些链接到数据库中字段的错误,并修复一些拼写错误。一旦我有了错误指南,就很容易修复。
https://stackoverflow.com/questions/56652175
复制相似问题