douju7245 2016-08-16 20:04
浏览 24
已采纳

准备好的SQL语句INSERT

I am currently following W3Schools tutorial with Prepared SQL statements, When I try to insert data into the database it's saying $firstname = "Nathan" and $lastname = "Kent" is an unused local variable? Also it returns no error so it's connecting fine and when I check the database it has no new entries.

 function setData(){
    global $servername;
    global $username;
    global $password;
    global $dbname;

    $conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
     if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
     } else{
         echo "Connection Successful" . "<br>";
   }
    $stmt = $conn->prepare("INSERT INTO test(firstname, surname) VALUES       (?,?)");
    $stmt->bind_param("ss", $firstname, $surname);

    $firstname = "James";
    $lastname = "Williams";
    $stmt->execute();

    echo "New Records Created";

    $stmt->close();
    $conn->close();

}
  • 写回答

2条回答 默认 最新

  • dscizpq790832708 2016-08-16 20:11
    关注

    With MYSQLI you actually need to look for errors rather than assuming they will shout at you.

    function setData(){
        global $servername;
        global $username;
        global $password;
        global $dbname;
    
        $conn = new mysqli($servername, $username, $password, $dbname);
    
        // Check connection
        if ($conn->connect_error) {
             die("Connection failed: " . $conn->connect_error);
        } else{
             echo "Connection Successful" . "<br>";
        }
        $stmt = $conn->prepare("INSERT INTO test(firstname, surname) VALUES (?,?)");
    
        // add error check
        if ( $stmt === false ) {
            echo $conn->error;
            exit;
        }
    
        // Its not necessary to load the variables before the `bind_param`
        // but as you dont actually have these variables yet you do
        $firstname = "James";
        $lastname = "Williams";
    
        $stmt->bind_param("ss", $firstname, $surname);
    
        $status = $stmt->execute();
    
        // add error check
        if ( $status === false ) {
            echo $conn->error;
            exit;
        }
    
    
        echo "New Records Created";
    
        $stmt->close();
        $conn->close();
    
    }
    

    You could clean this up a bit as well by connecting to the database in the mainline code and passing the connection param to the function as well as the data parameters

    function setData($conn, $firstname, $lastname){
    
        $stmt = $conn->prepare("INSERT INTO test(firstname, surname) VALUES (?,?)");
    
        // add error check
        if ( $stmt === false ) {
            echo $conn->error;
            exit;
        }
    
        $stmt->bind_param("ss", $firstname, $surname);
    
        $status = $stmt->execute();
    
        // add error check
        if ( $status === false ) {
            echo $conn->error;
            exit;
        }
    
        return "New Records Created";
    
        $stmt->close();
        $conn->close();
    
    }
    
    
    $servername = '127.0.0.1';
    $username   = 'root';
    $password   = 'veryStRongPassPhrASe';
    $dbname     = 'mydatabase';
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Check connection
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
    } else{
         echo "Connection Successful" . "<br>";
    }
    
    
    $msg = setData($conn, 'Fred', 'Bloggs');
    echo $msg;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度