我正在尝试学习MySql / PDO并且已经按照教程展示了如何设置crud。它工作正常。但是,我希望能够更新记录中的3个字段,但似乎无法使其正常工作。我是否需要在小更新字符串中使用完整结构,或者只需要我想要更新的字段。 这是适用于完整数据库结构的代码: -
// update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE customers set outstanding = ?, estimate = ?, fault_Description = ?, item_Description = ?, email = ?, mobile = ?, name = ?, address = ?, address2 = ?, town = ?, county = ?, postcode = ?, condition01 = ?, leads = ?, model = ?, username = ?, password = ?, backed_Up = ?, location = ? WHERE id = ?";
$q = $pdo->prepare($sql);
/* $q->execute(array($coa,$version,$activated,$owner,$notes,$id)); */
$q->execute(array($outstanding,$estimate,$fault_Description,$item_Description,$email,$mobile,$name,$address,$address2,$town,$county,$postcode,$condition01,$leads,$model,$username,$password,$backed_Up,$location,$id));
Database::disconnect();
header("Location: test-index.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
/* $sql = "SELECT * FROM serials where id = ?"; */
$sql = "SELECT * FROM customers where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$outstanding = $data['outstanding'];
$estimate = $data['estimate'];
$fault_Description = $data['fault_Description'];
$item_Description = $data['item_Description'];
$email = $data['email'];
$mobile = $data['mobile'];
$name = $data['name'];
$address = $data['address'];
$address2 = $data['address2'];
$town = $data['town'];
$county = $data['county'];
$postcode = $data['postcode'];
$condition01 = $data['condition01'];
$leads = $data['leads'];
$COA = $data['COA'];
$model = $data['model'];
$username = $data['username'];
$password = $data['password'];
$backed_Up = $data['backed_Up'];
$location = $data['location'];
Database::disconnect();
}
?>
我想要更新的唯一字段是Outstanding Estimate and Location。
更新: 根据评论我已经减少了对此的完整工作更新,但它不起作用: -
<?php
require 'test-database.php';
$id = null;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( null==$id ) {
header("Location: test-workshop.php");
}
if ( !empty($_POST)) {
// keep track validation errors
$fault_DescriptionError = null;
// keep track post values
$fault_Description = $_POST['fault_Description'];
// validate input
if (empty($fault_Description)) {
$nameError = 'Please Describe the fault';
$valid = false;
}
// update data
if ($valid) {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE customers
set fault_Description = ?
WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($fault_Description,$id));
Database::disconnect();
header("Location: test-workshop.php");
}
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT fault_Description
FROM customers
where id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
$data = $q->fetch(PDO::FETCH_ASSOC);
$fault_Description = $data['fault_Description'];
Database::disconnect();
}
?>
<div class="container">
<div class="span10 offset1">
<div class="row">
<h3>Update a Customer</h3>
</div>
<form class="form-horizontal" action="update-yyy.php?id=<?php echo $id?>" method="post">
<div class="control-group <?php echo !empty($fault_DescriptionError)?'error':'';?>">
<label class="control-label">Fault Description</label>
<div class="controls">
<input name="fault_Description" type="text" placeholder="fault_Description" value="<?php echo !empty($fault_Description)?$fault_Description:'';?>">
<?php if (!empty($fault_DescriptionError)): ?>
<span class="help-inline"><?php echo $fault_DescriptionError;?></span>
<?php endif;?>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-success">Update</button>
<a class="btn" href="test-workshop.php">Back</a>
</div>
</form>