我为php实践创建了一个小应用程序。我已经成功创建了登录和插入功能。我在dashboard.php上遇到删除功能错误。
登录page。
用户名:admin 密码:12345
以下是dashboard.php文件的php代码:
<?php
include 'conntect.php';
if (isset($_GET['delete'])){
$del=$_GET['delete'];
$sql="delete from partners where partner_id=".$del;
mysqli_query($conn, $sql);
header('location:dashboard.php');
}
mysqli_close($conn);
?>
<td><a href="dashboard.php?delete='.$row["partner_id"].'" style="color:red;"><i class="far fa-window-close"></i></a></td>
当我单击删除图标时,我想从数据库中删除记录。
答案 0 :(得分:0)
将您的<td>
替换为以下代码:
<td><a href="dashboard.php?delete=<?php echo $row["partner_id"]; ?>" style="color:red;"><i class="far fa-window-close"></i></a></td>
如果进一步使用mysqli
,请不要关闭MySQL连接。
答案 1 :(得分:0)
您需要共享“ connect.php”文件的内容。出现错误的原因是因为“ mysqli_query($ conn,$ sql)”期望参数1是(mysqli $ link)或(mysqli $ object)。
选中此 =>( https://php.net/manual/en/mysqli.query.php )
面向对象的样式 mysqli :: query(字符串$ query [,int $ resultmode = MYSQLI_STORE_RESULT]):混合
程序样式 mysqli_query(mysqli $ link,字符串$ query [,int $ resultmode = MYSQLI_STORE_RESULT]):混合
要进行快速修复,可以将我的这段代码放入connect.php文件中
class Connect {
private $DB_NAME = 'db_name'; //database name
private $DB_HOST = 'localhost'; //database host
private $DB_USER = 'root'; //database user
private $DB_PASS = ''; //database password
private $QUERY_DEBUG = 'on'; //enable debug locally and disable in production
public static $conn;
public static $pdo_conn;
/**
constructor function
**/
public function __construct() {
try{
//initialize a static mysqli connection object or die on error
self::$conn = new mysqli($this->DB_HOST, $this->DB_USER, $this->DB_PASS,
$this->DB_NAME)or
die('unable to select Database, Connection Error'. mysqli_error());
} catch (Exception $e) {
die('unable to select Database, Connection Error'. $e->getMessage());
}
/**
//if you are using pdo, uncomment this block of code
$pdo = "mysql:host=" . $this->DB_HOST . ";dbname=" . $this->DB_NAME;
try{
self::$pdo_conn = new PDO($pdo, $this->DB_USER, $this->DB_PASS);
}catch (Exception $e)
{
die('unable to select Database, Connection Error'. $e->getMessage());
}
**/
}
/**
function to encapsulate your queries
**/
public function safeQuery($query="")
{
if(empty($query)){return false;}
if($this->QUERY_DEBUG == "off")//check query debug mode
{
//$result = mysqli_query(self::$conn,$query) -> procedural way
$result = self::$conn->query($query) or
die("An error occured");
}
else
{
//$result = mysqli_query(self::$conn,$query) -> procedural way
$result = self::$conn->query($query)
or die("query falied:"."<li>ERROR_NO = ".mysqli_errno(self::$conn)."
<li>ERROR= ". mysqli_error(self::$conn).$query);
}
return $result;
}
}
然后,要在代码中的任何位置使用mysqli查询,只需执行此操作
<?php
include 'conntect.php';
$conn = new Connect();
//**Note** $conn exposes a static $conn property(which is a mysqli connection object
if (isset($_GET['delete'])){
$del=$_GET['delete'];
$sql="delete from partners where partner_id=".$del;
$conn->safeQuery($sql); // (Recommended) Object oriented way
//mysqli_query($conn::$conn, $sql); // procedural way
header('location:dashboard.php');
}
//mysqli_close($conn::$conn);
?>
<td><a href="dashboard.php?delete='.$row["partner_id"].'" style="color:red;"><i class="far fa-window-close"></i></a></td>
我希望我能够提供帮助吗?如果是,请接受我的回答。最好的问候
答案 2 :(得分:-1)
$ sql =“从合作伙伴处删除,其中partner_id ='{$ del}'”;