此代码有一个表单,当使用单击提交时,我尝试更新数据库,但我不知道由于某种原因我的更新查询不起作用,这段代码检测到错误
catch(PDOException $ex)
{
die("Error");
}
关于如何解决这个问题的任何想法
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;
最佳答案
正确的方法
$query = "UPDATE courses SET name = '$name' WHERE id = 1";
最后有一个括号。
$query = "UPDATE courses SET name = '$name' WHERE id = 1)";
^---- Here
正如 Gerald Schneider 在 comment 中提到的那样,请进行更改,以便您可以轻松跟踪错误。
关于Php 更新查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22856810/