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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