我正在尝试使用准备语句创建更新页面, 发布item_id和user_id将使用正确的vaules更新页面。 但它不会在输入中列出项目值。
没有错误,看起来一切都很好,除了列出问题。 当我创建只有item_id的链接时,它可以工作,但是当我将链接更改为下面的链接时,它不起作用。
<a href='Esupdate.php?item_id=". $row['item_id'] ."&user_id=".htmlspecialchars($_SESSION['id']) ."' title='Update Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>
这是我的选择代码部分。
if(isset($_GET['item_id']) && !empty(test_input($_GET['item_id'])) AND isset($_GET['user_id']) && !empty(test_input($_GET['user_id']))){
$item_id = test_input($_GET['item_id']);
$user_id = test_input($_GET['user_id']);
// Prepare a select statement
$sql = "SELECT * FROM items WHERE item_id = ? AND user_id = ?";
if($stmt = $conn->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("ii", $param_item_id, $param_user_id);
// Set parameters
$param_item_id = $item_id;
$param_user_id = $user_id;
// Attempt to execute the prepared statement
if($stmt->execute()){
$result = $stmt->get_result();
if($result->num_rows == 1){
while($row = $result->fetch_assoc());
// Retrieve individual field value
$param_cat_id = htmlspecialchars($cat_id);
$param_item_name = htmlspecialchars($item_name);
$param_item_title = htmlspecialchars($item_title);
$param_item_image = htmlspecialchars($item_image);
$param_item_seo_url = htmlspecialchars($item_seo_url);
$param_item_detail = htmlspecialchars($item_detail);
} else{
// URL doesn't contain valid id. Redirect to error page
header("location: error.php");
exit();
}
} else{
echo "Oops! Something went wrong. Please try again later.";
}
}
// Close statement
$stmt->close();
// Close connection
$conn->close();
} else{
// URL doesn't contain id parameter. Redirect to error page
header("location: error.php");
exit();
}
这是表格中应列出的项目。
<form action="<?php echo htmlspecialchars(basename($_SERVER['REQUEST_URI'])); ?>" method="post" enctype="multipart/form-data">
<div class="form-group <?php echo (!empty($item_name_err)) ? 'has-error' : ''; ?>">
<label>Name</label>
<input type="text" name="item_name" class="form-control" value="<?php echo $item_name; ?>">
<span class="help-block"><?php echo $item_name_err;?></span>
</div>
<div class="form-group <?php echo (!empty($item_title_err)) ? 'has-error' : ''; ?>">
<label>Title/label>
<input type="text" name="item_title" class="form-control" value="<?php echo $item_title; ?>">
<span class="help-block"><?php echo $item_title_err;?></span>
</div>
<div class="form-group <?php echo (!empty($item_image_err)) ? 'has-error' : ''; ?>">
<label for="item_image">Image</label>
<input type="file" name="item_image" id="item_image" value="<?php echo $item_image; ?>">
<p><strong>Note:</strong> Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 5 MB.</p>
<span class="help-block"><?php echo $item_image_err;?></span>
</div>
<div class="form-group <?php echo (!empty($item_detail_err)) ? 'has-error' : ''; ?>">
<label>Detail</label>
<textarea name="item_detail" class="form-control"><?php echo $item_detail; ?></textarea>
<span class="help-block"><?php echo $item_detail_err;?></span>
</div>
<div class="form-group <?php echo (!empty($cat_id_err)) ? 'has-error' : ''; ?>">
<select class="form-control" name="cat_id" value="<?php echo $cat_id; ?>">
<option value="">Categories</option>
<?php
categoryTree();
?>
</select>
<span class="help-block"><?php echo $cat_id_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
这是test_input代码来自function.php 我删除了categoryTree函数(列出类别)和test_input代码没有改变。 我怀疑代码中有什么东西。
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
编辑:我必须声明这些变量吗?
$param_cat_id = htmlspecialchars($cat_id);
$param_item_name = htmlspecialchars($item_name);
$param_item_title = htmlspecialchars($item_title);
$param_item_image = htmlspecialchars($item_image);
$param_item_seo_url = htmlspecialchars($item_seo_url);
$param_item_detail = htmlspecialchars($item_detail);
喜欢这个吗?
$item_id = test_input($_GET['item_id']);
$user_id = test_input($_GET['user_id']);
答案 0 :(得分:0)
我建议将bind_param更改为
// Bind variables to the prepared statement as parameters
$stmt->bindParam(1, $param_item_id);
$stmt->bindParam(2, $param_user_id);
然后你可以在之后设置变量(虽然我总是把它们放在绑定之前)
// Set parameters
$param_item_id = $item_id;
$param_user_id = $user_id;
我还建议您将sql更改为
$sql = "SELECT * FROM items WHERE item_id = :itemId AND user_id = :userId";
然后按照以下方式进行绑定;
$stmt->bindParam(':itemId', $param_item_id);
$stmt->bindParam(':userId', $param_user_id);