简体   繁体   English

使用mySQLi预准备语句进行更新

[英]Update with 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();
?>

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. 下面的代码将获取从url传递来的id,执行一条准备好的语句来更新所选的id,然后重定向到url位置。 The only thing missing is a Limit statement which I haven't figured out how to make work. 唯一缺少的是Limit语句,我还没有弄清楚如何进行工作。

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();
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM