gpt4 book ai didi

php - 使用 PHP 面向对象编程简单更新 MySQL

转载 作者:行者123 更新时间:2023-11-29 22:24:37 24 4
gpt4 key购买 nike

我正在对 mysql 进行更新,但希望在 OOP 中而不是过程方法中执行此操作。如何使用 OOP 进行更新?我首先测试输入中是否有数据需要更新。检查一下是否为空。然后我获取该结果并将其存储到一个变量中,然后查询数据库以更新该列,然后将图像文件提交到正确的文件夹。下面是我正在使用的代码。我不断收到空白页。我没有收到任何错误消息来查看我哪里出错了。我尝试了一些不同的修改,但没有一个起作用。

    if(!empty($_FILES['logo']) && !empty($_FILES['logoalt'])) { 

// Testing the connection to MySQL either send message or query MySQL for table creation

if($DB->connect_errno){
printf("Could not connect to MySQL Database: %s \n",$DB->connect_error);
exit();
} else{

$DB = new mysqli(SERVER,USER,PASSWORD,DATABASE);

$logo = $_FILES['logo']['name'];
$logoalt = $_POST['logoalt'];

if($DB->query("UPDATE HomePageData SET Logo = '$logo', ImgAlt = 'logoalt'")) {

foreach($_FILES as $file_name => $file_array) {
if(is_uploaded_file($file_array['tmp_name'])) {
move_uploaded_file($file_array['tmp_name'],
"$file_dir/".$file_array['name'])
or die ("Couldn't move file");
echo "<p>Your data was successfully submitted</p>";
} else {
printf("Could not load file: %s \n",$DB->error);
}
}
$DB->close();
}
}
}

最佳答案

引用php.net :

面向对象的风格

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

程序风格

$link = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');

How can I update with OOP?

你已经是了。因此,在实例化/创建新对象时

$DB = new mysqli(SERVER,USER,PASSWORD,DATABASE);

概述了面向对象风格的用法。

I keep getting a blank page. I don't get any error messages to see where I am going wrong.

这与标题无关 - 对于非常简单的解决方案,我建议打开 error displaying

error_reporting(E_ALL); //should be used in development environment
ini_set('display_errors', true); //should be used in development environment

关于php - 使用 PHP 面向对象编程简单更新 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30390345/

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