我正在处理将类别添加到类别表的页面。我得到了插入(添加新类别)功能,但我的更新功能未更新该值。我已经看到过类似的其他问题,但它似乎无法解决我的问题,除了“更新”功能之外,我无法专门识别该问题。使用文本框和编辑按钮更新MySQL表(使用HTML和PHP)
我怀疑我的更新查询,但我相信它是正确的。所以我很难过,并寻找另一个人,看看他们是否抓住了一些东西。
<html>
<body>
<h1>Current Categories:</h1>
<?php
$con=mysqli_connect("hidden","hidden","hidden","hidden");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$strSQL = "SELECT * FROM Category WHERE Class_idClass = 1";
//WHERE $_SESSION['user'] =
$result = mysqli_query($con,$strSQL);
?>
<table border='1'>
<tr>
<th>Category</th>
<th>Weight</th>
<th>Category Type</th>
<th>Est. # of Assignments</th>
</tr>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td>
<input type="text" name="CategoryTitle" size="20" value="<?php echo $row['CategoryTitle']; ?>">
</td>
<td>
<input type="text" name="CategoryWeight" size="20" value="<?php echo $row['CategoryWeight']; ?>" onkeypress="return isNumber(event)">
</td>
<td>
<select id="CategoryType" name="CategoryType">
<option value="<?php echo $row['CategoryType']; ?>" selected='selected'>Current: <?php echo $row['CategoryType']; ?></option>
<option value="T">Test (T)</option>
<option value="H">Homework (H)</option>
<option value="Other">Other</option>
</select>
</td>
<td>
<input type="text" name="CategoryAssignmentEstimates" value="<?php echo $row['CategoryAssignmentEstimates']; ?>" onkeypress="return isNumber(event)">
</td>
<td>
<?php
$iduser=mysqli_real_escape_string($connect,$rows['idUser']);
echo "<input name='Edit' type='submit' id='Edit' value='Edit'></td>";
?>
</td>
</tr>
<?php
}
?>
</table>
<?php
mysqli_close($con);
?>
<form action="insertCategory.php" method="post">
<br>
New Category Name: <input type="text" name="CategoryTitle"><br>
Weight Percentage: <input type="text" name="CategoryWeight" onkeypress="return isNumber(event)"><br>
Category Type:
<select id="CategoryType" name="CategoryType">
<option value="T">Exam/Test</option>
<option value="H">Homework</option>
<option value="">Other</option>
</select><br>
Estimated Number of Assignments: <input type="text" name="CategoryAssignmentEstimates" onkeypress="return isNumber(event)">
<?php
$ClassID = '1';
$CatClassID = $_POST['$ClassID'];
?>
<script language="javascript">
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
}
</script>
<br>
<input type="submit" name="Add">
</form>
<?php
//If edit was hit
if ($_POST['edit']) {
$CatTitle = $_POST['CategoryTitle'];
$CatWeight= $_POST['CategoryWeight'];
$CatType = $_POST['CategoryType'];
$CatEstNumAssign= $_POST['CategoryAssignmentEstimates'];
$query="UPDATE Category SET CategoryTitle = '$CatTitle', CategoryWeight = '$CatWeight', CategoryType = '$CatType', CategoryAssignmentEstimates = '$CatEstNumAssign' WHERE Class_idClass = 1";
//Below didn't work with period and quotes
//$query="UPDATE Category SET CategoryTitle = '".$CatTitle."', CategoryWeight = '".$CatWeight."', CategoryType = '".$CatType."', CategoryAssignmentEstimates = '".$CatEstNumAssign."' WHERE Class_idClass = 1";
$result = mysqli_query($con,$query);
}
// Insert data
//if ($_POST['Add']) {
// $Insert = mysql_query("INSERT Category SET CategoryTitle = '$CatTitle', CategoryWeight = '$CatWeight', CategoryType = '$CatType', CategoryAssignmentEstimates = '$CatEstNumAssign', Class_idClass = 1")
// or die ('Error Updating Data! <br />' .mysql_error());
// echo 'Update successful';
//}
?>
</body>
</html>
您是否收到任何错误?点击“修改”会发生什么? –
我不会收到错误。什么都没发生。我尝试将类型=按钮的编辑按钮更改为类型=提交,但它没有什么区别。 – Coogie7