【发布时间】:2018-07-23 06:53:39
【问题描述】:
我有这个 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();
}
【问题讨论】:
-
我不知道怎么了,但我不喜欢你的
getMissionsId()方法。它得到一个数组而不是一个标识符。所以名称与其功能不符。此外,它依赖于$_GET['id']的存在,这是有问题的标识符。所以它甚至没有“得到”它,它需要它。我会使用参数而不是超级全局$_GET,并将方法命名为getMissionRow($missionId);。 -
谢谢!我应该使用
$_SESSION传递参数吗? -
我无法回答这个问题,这需要我完全理解您的代码,而我没有。只是不要在类的方法中使用超级全局
$_GET。 “为什么?”;你问?一方面,这意味着您可以使用独立于任何 url 查询参数的方法。此外,一个方法应该做一件事。向其中添加查询参数的检索和验证只会使事情变得混乱。这有很多花哨的术语,但请记住,方法的名称应该反映它的作用,并且它应该只做一件明确定义的事情。 -
另一种思考方式是:如果你不能给一个方法一个简短而清晰的名称,准确描述它的作用,那么你可能正在这样做错误的。您要么在一种方法中做太多事情,要么更糟糕的是,您根本不知道其中发生了什么。