PDO Update record not updating

Hello. I have a problem with UPDATE. The data is retrieved from the database without error, but the issue arises when I change some data and then try to update it. If the update is successful the page is redirected back to index. Since this does not happen, I assume there is an issue in the code.

The php code:

<?php
require_once("dbcon/dbc.php");
if(!empty($_POST["save_record"])) {
	$pdo_statement = $dbh->prepare("update pages set
		pagelinks='" . $_POST[ 'pagelinks' ] . "',
		title='" . $_POST[ 'title' ] . "'.
		body='" . $_POST[ 'body' ]. "',
		side='". $_POST[ 'side' ]. "'.
		sourceref='" . $_POST[ 'sourceref' ]. "',
		sourceimg='" . $_POST[ 'sourceimg' ]. "'
 
	where id=" . $_GET["id"]);
	$result = $pdo_statement->execute();
	
	if($result) {
		header('location:index.php');
	}
}
	$pdo_statement = $dbh->prepare("SELECT * FROM pages where id=" . $_GET['id']);
	$pdo_statement->execute();
	$result = $pdo_statement->fetchAll();
?>

This is the form (including table) html:

<table class="table table-bordered xTable zTable">
	
		<form name="frmAdd" action="" method="POST">
		
			<tr>
			
				<td>
				
					<h3>Page Links</h3>
				
				</td>
				
				<td>
				
					<input type="text" name="pagelinks"value="<?php echo $result[0]['pagelinks']; ?>">
				
				</td>
			
			</tr>
			
			<tr>
			
				<td>
				
					<h3>Title</h3>
					 
				</td>
				
				<td>
				
					  <input type="text" name="title" value="<?php echo $result[0]['title']; ?>">
				
				</td>
			
			</tr>
			
			<tr>
			
				<td>
				
					<h3>Body</h3>
				
				</td>
				
				<td>
				
					<textarea name="body"><?php echo $result[0]['body']; ?></textarea>
				
				</td>
			
			</tr>
			
			<tr>
			
				<td>
				
					<h3>Side</h3>
				
				</td>
				
				<td>
				
					<textarea name="side"><?php echo $result[0]['side']; ?></textarea>
				
				</td>
			
			</tr>
			
			<tr>
			
				<td>
				
					<h3>Source Ref</h3>
				
				</td>
				
				<td>
				
					<textarea name="sourceref"><?php echo $result[0]['sourceref']; ?></textarea>
				
				</td>
			
			</tr>
			
			<tr>
			
				<td>
				
					<h3>Source Img</h3>
				
				</td>
				
				<td>
				
					<textarea name="sourceimg"><?php echo $result[0]['sourceimg']; ?></textarea>
				
				</td>
			
			</tr>
			
			<tr>
			
				<td colspan="2" class="text-center">
				
					 <input name="add_record" class="btn btn-warning" type="submit" value="Update">				
				
				</td>
			
			</tr>
		
		</form>
	
	</table>

I understand you can find out if there are errors with php code, but I have no idea how to do this, or how to implement it within my code.

I would be grateful if you can point out the errors, and if there is code to identify the errors I would appreciate the code as it would appear in mine.

Thanks in advance.

This is not a prepared statement.

The main point of a prepared query is that you DON’T put external/unknown/dynamic values directly into the sql query statement. You put a simple ? place-holder into the sql query statement for each value, then supply the values, as an array, when you call the execute([]) method. While a prepared query adds one php statement per query, it simplifies the sql query syntax, making it easier to build an error free query or to see what is wrong with the sql query syntax you have. When you remove the variables from the sql query statement, you also remove all the single-quotes around the variables and remove all the concatenation dots and the extra double-quotes, leaving you with just the sql query syntax and place-holders.

To add error handling for all the database statements that can fail, use exceptions for errors and in most cases let php catch and handle the exception where it will use its error related settings to control what happens with the actual error information (database statement errors will ‘automatically’ get displayed/logged the same as php errors.)

To enable exceptions for all PDO statement errors (the connection always uses exceptions for errors), you would set the error mode to exceptions when you make the database connection. You should also set the character set for the connection, set emulated prepared queries to false, and set the default fetch mode to assoc. The following is an example of typical PDO connection code with these settings -

$DB_HOST = ''; // database host name or ip address
$DB_USER = ''; // database username
$DB_PASS = ''; // database password
$DB_NAME = ''; // database name
$DB_ENCODING = 'utf8'; // db character encoding. set to match your database table's character set

$options = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, // set the error mode to exceptions
			PDO::ATTR_EMULATE_PREPARES => false, // run real prepared queries
			PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, // set default fetch mode to assoc
			];

$pdo = new pdo("mysql:host=$DB_HOST;dbname=$DB_NAME;charset=$DB_ENCODING",$DB_USER,$DB_PASS,$options);

I personally wouldn’t worry about exceptions about them right away (though turn them on).

$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

I would worry about errors in general and turn on error reporting.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

As for PDO - I would read or look over this link - https://phpdelusions.net/pdo

I’m not knocking you, but I can never understand why people put a HTML table inside a form? You can stylize a form in CSS or simply use a table if that is what your want to do.

Thanks for the information. Will research the subject.

Thanks for the advice and information. Will readjust my code soon.

Thanks for the advice. I may consider removing the table at some point, but right now, I’m more interested in getting my code to work.

Sponsor our Newsletter | Privacy Policy | Terms of Service