douyong1885 2015-07-09 16:39
浏览 28
已采纳

使用mySQLi Prepared Statement更新

I have written the code below and it is supposed to update a selected entry in the database when the user presses the update button. When I press the update button I just get a blank page. I cannot figure out what is wrong with my code. Any help would be appreciated.

        <?php
    //Open a new connection to the MySQL server
    $mysqli = new mysqli('localhost','some directory','some password','some user ');

    //Output any connection error
    if ($mysqli->connect_error) {
        die('Connection failed : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
    }

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

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


$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'];


$query = ("UPDATE tabele SET firstname, lastname, phonenumber, city, state, zipcode, dob, doi, adjustername, claimrefnumber, providernature, created,language, client, amountauthorized, active, invoiceformat, WHERE id)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$statement = $mysqli->prepare($query);

$statement->bind_param('isssssssssssssssss', $id, $firstname, $lastname, $phonenumber, $city, $state, $zipcode, $dob, $doi, $adjustername, $claimrefnumber, $providernature, $created, $language, $client, $amountauthorized, $active, $invoiceformat);

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

1条回答 默认 最新

  • dsklfsdlkf1232 2015-07-09 20:28
    关注

    I figured it out, I finally found an example that works although I had to tweak some parts. The code below will grab the id passed from the url, do a prepared statement to update the selected id, and then redirect to a url location. The only thing missing is a Limit statement which I haven't figured out how to make work.

    The code will also work to delete will be nearly identical with a few minor tweaks.

    <?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();
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器