我有这个HTML表单,旨在更新MySQL数据库中的值。在页面加载时,表中将填充数据库中的现有数据。 当我发出POST命令时,数据库中除DATE以外的所有字段都会更新。我在这里做什么错了?
MySQL表:
----------------------------------------------------------------------------------
|# | Name | Type | Collation | Null | Default | Extra |
----------------------------------------------------------------------------------
|1 | ID | int(11) | | No | none | auto_inc |
|2 | username | varchar(50) | utf8_general_ci | No | none | |
|3 | date | date | | No | none | |
|4 | mission | varchar(100) | utf8_general_ci | No | none | |
|5 | transFrom | varchar(100) | utf8_general_ci | No | none | |
|6 | transpTo | varchar(100) | utf8_general_ci | No | none | |
|7 | payment | varchar(100) | utf8_general_ci | No | none | |
|8 | comment | text | utf8_general_ci | No | none | |
|9 | created_at | datetime | | Yes | curnt_tstmp | |
----------------------------------------------------------------------------------
表格:
<?php
require_once("db.php");
$arr = $conn->getMissionsId();
$mission_id = isset($_GET['id']) ? $_GET['id'] : '';
for($i=0; $i < count($arr); $i++) {
if ($id = $mission_id && $arr[$i]['username'] == $_SESSION['username']) {
$username = $arr[$i]['username'];
$date = $arr[$i]['date'];
$mission = $arr[$i]['mission'];
$from = $arr[$i]['transpFrom'];
$to = $arr[$i]['transpTo'];
$payment = $arr[$i]['payment'];
$comment = $arr[$i]['comment'];
}
}
?>
<div class='newMissionForm'>
<form method='POST' action='?p=editMissionUpdate' enctype="multipart/form-data">
<div class='masterHeading'>Oppdater oppdrag</div>
<input type='hidden' name='id' id='id' class='inputClass' value='<?php echo $id ?>'>
<div class='form1'><label for='date' class='labelClass'>Dato</label><input type='date' name='date' id='date' class='inputClass' value='<?php echo $date ?>'></div>
<div class='form1'><label for='mission' class='labelClass'>Type oppdrag</label>
<select class='form_control inputClass' id='mission' name='mission'>
<option value=''>Velg oppdragstype</option>
<option value='Vakt hverdag'<?php if ($mission == "Vakt hverdag") echo " selected='selected'"; ?>>Vakt hverdag</option>
<option value='Vakt helg'<?php if ($mission == "Vakt helg") echo " selected='selected'"; ?>>Vakt helg</option>
<option value='Vakt helgedag'<?php if ($mission == "Vakt helgedag") echo " selected='selected'"; ?>>Vakt helgedag</option>
<option value='Vakt hel uke'<?php if ($mission == "Vakt hel uke") echo " selected='selected'"; ?>>Vakt hel uke</option>
<option value='Vakt firedagers uke'<?php if ($mission == "Vakt firedagers uke") echo " selected='selected'"; ?>>Vakt firedagers uke</option>
<option value='Tillegg helgedag'<?php if ($mission == "Tillegg helgedag") echo " selected='selected'"; ?>>Tillegg helgedag</option>
</select>
<div id='missionInfo1' class='<?php if ($mission !== 'Vakt hverdag') { echo ' none'; } ?> missionInfo'><?php infoVaktHverdag() ?></div>
<div id='missionInfo2' class='<?php if ($mission !== 'Vakt helg') { echo ' none'; } ?> missionInfo'><?php infoVaktHelg() ?></div>
<div id='missionInfo3' class='<?php if ($mission !== 'Vakt helgedag') { echo ' none'; } ?> missionInfo'><?php infoVaktHelgedag() ?></div>
<div id='missionInfo4' class='<?php if ($mission !== 'Vakt hel uke') { echo ' none'; } ?> missionInfo'><?php infoVaktUke() ?></div>
<div id='missionInfo5' class='<?php if ($mission !== 'Vakt firedagers uke') { echo ' none'; } ?> missionInfo'><?php infoVakt4dgUke() ?></div>
<div id='missionInfo6' class='<?php if ($mission !== 'Tillegg helgedag') { echo ' none'; } ?> missionInfo'><?php infoTilleggHelg() ?></div>
</div>
<div class='form1'><label for='from' class='labelClass'>Transport fra</label><input type='text' name='from' id='from' class='inputClass' value='<?php echo $from ?>'></div>
<div class='form1'><label for='to' class='labelClass'>Transport til</label><input type='text' name='to' id='to' class='inputClass' value='<?php echo $to ?>'></div>
<div class='form1'><label for='payment' class='labelClass'>Lønn</label><input type='text' name='payment' id='payment' class='inputClass readonly' value='<?php echo $payment ?>' readonly></div>
<div class='form1'><label for='comment' class='labelClass'>Kommentar</label>
<textarea name='comment' id='comment' class='inputClass2'><?php echo $comment ?></textarea></div>
<button type="submit" name="submit_form" class="btn btn-primary">Oppdater oppdrag</button>
</form>
<?php btnCancel(); ?>
</div>
函数getMIssionsID()
:
public function getMissionsId()
{
$arr = array();
$mission_id = isset($_GET['id']) ? $_GET['id'] : '';
$statement = $this->conn->prepare("SELECT id, username, date, mission, transpFrom, transpTo, payment, comment, created_at from missions where id = $mission_id");
//echo $this->conn->error;
$statement->bind_result($id, $username, $date, $mission, $from, $to, $payment, $comment, $created_at);
$statement->execute();
while ($statement->fetch()) {
$arr[] = [ "id" => $id, "username" => $username, "date" => $date, "mission" => $mission, "transpFrom" => $from, "transpTo" => $to, "payment" => $payment,
"comment" => $comment, "created_at" => $created_at];
}
$statement->close();
return $arr;
}
发出POST命令(editMissionUpdate.php)时被调用的文件:
<?php
require_once("db.php");
$id = intval($_POST['id']);
$mission = strip_tags($_POST['mission']);
$rawdate = htmlentities($_POST['date']);
$date = date('Y-m-d', strtotime($rawdate));
$from = strip_tags($_POST['from']);
$to = strip_tags($_POST['to']);
$payment = strip_tags($_POST['payment']);
$comment = strip_tags($_POST['comment']);
$conn->updateMission($id, $mission, $date, $from, $to, $payment, $comment);
?>
<div style='width: 50em'>
<div class='masterHeading'>Oppdraget er oppdatert</div>
<br><br>
<div class='descrText'>Oppdraget ble vellykket oppdatert.</div>
<?php btnCancel(); ?>
</div>
函数updateMission()
:
public function updateMission($id, $mission, $date, $from, $to, $payment, $comment)
{
$statement = $this->conn->prepare("UPDATE missions SET mission = ?,date = ?,transpFrom = ?,transpTo = ?,payment = ?,comment = ? WHERE id = ?");
echo $this->conn->error;
$statement->bind_param('ssssssi', $mission, $date, $from, $to, $payment, $comment, $id);
$statement->execute();
$statement->close();
}
答案 0 :(得分:1)
只需将$ id = $ mission_id更改为$ id = $ arr [$ i] ['id']即可解决问题:)