【发布时间】:2019-06-15 20:21:38
【问题描述】:
我正在努力实现的目标
我正在尝试使用用户在表单中填写的值来更新我的数据库。
我已经花了几个月的时间在无休止的 Google 搜索中避免向 StackOverflow 提问,每个网站都教会了我一些东西,但我还没有完成这个产品。
我正在努力解决的问题
提交时,PDO::execute 不执行且数据库不更新。
以前我收到"Undefined index" 的错误,但这些错误已通过正确传递变量得到解决。现在我没有收到任何错误。
我的问题
我做错了什么?考虑到我在控制台或任何地方都没有收到任何错误(并且error_reporting 已打开)。我检查它是否已执行的if 语句总是给出nope,所以在查询的某个地方它失败了。
另外我怎样才能更好地调试这个?
我尝试过的 (代码已被缩小以显示相关内容)
index.php(显示带有 Update 按钮的表格,将我重定向到 update.php)
<?php
session_start();
require 'assets/php/database.php';
require 'assets/php/usersession.php';
?>
<!DOCTYPE html>
<html>
<head>
<!-- irrelevant content -->
</head>
<body>
<?php
$sql = "SELECT * FROM utlansliste";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
?>
<table>
<thead>
<th>ID</th>
<th>Brukernavn</th>
<th>Datamaskin</th>
<th>Periferiutstyr</th>
<th>Start dato</th>
<th>Slutt dato</th>
<th>Oppdater tabell</th>
</thead>
<?php
foreach($result as $rows) {
?>
<tr>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['username']; ?></td>
<td><?php echo $rows['hostname']; ?></td>
<td><?php echo $rows['peripherals']; ?></td>
<td><?php echo $rows['start_date']; ?></td>
<td><?php echo $rows['end_date']; ?></td>
<td><a href="update.php?id=<?php echo $rows['id'];?>&hostname=<?php echo $rows['hostname'];?>"><div class="btn btn-primary">Oppdater</div></a></td>
</tr>
<?php
}
?>
</table>
</body>
</html>
update.php(用户填写表格和执行的地方)
<?php
session_start();
require 'assets/php/database.php';
require 'assets/php/usersession.php';
if(isset($_GET['id'])) {
$id = $_GET['id'];
$hostname = $_GET['hostname'];
global $conn;
$sql = "SELECT * FROM utlansliste WHERE id='$id';";
$stmt = $conn->prepare($sql);
$stmt->execute();
$row = $stmt->fetchAll();
$username = $row[0]['username'];
$peripherals = $row[0]['peripherals'];
$start_date = $row[0]['start_date'];
$end_date = $row[0]['end_date'];
if(isset($_POST['submit'])) {
try {
global $conn;
$sql = "UPDATE utlansliste SET username = ':username', peripherals = ':peripherals', start_date = ':start_date', end_date = ':end_date', WHERE id = ':id'";
$stmt = $conn->prepare($sql);
$stmt->bindParam(":username", $username, PDO::PARAM_STR);
$stmt->bindParam(":peripherals", $peripherals, PDO::PARAM_STR);
$stmt->bindParam(":start_date", $start_date, PDO::PARAM_STR);
$stmt->bindParam(":end_date", $end_date, PDO::PARAM_STR);
$stmt->bindParam(":id", $id, PDO::PARAM_STR);
$stmt->execute();
if ($stmt->execute()) {
echo "gg";
} else {
echo "nope";
}
/*header('Location:index.php');*/
}
catch(PDOException $exception) {
echo "Error: " . $exception->getMessage();
}
}
}
?>
<html>
<head>
<!-- irrelevant content -->
</head>
<body>
<?php include 'assets/php/header.php' ?>
<?php if( !empty($user) ): ?>
<div class="content">
<strong>Oppdater utlånsliste for <?php echo $hostname; ?></strong>
<form name="form" method="POST" action="">
<div class="updatebox" style="text-align:center;">
<label for="username">Brukernavn</label>
<div><input type="text" name="username" value="<?php echo $username;?>" id="username" required/></div>
<label for="peripherals">Periferiutstyr</label>
<div><input type="text" name="peripherals" value="<?php echo $peripherals;?>" id="peripherals"/></div>
<label for="startdate">Låne fra - dato</label>
<div><input data-date-format="YYYY/MM/DD" type="date" name="start_date" value="<?php echo $start_date;?>" id="start_date" required/></div>
<label for="enddate">Låne til - dato</label>
<div><input data-date-format="YYYY/MM/DD" type="date" name="end_date" value="<?php echo $end_date;?>" id="end_date" required/></div>
<input name="id" type="hidden" id="id" value="<?php echo $id;?>"/>
<input name="hostname" type="hidden" value="<?php echo $hostname;?>" id="hostname"/>
<input type="submit" name="submit" value="Submit"/>
</div>
</form>
</div>
<body>
</html>
其他 cmets
我查看了许多寻求帮助和知识的网站。这些只是其中的几个。我主要是在寻找知识来学习这种更新数据库的安全方法,所以即使只是一个评论也有很大帮助!
【问题讨论】:
-
在占位符
username = ':username', peripherals = ':peripherals',周围丢失引号 ~ 尝试改用username = :username, peripherals = :peripherals,等,只有一个$stmt->execute() -
@RamRaider 没有解决。只有一个
$stmt->execute();是什么意思?我只有一个,另一个只是检查查询的if语句。 -
您链接了 PHPDelusions,更具体地说是该指南的更新部分,它显示了 named parameters 没有被引用,我知道 @RamRaider 已经提到了这一点,但我再次提出它的原因是尽管您列出了一堆您浏览过的资源,但您真正阅读过的资源有多少?
-
关于错误报告,资源列表中的最后一个链接将引导您到this,它会教您设置属性,以便 PDO 抛出异常。
-
@Sanguinary 你能把那个答案链接给我吗?这样我就可以在答案上留下注释,以防止错误信息的传播。