I have a problem with while loop I used
$row = $result->fetch_array(MYSQLI_ASSOC);
Since result was set contains only one row it was fine,
but I needed to check editer with both ids, item_id and user_id,
then had to use while loop and code stopped listing values.
why I needed to check editer with item_id and user_id ?
I have 3 user roles if a user in same role steals the ids from another user oops he/she could delete,edit, item.
here is the code SELECT PART OF THE CODE (which will selet item values from items table and fill in the form )
So this is a question and a lesson (espacialy for newcomers like me )
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();
}
and Html form in update_item.php
<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>
And this is a small function to clear some html tags in posts can be use in forms when validating.
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
Ok Main problem is I cant list item values from database to update table.
need your help, I am using $conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);oop