要儘量保持簡短。我有grid.php文件while循環填補一個表作爲這樣...更新查詢不起作用php
<?php while($product = $products->fetch_assoc()) { ?>
<tr>
<td><?php echo $product['cd_id']?></td>
<td><?php echo $product['cd_title']?></td>
<td><?php echo $product['cd_musician_fname']?></td>
<td><?php echo $product['cd_musician_lname']?></td>
<td><?php echo $product['cd_price']?></td>
<td><a href="edit.php?id=<?echo $product['cd_id'];?>" type="button" class="btn btn-primary">Edit</a></td>
<td><a href="delete.php?id=<?echo $product['cd_id'];?>" type="button" class="btn btn-danger">Delete</a></td>
</tr>
<?php } ?>
如果我點擊第一個錨標記帶我到一個edit.php文件,這裏是頭部代碼文件。
<?php include '_includes/db.php';
$cd_id = trim($_GET['id']);
$message = '';
include '_includes/connection.php';
if($db->connect_error){
$message = $db->connect_error;
}else{
$sql = "SELECT * FROM CD WHERE cd_id = $cd_id";
$result = $db->query($sql);
$row = $result->fetch_assoc();
if($db->error){
$message = $db->error;
}
}
?>
現在我就告訴edit.php的HTML
<!-- Product Musician last name-->
<fieldset class="form-group">
<label for="cd_musician_lname">Musician's lirst name</label>
<input type="text" class="form-control" id="cd_musician_lname" name="cd_musician_lname" value="<?php echo $row['cd_musician_lname'];?>">
</fieldset>
<!-- End of Musician last name-->
<!-- Product price-->
<fieldset class="form-group">
<label for="cd_price">Product price</label>
<input type="text" class="form-control" id="cd_price" name="cd_price" value="<?php echo $row['cd_price'];?>">
</fieldset>
<!-- End of Product price-->
<!-- Form submit button-->
<a href="edit_confirm.php?id=<?echo $row['cd_id'];?>" type="submit" class="btn btn-primary">Update Record</a>
<a class="btn btn-primary" href="index.php" role="button">Go Back Home</a>
我有edit.php頁的工作得很好,但如果我在田野裏的變化並點擊提交錨標記,我得到該行的所有字段都是空的,但是PK。下面是最終edit_confirm.php文件
<?php
include '_includes/db.php';
$cd_id = trim($_GET['id']);
$cd_title = $_POST['cd_title'];
$cd_musician_fname = $_POST['cd_musician_fname'];
$cd_musician_lname = $_POST['cd_musician_lname'];
$cd_price = $_POST['cd_price'];
$message = '';
include '_includes/connection.php';
if($db->connect_error){
die("Connection failed: ".$db->connect_error);
} else {
$sql = "UPDATE CD SET cd_title='".$cd_title."', cd_musician_fname='".
$cd_musician_fname."', cd_musician_lname='".
$cd_musician_lname."', cd_price='".$cd_price."' WHERE cd_id = $cd_id ";
$db->query($sql);
var_dump($sql);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<?php include '_includes/main-head.php';?>
</head>
<body>
<?php include '_includes/main-navbar.php';?>
<div class="container">
<hr>
<?php
if($db->query($sql) === TRUE){ ?>
<h1>Record updated successfully.</h1>
<?php echo $cd_title; ?>
<?php echo $record->affected_rows ?>
<p> record was updated in the database.</p></br>
<?php } else { ?>
<p>Error updating the record: </p> <?php $db->error; ?>
<?php }; ?>
<hr>
<a class="btn btn-primary" href="index.php" role="button">Go Back Home</a>
</div>
<?php include '_includes/main-script.php';?>
</body>
</html>
代碼如果您在edit_confirm.php我做了的var_dump看什麼都在變量值發現,它顯示爲空。
我需要幫助。 預先感謝您。
您是否啓用短標記? '<?echo $ product ['cd_id'];?>'(以前沒有使用過短標籤,我不確定這個echo是否可以運行)。您也不應該直接將用戶輸入傳遞給SQL。 – chris85
Cd_price用引號引起來。它是一個字符或數字數據類型? –
我不知道是否啓用短標籤。我只是添加了缺少的PHP。我認爲只有一個失蹤。 – Gilbert