为什么我的任何查询按钮都不能在我的php表单上工作?

时间:2016-04-06 15:28:48

标签: php html mysql

我试图在页面上的表格中布置我的MySQL数据。我没有将用户指向其他表单以执行添加,更新和删除查询,而是选择以按钮的形式在表中进行查询,节省时间和精力,因为可以在此时添加,更新或删除行。那里。但是现在我的页面,表单和表格设置我尝试建立按钮可以设置的查询。在测试之后,没有任何作用,当我点击任何按钮时,字段只会返回到我们更改之前的任何内容。

Admin_Album_Page.php

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();
require_once 'ConnectorCode.php';
?>

<!DOCTYPE HTML>
<html>
<head>
<title> Albums </title>
</head>
<body>

<?php

if(isset($_POST['update'])) {
$UpdateQuery = "UPDATE tbl_Albums SET Album_Name='{$_POST['albumname']}', 
Number_Of_Tracks='{$_POST['numberoftracks']}', Genre='{$_POST['genre']}', 
Artist_id='{$_POST['artistid']}' WHERE Album_id='{$_POST['hidden']}'";
mysqli_query ($conn, $UpdateQuery);
}

if(isset($_POST['delete'])) {
$DeleteQuery = "DELETE FROM tbl_Albums WHERE Album_id='$_POST[hidden]'";
mysqli_query ($conn, $DeleteQuery);

}

if(isset($_POST['add'])) {
$AddQuery = "INSERT INTO tbl_Album (Album_Name, Number_Of_Tracks, Genre,  Artist_id) 

VALUES ('$_POST[uartistname]', '$_POST[unumberoftracks]', 
'$_POST[ugenre]', 
'$_POST[uartist]')";
 mysqli_query ($conn, $AddQuery);

}

?>

<?php

$result = mysqli_query($conn, "SELECT*FROM tbl_Albums");

?> <table border="1">
<tr>
<th>Album ID</th>
<th>Album Name</th>
<th>Number of Tracks</th>
<th>Genre</th>
<th>Artist ID</th>
</tr>
<?php

while($row = mysqli_fetch_array($result)) {
?>
<form method = "POST">
<tr>
<td><input type="number" name="albumid" value="<?php echo $row     ['Album_id']; ?>" /></td>
<td><input type="text" name="albumname" value="<?php echo $row['Album_Name']; ?>" /></td>
<td><input type="number" name="numberoftracks" value="<?php echo $row['Number_Of_Tracks']; ?>" /></td>
<td><input type="text" name="genre" value="<?php echo $row['Genre']; ?>"/></td>
<td><input type="number" name="artistid" value="<?php echo $row['Artist_id']; ?>" /></td>
<td><input type="hidden" name="hidden" value="<?php echo $row['Album_id']; ?>"/></td>    
<td><input type="submit" name="update" value="Update" /></td>
<td><input type="submit" name="delete" value="Delete"/></td>
</tr>
</form>
<?php
}  
?>
<form method="POST">
 <tr>
     <td></td>
     <td><input type="text" name="ualbumname" /></td>    
     <td><input type="text" name="unumberoftracks" /></td>
     <td><input type="text" name="ugenre" /></td>
    <td><input type="text" name="uartistid" /></td>
    <td><input type="submit" name="add" value="Add"/></td>
</tr>
</form>
</table>
<?php

mysqli_close($conn);
?>

<p><a href="Admin.php">Return to main page</a></p>

   

我哪里错了?我尽最大努力调用我声明的行并在查询中使用它们,但我得到的只是一个刷新的页面,没有更改行中的数据

1 个答案:

答案 0 :(得分:2)

如果您的所有查询都完美,那么这将完美运行。

更新阻止

if(isset($_POST['update'])){
  $UpdateQuery = "UPDATE tbl_Albums SET Album_Name='{$_POST['albumname']}', 
  Number_Of_Tracks='{$_POST['numberoftracks']}', Genre='{$_POST['genre']}', 
  Artist_id='{$_POST['artistid']}' WHERE Album_id='{$_POST['hidden']}'";
  mysqli_query($conn, $UpdateQuery);
}

删除阻止

if(isset($_POST['delete'])){
  $DeleteQuery = "DELETE FROM tbl_Albums WHERE Album_id='$_POST[hidden]'";
  mysqli_query($conn, $DeleteQuery);
}

添加阻止

if(isset($_POST['add'])){
    $uartistname = $_POST['uartistname'];
    $unumberoftracks = $_POST['unumberoftracks'];
    $ugenre = $_POST['ugenre'];
    $uartist = $_POST['uartist'];

    $AddQuery = "INSERT INTO tbl_Album (Album_Name, Number_Of_Tracks, Genre,  Artist_id) VALUES('$uartistname', $unumberoftracks, '$ugenre', $uartist)";
   mysqli_query($conn, $AddQuery);
}

动态HTML阻止

$result = mysqli_query($conn, "SELECT*FROM tbl_Albums");?> 
<table border="1">
  <tr>
    <th>Album ID</th>
    <th>Album Name</th>
    <th>Number of Tracks</th>
    <th>Genre</th>
    <th>Artist ID</th>
  </tr>
  <?php
while($row = mysqli_fetch_array($result)) {
?>    
  <tr>
    <form method="POST" action="">
      <td><input type="number" name="albumid" value="<?php echo $row['Album_id']; ?>" /></td>
      <td><input type="text" name="albumname" value="<?php echo $row['Album_Name']; ?>" /></td>
      <td><input type="number" name="numberoftracks" value="<?php echo $row['Number_Of_Tracks']; ?>" /></td>
      <td><input type="text" name="genre" value="<?php echo $row['Genre']; ?>"/></td>
      <td><input type="number" name="artistid" value="<?php echo $row['Artist_id']; ?>" /></td>
      <td><input type="hidden" name="hidden" value="<?php echo $row['Album_id']; ?>"/></td>    
      <td><input type="submit" name="update" value="Update" /></td>
      <td><input type="submit" name="delete" value="Delete"/></td>
    </form>
  </tr>    
  <?php }?>    
  <tr>
    <form method="POST" action="">
      <td></td>
      <td><input type="text" name="ualbumname" /></td>    
      <td><input type="text" name="unumberoftracks" /></td>
      <td><input type="text" name="ugenre" /></td>
      <td><input type="text" name="uartistid" /></td>
      <td><input type="submit" name="add" value="Add"/></td>
    </form>
  </tr>
</table>

这只是该守则的重新代表。如果有任何问题,请告诉我。