gpt4 book ai didi

php - 我无法更新 MySQL 数据库中表的列

转载 作者:行者123 更新时间:2023-11-29 20:59:31 28 4
gpt4 key购买 nike

我使用此命令创建了一个包含值的选项卡:

 CREATE TABLE `news` (
`id` int(11) NOT NULL auto_increment,
`title` text NOT NULL,
`content` text NOT NULL,
`price` text NOT NULL,
`link` text NOT NULL,
`ppcode` text NOT NULL,
`type` text NOT NULL,
PRIMARY KEY (`id`)
)

当我使用这个 php 代码时,我无法更新列的任何值:

 if (isset($_POST['edit'])){
$delsql = "UPDATE news SET title='$newsubject',content='$newdisc',link='$newlink',
price='$newprice',ppcode='$newppcode' WHERE id = '$id'";
$result = mysql_query($delsql) or die(mysql_error());
echo 'OK';
}

注意:MySQL 版本为 3.5.2.2,PHP 版本为 5.3

最佳答案

使用 mysqli_* 函数或 PDO!!!

//db connection

global $conn;

$servername = "localhost"; //host name

$username = "username"; //username

$password = "password"; //password

$mysql_database = "dbname"; //database name

//mysqli prepared statement

$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");

$stmt = $conn->prepare("UPDATE news SET title=?,content=?,link=?,price=?,ppcode=? WHERE id =?");

$stmt->bind_param('sssdii',$newsubject,$newdisc,$newlink,$newprice,$newppcode,$id);

// i corresponding variable has type integer
// d corresponding variable has type double
// s corresponding variable has type string
// b corresponding variable is a blob and will be sent in packets

$stmt->execute();

$row_count= $stmt->affected_rows;
$stmt->close();
$conn->close();

关于php - 我无法更新 MySQL 数据库中表的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37358808/

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