Update does not work

hey guys, so im trying to update but its not working when update button is clicked the page just refreshes. im new to pdo so maybe my codes wrong somewhere?


<?php
if($_SESSION['sess_user_id']){
	if(isset($_POST['update']))
	{
		require "connection.php";
		
		$name = $_POST['ee_name'];
		$sdate = $_POST['ee_sd'];
		$stime = $_POST['ee_st'];
		$edate = $_POST['ee_ed'];
		$etime = $_POST['ee_et'];
		$evenue = $_POST['ee_venue'];
		$id = $_POST['id'];
		$usrid = $_SESSION['sess_user_id'];
		
		$update = $dbh->prepare("UPDATE event SET event_name = '$name', start_date = '$sdate', end_date = '$edate', start_time = '$stime', end_time = '$etime', event_venue = '$evenue' WHERE euser_id = '$usrid' AND event_id = '$id' ");		
		
		if($update->execute())
		{
			header("Location : event.php");
		}
	}
}
?>

tia!

Does it try to run the query? How far through the code does it get? I’d have a look at bindParam() for adding values into prepared queries in PDO as well.


<?php
if($_SESSION['sess_user_id']){
echo("got user id");
	if(isset($_POST['update']))
	{
echo("got update post");
		require "connection.php";
		
		$name = $_POST['ee_name'];
		$sdate = $_POST['ee_sd'];
		$stime = $_POST['ee_st'];
		$edate = $_POST['ee_ed'];
		$etime = $_POST['ee_et'];
		$evenue = $_POST['ee_venue'];
		$id = $_POST['id'];
		$usrid = $_SESSION['sess_user_id'];
		
echo("Got post vars" . $usrid);
		$update = $dbh->prepare("UPDATE event SET event_name = '$name', start_date = '$sdate', end_date = '$edate', start_time = '$stime', end_time = '$etime', event_venue = '$evenue' WHERE euser_id = '$usrid' AND event_id = '$id' ");		
		
		if($update->execute())
		{
			header("Location : event.php");
		}
	}
}
?>

Then lose all the spurious echo() statements when you’ve sorted it.

all the echos echoed. the update part doesn’t seem to work like its not reading it at all. doesn’t execute.

Is error reporting enabled?

ini_set('error_reporting', E_ALL);

Did you check your PHP logs and Apache/Nginx logs to be certain that there was no error?

Try adding:


if($update->execute())
{
            header("Location : event.php");
} else {
             echo "An error occured:";
             $arr = $update->errorInfo();
             print_r($arr);
}

If an error occured in your query, the method “execute()” will return false as stated in the manual: http://www.php.net/manual/en/pdostatement.execute.php (Returns TRUE on success or FALSE on failure. ) So, if it’s the case, it won’t redirect and probably do nothing since you don’t check for the FALSE value.

A warning: If you plan on putting that code live, you should read on prepared statements with PDO, because you are open to SQL injections right now. With prepared statements, your query should look like:
"UPDATE event SET event_name = ?, start_date = ?, end_date = ?, etc…
And then you bind the parameters afterward, that will prevent SQL injections. Do a search on Google for “PDO prepared statements example” and you should find something. :slight_smile:

fixed. thanks for the input!