【问题标题】:I believe there is an error in the query on how i insert info into the database for update我相信关于我如何将信息插入数据库进行更新的查询中存在错误
【发布时间】:2018-09-09 19:08:07
【问题描述】:

程序应该使用表单来更新数据库

我删除了更新表的查询,然后页面出现了,我有一个预设检查器,当我取出查询以更新页面显示,当我添加它时,我被重定向到页面

这是我认为有错误的代码

$sql = "UPDATE books SET ISBN=?,Title=?,PubDate=?,PubID=?,Cost = ?, Retail = ?, Category = ? WHERE books.ISBN='$isbn'";

  if($stmt = $mysqli->prepare($sql)){
        $stmt->bind_param("issiiis", $isbn, $title,$pubdate,$pubid,$cost,$retail,$category);
            
            // Set parameters
            $ISBN = $isbn;
            $Title = $title;
            $PubDate = $pubdate;
            $PubID = $pubid;
            $Cost = $cost;
            $Retail = $retail; 
            $Category = $category;

            
            
            // Attempt to execute the prepared statement
            if($stmt->execute())
            {
                echo "starting query";
                // Records created successfully. Redirect to landing page
                
                  header("location: index.php");
                exit();
            } else
            {
                echo $stmt->error; //"Something went wrong. Please try again later.";
            }
        
            
        }
        // Close statement  
        $stmt->close(); 
    }

另一方面,这是从数据库获取信息的查询

    // Check existence of student_ID parameter before processing further
    if(isset($_GET["ISBN"]) && !empty(trim($_GET["ISBN"])))
    {
        // Get URL parameter
        $id =  trim($_GET["ISBN"]);
               
        // Prepare a select statement
        $sql = "SELECT * FROM books WHERE ISBN = ?";
        
        if($stmt = $mysqli->prepare($sql)){
            // Bind variables to the prepared statement as parameters
            $stmt->bind_param("s", $param_id);
            
            // Set parameters
            $param_id = $isbn;
            
            // Attempt to execute the prepared statement
            if($stmt->execute()){
                $result = $stmt->get_result();
                
                if($result->num_rows == 1){
                    /* Fetch result row as an associative array. Since the result set
                    contains only one row, we don't need to use while loop */
                    $row = $result->fetch_array(MYSQLI_ASSOC);
                    
                    // Retrieve individual field value
                    $ISBN = $row["ISBN"];
                    $Title= $row["Title"];
                    $PubDate = $row["PubDate"];
                    $PubID = $row["PubID"];
                    $Cost = $row["Cost"];
                    $Retail = $row["Retail"];
                    $Category = $row["Category"];
                    
                }
                else
                {
                    // URL doesn't contain valid student_ID. Redirect to error page
                    header("location: error.php");
                    exit();
                }
                
            }
            else
            {
                echo "Oops! Something went wrong. Please try again later.";
            }
        }
        
        // Close statement
        $stmt->close();
        
        // Close connection
        $mysqli->close();
    }  else{
        // URL doesn't contain student_ID parameter. Redirect to error page
        header("location: error.php");
        exit();
} 

我为代码量和我的无知提前道歉,但这非常重要

【问题讨论】:

  • 这篇文章很长。
  • 您需要简化问题。这里的代码太多了。见stackoverflow.com/help/mcve
  • 完成了,我有两个问题我认为已经发布了问题

标签: php mysql sql database wamp


【解决方案1】:

你的类型字符串正确吗?

您传递的是“issiiis”,因此 ISBN 作为整数传递。在 where 子句中,尽管您将其视为字符串,但考虑到破折号和可能的前导零,这似乎更有可能?

【讨论】:

  • 谢谢你我没注意到,你建议我做什么?
  • 我会将 ISBN 存储为字符串,所以坚持使用,只需将您的类型字符串更改为 'sssiiis'
  • 试过了,没成功
猜你喜欢
  • 2020-03-03
  • 2015-10-31
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 2015-11-01
相关资源
最近更新 更多