MyBB Community Forums

Full Version: update_query on post method not working
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Currently trying to build a plugin and while all other parts are working as expected to the edit script, which is supposed to update the database table, is not.

When clicking submit after making changes I get the redirect success message. However, when I look at the database and go to edit or view this particular one no changes were made to the field I updated. If anyone has any idea why I'd greatly appreciate feedback.

I'm also throwing "Trying to access array offset on value of type null" error for the following that I'm not sure why?:
  • line 355: $lang->postingtables_editing = $lang->sprintf($lang->postingtables_editing, $ptable['name']);
  • line 356-360: $mybb->input['ptname'] = $ptable['name']; $mybb->input['ptheader'] = $ptable['header']; $mybb->input['ptfooter'] = $ptable['footer']; $mybb->input['ptspeech'] = $ptable['speech']; $mybb->input['ptspeechalt'] = $ptable['speech_alt'];

postingtables_usercp_edit Template
<html>
	<head>
		<title>{$lang->user_cp} - {$lang->postingtables_edit}</title>
		{$headerinclude}
	</head>
	<body>
		{$header}
		<table width="100%" border="0" align="center">
			<tr>
				{$usercpnav}
				<td valign="top">
					{$errors}
					<form action="usercp.php" method="post">
						<input type="hidden" name="my_post_key" value="{$mybb->post_code}" />
						<input type="hidden" name="action" value="postingtables-edit" />
						<table border="0" cellspacing="{$theme['borderwidth']}" cellpadding="{$theme['tablespace']}" class="tborder">
							<tr>
								<td class="thead" colspan="50"><strong>{$lang->postingtables_editing}</strong></td>
							</tr>
							<tr>
								<td class="tcat" colspan="50">{$lang->postingtables_create_warning}</td>
							</tr>
							<tr>
								<td class="trow1" width="40%" valign="top"><strong>{$lang->postingtables_table_name}</strong><br /><span class="smalltext">{$lang->postingtables_table_name_desc}</span></td>
								<td class="trow1" valign="top"><input type="text" name="ptname" class="textbox" maxlength="250" value="{$mybb->input['ptname']}" style="width: 99%;" /></td>
							</tr>
							<tr>
								<td class="trow2" width="40%" valign="top"><strong>{$lang->postingtables_table_header}</strong><br /><span class="smalltext">{$lang->postingtables_table_header_desc}</span></td>
								<td class="trow2" valign="top"><textarea name="ptheader" rows="10" cols="50" style="width: 99%;">{$mybb->input['ptheader']}</textarea></td>
							</tr>
							<tr>
								<td class="trow1" width="40%" valign="top"><strong>{$lang->postingtables_table_footer}</strong><br /><span class="smalltext">{$lang->postingtables_table_footer_desc}</span></td>
								<td class="trow1" valign="top"><textarea name="ptfooter" rows="10" cols="50" style="width: 99%;">{$mybb->input['ptfooter']}</textarea></td>
							</tr>
							<tr>
								<td class="trow1" width="40%" valign="top"><strong>{$lang->postingtables_table_speech}</strong><br /><span class="smalltext">{$lang->postingtables_table_speech_desc}</span></td>
								<td class="trow1" valign="top"><input type="text" name="ptspeech" class="textbox" value="{$mybb->input['ptspeech']}" style="width: 99%;" /></td>
							</tr>
							<tr>
								<td class="trow1" width="40%" valign="top"><strong>{$lang->postingtables_table_speech_alt}</strong><br /><span class="smalltext">{$lang->postingtables_table_speech_alt_desc}</span></td>
								<td class="trow1" valign="top"><input type="text" name="ptspeechalt" class="textbox" value="{$mybb->input['ptspeechalt']}" style="width: 99%;" /></td>
							</tr>
						</table>
						<br />
						<div class="float_right">
							<input type="submit" class="button" name="submit" value="Submit & Save" />
						</div>
					</form>
				</td>
			</tr>
		</table>
		{$footer}
	</body>
</html>

$mybb->get_input("action") == "postingtables-edit" PHP
		global $mybb, $db, $theme, $templates, $header, $headerinclude, $footer, $usercpnav, $lang, $errors;
		$lang->load("postingtables");
		$ptableid = intval($mybb->input['ptid']);
		add_breadcrumb($lang->nav_usercp,"usercp.php");
		add_breadcrumb("$lang->postingtables_name","usercp.php?action=postingtables");
		add_breadcrumb("$lang->postingtables_edit","usercp.php?action=postingtables-edit");
			$fetch_query = $db->simple_select("postingtables", "*", "ptid = $ptableid");
			$ptable = $db->fetch_array($fetch_query);
			$lang->postingtables_editing = $lang->sprintf($lang->postingtables_editing, $ptable['name']);
			$mybb->input['ptname'] = $ptable['name'];
			$mybb->input['ptheader'] = $ptable['header'];
			$mybb->input['ptfooter'] = $ptable['footer'];
			$mybb->input['ptspeech'] = $ptable['speech'];
			$mybb->input['ptspeechalt'] = $ptable['speech_alt'];
			if ($mybb->request_method == "post") {
				verify_post_check($mybb->input['my_post_key']);
					$dataupdates = array(
						"name" => $db->escape_string($mybb->get_input("ptname")),
						"header" => $db->escape_string($mybb->get_input("ptheader")),
						"footer" => $db->escape_string($mybb->get_input("ptfooter")),
						"speech" => $db->escape_string($mybb->get_input("ptspeech")),
						"speech_alt" => $db->escape_string($mybb->get_input("ptspeechalt"))
					);
					$db->update_query("postingtables", $dataupdates, "ptid = $ptableid");
					redirect("usercp.php?action=postingtables", $lang->postingtables_edit_success_redirect);
			}
(2023-09-10, 10:14 PM)Taylor M Wrote: [ -> ]when I look at the database and go to edit or view this particular one no changes were made to the field I updated. If anyone has any idea why I'd greatly appreciate feedback.

You're storing the same values back into the database as you retrieved from it.

(2023-09-10, 10:14 PM)Taylor M Wrote: [ -> ]I'm also throwing "Trying to access array offset on value of type null" error for the following that I'm not sure why?

Because $db->fetch_array($fetch_query) is returning null, indicating that there are no rows in postingtables with ptid equal to $ptableid.
Out of curiosity if there are no rows being returned with the fetch array why are the variables working in the template?
[attachment=46314][attachment=46315]
(2023-09-11, 12:33 PM)Taylor M Wrote: [ -> ]Out of curiosity if there are no rows being returned with the fetch array why are the variables working in the template?

My guess is that you're correctly supplying ptid as an input parameter in that execution path, which is why the query works and the variables are shown correctly.

However, you don't seem to be propagating it in the form, judging by the template you shared: ptid should perhaps be a hidden input, but it doesn't seem to occur as an input at all. Probably, then, intval($mybb->input['ptid']) is evaluating to zero after you submit the form, because mybb->input['ptid'] is unset, which is why no rows are matching.