SQL Update无法正常工作

时间:2015-11-28 05:40:21

标签: php sql-update

我在下面有一个SQL更新,没有错误,但它没有反映我对数据库所做的任何更改。我也贴在这里2前面的PHP将导致更新PHP。 UPDATE的SQL查询也没有显示错误。

如果我错过了什么,请告诉我。

非常感谢。

表1:nmc_cd

enter image description here

表2:nmc_category

enter image description here

表3:nmc_publisher

enter image description here

这是显示CD列表的php,这里的关键是CDID,一旦选中将被发送到下一个php。

include 'database_conn.php';    //make db connection

    $sql = "SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear, nmc_cd.CDPrice, nmc_category.catDesc, nmc_cd.catID
                FROM nmc_cd 
                LEFT JOIN nmc_category ON nmc_cd.catID=nmc_category.catID"; 

    $queryresult = mysqli_query($conn, $sql)
    or die (mysqli_error($conn));       


        echo'<table cellpadding="0" cellspacing="0" class="db-table" table align="center">';
        echo"<tr><th>Music Title</th><th>Year</th><th>Price</th><th>Category</th></tr>"; 


    while($row = mysqli_fetch_assoc($queryresult)) {
        $iCDID = $row['CDID'];
        $CDTitle = $row['CDTitle'];
                $CDYear = $row['CDYear'];
        $CDPrice = $row['CDPrice'];
                $CDCat = $row['catDesc'];
                $CDCatID = $row['catID'];


                echo "<tr><td>";
                echo "<a href = \"CDSelect.php?itemCode=$iCDID\">$CDTitle</a>";
                echo "</td><td>";  
                echo "<div align='center'>$CDYear</div>\n";
                echo "</td><td>";  
                echo "<div align='center'>$CDPrice </div>\n";
                echo "</td><td>";  
                echo "<div align='center'>$CDCat</div>\n";
                echo "</TD></tr>";  
    }

        echo "</table>"; 

    mysqli_free_result($queryresult); 
    mysqli_close($conn);
?>

在这个php中,它显示所选的CD并允许编辑

 <?php       
 header('Content-type: text/html; charset=iso-8859-1');  //set the charset parameter

 include 'database_conn.php';       //make db connection

  $code = $_GET['itemCode'];        //collect form data from user input

  $sql = "SELECT nmc_cd.CDID, nmc_cd.CDTitle, nmc_cd.CDYear, nmc_cd.CDPrice, nmc_cd.catID, nmc_cd.pubID, nmc_category.catDesc, nmc_publisher.pubID, nmc_publisher.pubName
          FROM nmc_cd 
          JOIN nmc_category on (nmc_cd.catID = nmc_category.catID)
          JOIN nmc_publisher on (nmc_cd.pubID = nmc_publisher.pubID)
          WHERE nmc_cd.CDID = '$code'";                                 //link user input with primary key

  $queryresult = mysqli_query($conn, $sql) 
  or die (mysqli_error($conn));

  $row = mysqli_fetch_assoc($queryresult);      //function fetches result row

  $iCDID = $row['CDID'];
  $CDTitle = $row['CDTitle'];
  $CDYear = $row['CDYear'];
  $CDPrice = $row['CDPrice'];
  $CDCat = $row['catDesc'];
  $CDPub = $row['pubName'];
  $CDpubID = $row['pubID'];

    <form method="get" action="UpdateCD.php">
    <div align="center">  
    <div>Title <input type = "text" name = "CDTitle" value = "<?php echo $CDTitle; ?>" /></div></br>
    <div>Year <input type = "text" name = "CDYear" value = "<?php echo $CDYear; ?>" /></div></br>
    <div>Price <input type = "text" name = "CDPrice" value = "<?php echo $CDPrice; ?>" /></div></br>
    Category
       <select name="CDCat">
           <option value= " ">
            <?php 
            include 'database_conn.php';            //make db connection
             if (! ( is_object($conn ) && ( get_class( $conn ) == 'mysqli' ))) {
                die("DB connection failure.");
             }
             $rsCDCat = mysqli_query($conn, "SELECT nmc_category.catDesc FROM nmc_category");
             if ( !$rsCDCat ) {
                die("No result from DB query."); //probably invalid SQL, table error
             }
             if ( $rsCDCat->num_rows < 1 ) {  
                die("No rows returned from DB query."); //query runs but nothing is found in DB to match
             }
             while($Catcatresult = mysqli_fetch_array($rsCDCat)){
            $optioncat = "<option value='{$Catcatresult[0]}'";
            if($Catcatresult[0] == $CDCat){
            $optioncat .= " selected='selected'";
            }
            $optioncat .= ">{$Catcatresult[0]}</option>";
            echo $optioncat;
             }
             ?>
               </select></br></br>

    Publisher 
        <select name="CDPub">
           <option value= " ">
            <?php 
            include 'database_conn.php';            //make db connection
             if (! ( is_object($conn ) && ( get_class( $conn ) == 'mysqli' ))) {
                die("DB connection failure.");
             }
             $rsCDpub = mysqli_query($conn, "SELECT nmc_publisher.pubName FROM nmc_publisher");
             if ( !$rsCDpub ) {
                die("No result from DB query."); //probably invalid SQL, table error
             }
             if ( $rsCDpub->num_rows < 1 ) {  
                die("No rows returned from DB query."); //query runs but nothing is found in DB to match
             }
             while($Catpubresult = mysqli_fetch_array($rsCDpub)){
            $option = "<option value='{$Catpubresult[0]}'";
            if($Catpubresult[0] == $CDPub){
            $option .= " selected='selected'";
            }
            $option .= ">{$Catpubresult[0]}</option>";
            echo $option;
             }
             ?></select></br></br>
    <div><input type="submit" value="Update"></div>
    </form>

