使用mySQLi Prepared Statement更新

时间:2015-07-09 16:39:30

标签: php mysql mysqli

我编写了下面的代码,当用户按下更新按钮时,它应该更新数据库中的选定条目。当我按下更新按钮时,我只得到一个空白页面。我无法弄清楚我的代码有什么问题。任何帮助,将不胜感激。

MyResponse

1 个答案:

答案 0 :(得分:1)

我想通了,我终于找到了一个有效的例子,虽然我不得不调整一些部分。下面的代码将获取从url传递的id,执行预准备语句以更新所选的id,然后重定向到url位置。唯一缺少的是限制声明,我还没知道如何开展工作。

代码也可以删除,几乎与一些小调整相同。

<?php
     // check if the 'id' variable is set in URL, and check that it is valid
     if (isset($_GET['id']) && is_numeric($_GET['id']))

     // get id value
     $id = $_GET['id'];

    $results = $id;

    $firstname  = $_POST['firstname']; 
    $lastname   = $_POST['lastname'];
    $phonenumber    = $_POST['phonenumber'];    
    $city       = $_POST['city'];
    $state      = $_POST['state'];
    $zipcode    = $_POST['zipcode'];
    $dob        = $_POST['dob'];
    $doi        = $_POST['doi'];
    $adjustername   = $_POST['adjustername'];
    $claimrefnumber = $_POST['claimrefnumber'];
    $providernature = $_POST['providernature'];
    $created    = $_POST['created'];
    $language   = $_POST['language'];
    $client     = $_POST['client'];
    $amountauthorized = $_POST['amountauthorized'];
    $active     = $_POST['active'];
    $invoiceformat  = $_POST['invoiceformat'];

   $connection = new mysqli("localhost", "some directory", "some password", "some user");
   $statement = $connection->prepare("update table set firstname = ?, lastname = ?, phonenumber = ?, city = ?, state = ?, zipcode = ?, dob = ?, doi = ?, adjustername = ?, claimrefnumber = ?, providernature = ?, created = ?,language = ?, client = ?, amountauthorized = ?, active = ?, invoiceformat = ? where id = ?");
   $statement->bind_param("sssssssssssssssssi", $firstname, $lastname, $phonenumber, $city, $state, $zipcode, $dob, $doi, $adjustername, $claimrefnumber, $providernature, $created, $language, $client, $amountauthorized, $active, $invoiceformat, $id);

   $statement->execute();
    if($statement->execute()){
       header("some location");
    }else{
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
    }
    $statement->close();
?>