PHP loop from SQL table with results looped into a string and it not saving why?
我正在尝试在代码中将\\'newentryname \\'保存为字段名-但我总是遇到错误,当我检查sql表时,它会在刷新表后第一次保存,但是之后它不保存
我正在从另一个表中拉出字段并将它们循环到输入字段中,然后我试图将字段名放入循环中以放入字符串以保存在SQL中,以供其他地方使用。
第1部分:这将连接到同一wordpress SQL表内的单独表-$ wpdb变量包含sql连接信息
1 2 3 4 5 6 | <?php global $wpdb; // Add table name as variable to include prefix $rowname = $wpdb->prefix . 'rowname'; $results = $wpdb->get_results("SELECT * FROM $rowname"); // $selected = $item['name']; |
第2节:这部分代码将循环中的输入框作为复选框回显
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** Loop through the $results and add each as a checkbox options */ $checked = ' '; foreach($results as $result) : $resultid = $result->ID; $resultrowname = $result->name; $options.= sprintf("\\t".'<input type="checkbox" ' . '%1$s' . 'value="%3$s" name="newentryname[]">%3$s'."\ ", $checked, $resultid, $resultrowname); endforeach; |
注意:上面我不确定newentryname []是否是引起错误的原因-我无法确定这是要发送给数据库的信息,理想情况下,我只希望它存储要存储的数据。在下面的第3节中使用:
第3节:这应该合并上表中输入的数据,并将其添加到字符串中,以便可以将其保存在单个字段中-但我不确定代码是否能走得那么远。
1 2 3 4 5 6 7 8 9 10 11 | $fieldname=$item['fieldname']; // You have to loop through the array of checked box values ... $newentryname=""; foreach($fieldname as $entry): $newentryname .= $entry.","; endforeach; echo $options; ?> |
编辑:上面是一个函数
函数名($ item)
并将其插入数据库的代码是:
$ result = $ wpdb-> insert($ table_name,$ item);
我想知道是否还将字段名\\'newentryname \\'提交给数据库,如何解决该问题,以及如何将第三节中的数据提交到数据库中?
需要在foreach循环中添加$ n = 0和n
1 2 3 4 5 6 7 8 9 10 11 12 13 | $checked = ' '; $n = 0; foreach($results as $result) : $resultid = $result->ID; $resultrowname = $result->name; $n++; $options.= sprintf("\\t".'<input type="checkbox" ' . '%1$s' . 'value="%3$s" name="newentryname[]">%3$s'."\ ", $checked, $resultid, $resultrowname); endforeach; |
,然后我替换了下面这段代码:
1 2 3 4 5 6 7 8 9 10 | $fieldname=$item['fieldname']; // You have to loop through the array of checked box values ... $newentryname=""; foreach($fieldname as $entry): $newentryname .= $entry.","; endforeach; echo $options; |
使用此代码,可以处理输出并回显输出,以便可以将其发送到其他地方
1 2 3 4 5 6 7 8 | $fieldname = $_POST['fieldname']; foreach($fieldname as $entry): $output .= $entry.","; endforeach; echo $output; |
我仍然收到错误,这是由于我的数组结构的工作方式而引起的,它仍然不喜欢数组中的数组,但是要更正上面的代码,则需要执行上述操作。