在这个更新php中,它从前一个php获取值并更新表。

<?php

header('Content-type: text/html; charset=iso-8859-1');

include 'database_conn.php';        // make db connection

  $pCDTitle = filter_has_var(INPUT_GET, 'CDTitle') ? $_GET['CDTitle']: null;         // store all parameter in variable
  $pCDPubName = filter_has_var(INPUT_GET, 'CDPub') ? $_GET['CDPub']: null;
  $pCDYear = filter_has_var(INPUT_GET, 'CDYear') ? $_GET['CDYear']: null;
  $pCDCategory = filter_has_var(INPUT_GET, 'CDCat') ? $_GET['CDCat']: null;        
  $pCDPrice = filter_has_var(INPUT_GET, 'CDPrice') ? $_GET['CDPrice']: null;
  $pCDID = filter_has_var(INPUT_GET, 'CDID') ? $_GET['CDID']: null;
  $pCDPubID = filter_has_var(INPUT_GET, 'pubID') ? $_GET['pubID']: null;

  $sql = "UPDATE nmc_cd "
     . "SET nmc_cd.CDTitle='$pCDTitle',nmc_cd.CDYear='$pCDYear',nmc_cd.CDPrice='$pCDPrice',nmc_cd.catID='$pCDCategory',nmc_cd.pubID='$pCDPubName'"
     . "WHERE nmc_cd.CDID='$pCDID'";

  if ($conn->query($sql) === TRUE) {
    echo "<b><font face='verdana' font sise='3' color='red'>Record updated successfully</font></b>";
    $sql = "SELECT * FROM nmc_cd "
     . "JOIN nmc_category ON (nmc_cd.catID = nmc_category.catID)"
     . "JOIN nmc_publisher ON (nmc_cd.pubID = nmc_publisher.pubID)"
     . "WHERE nmc_cd.CDID = '$pCDID'"; //Query Database
$result = mysqli_query($conn, $sql);

$row = mysqli_fetch_assoc($result);
     $CDID = $row['CDID'];
     $CDTitle = $row['CDTitle'];
     $CDYear = $row['CDYear'];
     $CDPrice = $row['CDPrice'];
     $catDesc = $row['CDCat'];
     $pubName = $row['CDPub'];
     $location = $row['location'];

echo "<table>";
echo "<tr><th>Title</th><th>Year</th><th>Price</th><th>Catergory</th><th>Publisher</th><th>Location</th></tr>";
echo "<tr><td>".$row["CDTitle"]."</td><td> ".$row["CDYear"]."</td><td> ".$row["CDPrice"]."</td><td> ".$row["CatCD"]."</td><td> ".$row["CatPub"]."</td><td> ".$row["location"]."</td></tr>"; //Display Data Table Data
echo "</table"; 
mysqli_close($conn);
} else {
    echo "<b><font face='verdana' color='red'>Error updating record!!!</font></b>" . $conn->error;
}
?>

2 个答案:

答案 0 :(得分:0)

查看MySql日志。确保将错误的查询记录到文件中,您应该看到语法错误。你可以做的另一件事是转出$ sql字符串并在MySQL中运行它。看它是否成功运行。

答案 1 :(得分:0)

$sql = "UPDATE nmc_cd "
     . "SET nmc_cd.CDTitle='$pCDTitle',nmc_cd.CDYear='$pCDYear',nmc_cd.CDPrice='$pCDPrice',nmc_cd.catID='$pCDCategory',nmc_cd.pubID='$pCDPubName'"
     . "WHERE nmc_cd.CDID='$pCDID'";

应该是:

$sql = "UPDATE nmc_cd "
     . "SET nmc_cd.CDTitle='$pCDTitle',nmc_cd.CDYear='$pCDYear',nmc_cd.CDPrice='$pCDPrice',nmc_cd.catID='$pCDCategory',nmc_cd.pubID='$pCDPubName' "
     . "WHERE nmc_cd.CDID='$pCDID'";

即。您在WHERE子句之前缺少一个SPACE字符。

(注意第二行的结尾)