Carlos Perez
Carlos Perez

Reputation: 435

Php update query not working

This code has a form and when the use click submit I trying to update a database but I don't know for some reason my update query is not working, this piece of code detects an eror

 catch(PDOException $ex)
      {
        die("Error");
      }

any ideas of how to fix this problem

if(empty($_SESSION['user']))
{
  header("Location: index.php");
  die("Redirecting to index.php");
}

if(!empty($_POST))
{
  if(empty($_POST['name']))
  {
    die("Enter a name");
  }

  $query = "SELECT 1 FROM courses WHERE name = :name";
  $query_params = array(':name' => $_POST['name']);

  try
  {
    $stmt = $db->prepare($query);
    $result = $stmt->execute($query_params);
  }
  catch(PDOException $ex)
  {
    die("Error 1");
  }

  $row = $stmt->fetch();

  if($row)
  {
      die("Same Name");
  }

  $nombre = $_SESSION['user']['username'];
  $name = $_POST['name'];
  $query = "UPDATE courses SET name = '$name' WHERE id = 1)";


  try
  {
    $stmt = $db->prepare($query);
    $stmt->execute();
  }
  catch(PDOException $ex)
  {
    die("Error");
  }

  header("Location: index.php");
  die("Redirecting to index.php");
}


?> 
<?php 
foreach($rows as $row):    
  echo '<form action="mod.php" method="post">';
  echo '<input type="text" name="name" value="' .htmlentities($row['name']) . '" />'; 
  echo '<input type="submit" value="submit">';
  echo '</form>';
endforeach; 

Upvotes: 0

Views: 123

Answers (2)

Gus de Boer
Gus de Boer

Reputation: 401

Make sure to put your PHP variable the following way:

$query = "UPDATE courses SET name = '".$name."' WHERE id = 1";

Upvotes: 0

The right way

$query = "UPDATE courses SET name = '$name' WHERE id = 1";

There was a parenthesis hanging on the end.

 $query = "UPDATE courses SET name = '$name' WHERE id = 1)";
                                                         ^---- Here

As Gerald Schneider mentioned in this comment, please do the change so you can track your errors at ease.

Upvotes: 3

Related Questions