如何根据 fetch 数组执行 sql 代码
How to execute sql code based on fetch array
我有一个在 google 上找到的搜索引擎,我想在我拥有的 'barcode' 数据库中搜索,如果它找到人,它会执行 $ 下的代码语句(我在 $statement 下添加了我发现的代码中未包含的代码),我对信息的回显不感兴趣,这就是为什么我删除了那部分并进行了刷新,我只会有兴趣执行 sql 中的更改,一旦找到您要找的人,就不必是下面这样的代码,我真的不知道该怎么做,我在这里寻求帮助。
<?php
include 'includes/conn.php';
include 'includes/scripts.php';
if (isset($_POST['no'])) {
$sca=trim($_POST['no'],"");
$credentials="";
$new2 ="SELECT * FROM `barcode`";
$mysqli = new $conn;
$statement= $mysqli->prepare("Insert IGNORE into voters
Select * from barcode where id = id;
DELETE from barcode where id IN(SELECT id from voters)");
$res2=mysqli_query($conn, $new2);
while($row=mysqli_fetch_array($res2)){
if($row['credentials'] == $sca){
$statement->execute();
header("refresh: .5");
}
}
}
$statement->close();
mysqli_close($conn);
?>
不清楚你的插入逻辑是什么以及你到底想插入什么,但让我告诉你我将如何构造这样一个查询(包括一些安全措施,如准备好的语句),希望你可以改变 SQL 语句根据你的需要。我在大多数行上留下了评论来解释
<?php
include 'includes/conn.php';
include 'includes/scripts.php';
if (isset($_POST['no'])) {
$sca=trim($_POST['no'],"");
$credentials="";
$sql = "SELECT * FROM `barcode`";
$mysqli = new $conn;
// Prepare the statement
$stmt = $mysqli->prepare($sql);
// Attempt to execute
if ($stmt->execute()) {
// Save result
$result = $stmt->get_result();
// save the result in an assoc array
$row = $result->fetch_all(MYSQL_ASSOC);
// If there is a returned entry
if (count($row) > 0) {
if ($row['credentials'] === $sca) {
// close the statement so we can re-use
$stmt->close();
// We assume id is what we need
$id = $row['id'];
// Now you have to fix your INSERT statement here. I am not sure what you need to insert but follow the general docs on how to insert https://www.php.net/manual/en/mysqli-stmt.bind-param.php
$stmt = $mysqli->prepare("INSERT IGNORE INTO `voters` (columnName) VALUES (?)");
// Here you need to decide what you are inserting and change the variable
$stmt->bind_param("s", $whatever_variable_you_insert);
// Attempt to execute
if ($stmt->execute()) {
// if successful, proceed with the deletion too... or you can put it outside this execute condition
$stmt->close();
// Prepare the delete statement
$stmt = $mysqli->prepare("DELETE FROM `barcode` WHERE id=?");
// Bind the param
$stmt->bind_para("s", $id);
if ($stmt->exceute()) {
// something else or as you wanted - refresh
header("refresh: .5");
}
}
}
}
}
}
?>
这是存储过程的理想情况。我假设你尝试你的逻辑似乎是:
- 查找条形码(通过 id 和 sca?)
- 如果找到条形码,请插入投票器table
- 从条码中删除条码table
所以像
create procedure `check_barcode` (sca int)
begin
select * into result from `code` where id = sca limit 1;
if not result is null then
insert into `voters` select * from `barcode` where id = sca;
delete from `barcode` where id = sca;
end if;
end
您没有包含 table 设计,这有点猜测。
我找到了我要找的东西!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<?php
include "includes/scripts.php";
?>
</head>
<body>
<div class="container mt-5 pt-5">
<form id="scanform" autocomplete="off" method="POST">
<div class="row">
<div class="col-sm-3">
<h1>Scan:</h1>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" id="no" name="no" class="form-control" required>
</div>
</div>
<div class="col-sm-3">
<button class="btn btn-info" name="sub" type="submit">Buscar</button>
</div>
</div>
</form>
</div>
</body>
</html>
<?php
include 'includes/conn.php';
if (isset($_POST['no'])) {
$sca = trim($_POST['no'],"");
$flag = 0;
$id= "";
$credentials = "";
$password = "";
$firstname = "";
$lastname = "";
$sql = "SELECT * FROM `barcode` WHERE credentials = '" . $sca . "' LIMIT 1";
$result = mysqli_query($conn , $sql);
$barcode = mysqli_fetch_assoc($result);
if ($barcode) {
$mod = "INSERT IGNORE INTO voters
Select * from barcode where id = " . $barcode['id'];
$insert_result = mysqli_query($conn , $mod);
if ($insert_result) {
$del = "DELETE from barcode where id = " . $barcode['id'];
$del_result = mysqli_query($conn , $del);
echo "<div class='alert alert-success d-flex justify-content-center mt-3'>Product has been removed from barcode!</div></div>";
} else {
echo "<div class='alert alert-danger d-flex justify-content-center mt-3'>Something went wrong while deleting from barcode!</div></div>";
}
} else {
echo "<div class='alert alert-danger d-flex justify-content-center mt-3'>Product Not Found</div></div>";
return;
}
}
mysqli_close($conn);
?>
我有一个在 google 上找到的搜索引擎,我想在我拥有的 'barcode' 数据库中搜索,如果它找到人,它会执行 $ 下的代码语句(我在 $statement 下添加了我发现的代码中未包含的代码),我对信息的回显不感兴趣,这就是为什么我删除了那部分并进行了刷新,我只会有兴趣执行 sql 中的更改,一旦找到您要找的人,就不必是下面这样的代码,我真的不知道该怎么做,我在这里寻求帮助。
<?php
include 'includes/conn.php';
include 'includes/scripts.php';
if (isset($_POST['no'])) {
$sca=trim($_POST['no'],"");
$credentials="";
$new2 ="SELECT * FROM `barcode`";
$mysqli = new $conn;
$statement= $mysqli->prepare("Insert IGNORE into voters
Select * from barcode where id = id;
DELETE from barcode where id IN(SELECT id from voters)");
$res2=mysqli_query($conn, $new2);
while($row=mysqli_fetch_array($res2)){
if($row['credentials'] == $sca){
$statement->execute();
header("refresh: .5");
}
}
}
$statement->close();
mysqli_close($conn);
?>
不清楚你的插入逻辑是什么以及你到底想插入什么,但让我告诉你我将如何构造这样一个查询(包括一些安全措施,如准备好的语句),希望你可以改变 SQL 语句根据你的需要。我在大多数行上留下了评论来解释
<?php
include 'includes/conn.php';
include 'includes/scripts.php';
if (isset($_POST['no'])) {
$sca=trim($_POST['no'],"");
$credentials="";
$sql = "SELECT * FROM `barcode`";
$mysqli = new $conn;
// Prepare the statement
$stmt = $mysqli->prepare($sql);
// Attempt to execute
if ($stmt->execute()) {
// Save result
$result = $stmt->get_result();
// save the result in an assoc array
$row = $result->fetch_all(MYSQL_ASSOC);
// If there is a returned entry
if (count($row) > 0) {
if ($row['credentials'] === $sca) {
// close the statement so we can re-use
$stmt->close();
// We assume id is what we need
$id = $row['id'];
// Now you have to fix your INSERT statement here. I am not sure what you need to insert but follow the general docs on how to insert https://www.php.net/manual/en/mysqli-stmt.bind-param.php
$stmt = $mysqli->prepare("INSERT IGNORE INTO `voters` (columnName) VALUES (?)");
// Here you need to decide what you are inserting and change the variable
$stmt->bind_param("s", $whatever_variable_you_insert);
// Attempt to execute
if ($stmt->execute()) {
// if successful, proceed with the deletion too... or you can put it outside this execute condition
$stmt->close();
// Prepare the delete statement
$stmt = $mysqli->prepare("DELETE FROM `barcode` WHERE id=?");
// Bind the param
$stmt->bind_para("s", $id);
if ($stmt->exceute()) {
// something else or as you wanted - refresh
header("refresh: .5");
}
}
}
}
}
}
?>
这是存储过程的理想情况。我假设你尝试你的逻辑似乎是:
- 查找条形码(通过 id 和 sca?)
- 如果找到条形码,请插入投票器table
- 从条码中删除条码table
所以像
create procedure `check_barcode` (sca int)
begin
select * into result from `code` where id = sca limit 1;
if not result is null then
insert into `voters` select * from `barcode` where id = sca;
delete from `barcode` where id = sca;
end if;
end
您没有包含 table 设计,这有点猜测。
我找到了我要找的东西!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<?php
include "includes/scripts.php";
?>
</head>
<body>
<div class="container mt-5 pt-5">
<form id="scanform" autocomplete="off" method="POST">
<div class="row">
<div class="col-sm-3">
<h1>Scan:</h1>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="text" id="no" name="no" class="form-control" required>
</div>
</div>
<div class="col-sm-3">
<button class="btn btn-info" name="sub" type="submit">Buscar</button>
</div>
</div>
</form>
</div>
</body>
</html>
<?php
include 'includes/conn.php';
if (isset($_POST['no'])) {
$sca = trim($_POST['no'],"");
$flag = 0;
$id= "";
$credentials = "";
$password = "";
$firstname = "";
$lastname = "";
$sql = "SELECT * FROM `barcode` WHERE credentials = '" . $sca . "' LIMIT 1";
$result = mysqli_query($conn , $sql);
$barcode = mysqli_fetch_assoc($result);
if ($barcode) {
$mod = "INSERT IGNORE INTO voters
Select * from barcode where id = " . $barcode['id'];
$insert_result = mysqli_query($conn , $mod);
if ($insert_result) {
$del = "DELETE from barcode where id = " . $barcode['id'];
$del_result = mysqli_query($conn , $del);
echo "<div class='alert alert-success d-flex justify-content-center mt-3'>Product has been removed from barcode!</div></div>";
} else {
echo "<div class='alert alert-danger d-flex justify-content-center mt-3'>Something went wrong while deleting from barcode!</div></div>";
}
} else {
echo "<div class='alert alert-danger d-flex justify-content-center mt-3'>Product Not Found</div></div>";
return;
}
}
mysqli_close($conn);
?>