所以我得到了这个表格,我希望能够编辑来自数据库的数据。
我能够将数据读入表格
编辑/更新似乎不起作用。
我的部分HTML代码:
<form id="formAddChallenges" class="FrmChal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<div class="row">
<!-- Left Column -->
<div class="col-md-6">
<input type="hidden" name="hiddenId" value="<?php echo $hiddenID ?>" />
<label class="control-label">Challenge</label>
<input type="text" class="form-control" placeholder="Challenge name" name="challenge" required="" value="<?php echo $challenge ?>" />
<br>
<label class="control-label">Description</label>
<textarea required="" class="form-control" rows="5" name="description"><?php echo htmlspecialchars($description)?></textarea>
<br>
<label class="control-label">Points</label>
<input type="text" class="form-control" placeholder="How many points is this challenge worth?" name="points" required="" value="<?php echo $points ?>" />
<br>
<label class="control-label">Type</label>
<select class="form-control" name="selectType" >
<option disabled selected><?php echo $typeId?></option>
<!-- <option selected></option> -->
<option value="1">Photo</option>
<option value="2">Geolocation</option>
<option value="3">Quiz</option>
<option value="4">Time</option>
<option value="5">Puzzle</option>
<option value="6">Vote</option>
<option value="7">Tracking/Checkpoints</option>
<option value="8">Augmented Reality</option>
</select>
<br>
<label class="control-label">Status</label>
<select class="form-control" name="selectStatus">
<option disabled selected><?php echo $status?></option>
<option value="1">Active</option>
<option value="2">Finished</option>
</select>
<br>
<label class="control-label">Start Date</label>
<input type="text" class="form-control" id="datepicker-autoclose" placeholder="mm/dd/yyyy" name="startdate" value="<?php echo $startD ?>" />
<br>
<label class="control-label">End Date</label>
<input type="text" class="form-control" id="datepicker-autoclose2" placeholder="mm/dd/yyyy" name="enddate" value="<?php echo $endD ?>" />
</div>
<!-- Right Column -->
<div class="col-md-6">
<label class="control-label">Upload a picture</label>
<br>
<?php echo $picture ?>
<br><br><br><br>
<input type="file" id="input-file-now" class="dropify" name="uploadPicture" />
<br>
<label class="control-label">Category</label>
<select class="form-control" name="selectCategory">
<option disabled selected><?php echo $categoryId?></option>
<option value="1">Food & Drink</option>
<option value="2">Creative</option>
<option value="3">Sport</option>
<option value="4">Games</option>
<option value="5">Youtube</option>
<option value="6">Fashion</option>
<option value="7">Animals</option>
<option value="8">Movie & Tv</option>
<option value="9">General</option>
<option value="10">Adventure</option>
<option value="11">Lifestyle</option>
<option value="12">Travel</option>
<option value="13">Music</option>
<option value="14">Events</option>
</select>
<br>
<label class="control-label">Difficulty</label>
<select class="form-control" name="selectDifficulty">
<option disabled selected><?php echo $difficutlyId?></option>
<option value="1">Easy</option>
<option value="2">Normal</option>
<option value="3">Hard</option>
<option value="4">Extreme</option>
</select>
<!-- <div class="fillEmptSpace"></div> -->
</div>
</div><!-- end row -->
<br>
<button type="submit" id="btnEditChallenge" class="btn btn-danger" name="editChallenge">Edit Challenge</button>
</form>
我的部分PHP代码:
<?php
session_start();
include_once 'config.php';//db connection
if(!isset($_SESSION['user_id'])) {
header("Location: login.php");
}
$id = $_GET['challenge_id'];
//select data from db and place it in input field
$query_string = "SELECT * FROM tbl_Challenges
INNER JOIN
tbl_Type ON tbl_Challenges.type_id=tbl_Type.type_id
INNER JOIN
tbl_Status ON tbl_Challenges.status_id=tbl_Status.status_id
INNER JOIN
tbl_Category ON tbl_Challenges.category_id=tbl_Category.category_id
INNER JOIN
tbl_Difficulty ON tbl_Challenges.difficulty_id=tbl_Difficulty.difficulty_id
WHERE challenge_id = '$id'";
$query = mysqli_query($con, $query_string);
while($row = mysqli_fetch_assoc($query))
{
$hiddenID = $row['challenge_id'];
$challenge = $row['challenge'];
$description = $row['description'];
$points = $row["points"];
$picture = '<img src="'.$row['picture'].'" width="250" heigth="186" />';
//$picture = $row['picture'];
$categoryId = $row['category'];
$typeId = $row['type'];
$difficutlyId = $row['difficulty'];
$status = $row['status'];
$startD = $row['start_date'];
$endD = $row['end_date'];
}
//edit/update data to db
if(isset($_POST['editChallenge'])){
$challengeUpdate = mysqli_real_escape_string($con, $_POST['challenge']);
$challengeID = mysqli_real_escape_string($con, $_POST['hiddenId']);
$descriptionUpdate = mysqli_real_escape_string($con, $_POST['description']);
$pointsUpdate = mysqli_real_escape_string($con, $_POST['points']);
$categoryUpdate = mysqli_real_escape_string($con, $_POST['selectCategory']);
$typeUpdate = mysqli_real_escape_string($con, $_POST['selectType']);
$difficultyUpdate = mysqli_real_escape_string($con, $_POST['selectDifficulty']);
$statusUpdate = mysqli_real_escape_string($con, $_POST['selectStatus']);
$startDateUpdate = mysqli_real_escape_string($con, $_POST['startdate']);
$endDateUpdate = mysqli_real_escape_string($con, $_POST['enddate']);
$qry = "UPDATE tbl_Challenges SET
category = '$challengeUpdate',
description = '$descriptionUpdate ',
points = '$pointsUpdate',
category_id = '$categoryUpdate',
type_id = '$typeUpdate',
difficutly_id = '$difficultyUpdate',
status_id = '$statusUpdate',
start_date = '$startDateUpdate',
end_date = '$endDateUpdate'
WHERE challenge_id = '$challengeID
";
$result = mysqli_query($con, $qry);
echo $qry;
}
if($result){
header("Location: challenges.php");
}
?>
当我点击保存按钮时,没有任何内容被保存。 echo $ qry的输出如下所示: