gpt4 book ai didi

php - 使用 mySQLi 准备语句更新

转载 作者:行者123 更新时间:2023-11-29 12:06:54 26 4
gpt4 key购买 nike

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

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

最佳答案

我想通了,我终于找到了一个可行的例子,尽管我必须调整一些部分。下面的代码将获取从 url 传递的 id,执行准备好的语句来更新所选 id,然后重定向到 url 位置。唯一缺少的是一个 Limit 语句,我还没有弄清楚如何使其工作。

代码也可以用于删除,只需进行一些细微的调整即可几乎相同。

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

关于php - 使用 mySQLi 准备语句更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31323491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com