使用文本框和编辑按钮更新MySQL表(使用HTML和PHP)

时间:2014-05-16 04:10:57

标签: php html mysql

我正在处理一个向Category表添加类别的页面。我有插入(添加新类别)功能,但我的更新功能没有更新值。我已经看到了其他与我合作过的类似的问题,但它似乎没有解决我的问题,除了更新功能之外我无法识别的问题不起作用。

我怀疑我的更新查询,但我认为这是正确的。所以我很难过并且正在寻找另一个人,看看他们是否有所收获。

<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>

1 个答案:

答案 0 :(得分:0)

您的Edit提交按钮和输入不在表单中。当您点击Edit时没有任何反应,因为该按钮不是表单的一部分,因此它不会提交任何内容。尝试使用

包装所有编辑选项
<form action="" method="post">

    <input type="text" ... >
    <input type="text" ... >

    <select> ... </select> 

    ....

    <input type="submit" ... >

</form>

请注意,action=""会将表单提交到当前页面。