如何使用一个按钮从多个数据库删除记录
我四处搜寻,靠近,但没有雪茄。我只能使用一个数据库来完成此工作,尽管我试图用一个按钮从3个数据库中删除所有记录(不删除行结构)。当我尝试使用3个数据库执行此操作时,我会得到“页面不起作用”而没有错误代码。对我缺少的东西有什么想法吗?以下是我的代码,感谢大家的任何帮助。 该按钮在delete1.php上,并传递到delete2.php
更新2:好的,现在两个页面上的此更新,我都会得到零错误,但是它只会在第一个数据库中列出条目,也只会空的。第一个数据库上的条目。 想法吗?
有什么
delete1.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<body text="#FFFFFF" bgcolor="#000000" link="#FFFF00" vlink="#FFFF00" alink="#FFFF33">
<font face="Verdana, Arial, Helvetica, sans-serif">
<meta http-equiv="refresh" content="15" ></font>
<p> <?php
$servername = "localhost";
$username = " ";
$password = " ";
$dbname = "vbauyvmy_randomname";
$servernameA = "localhost";
$usernameA = " ";
$passwordA = " ";
$dbnameA = "vbauyvmy_reqlinksbu";
$servernameB = "localhost";
$usernameB = " ";
$passwordB = " ";
$dbnameB = "vbauyvmy_ppreq_busend";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
$result = $conn->query($sql);
$resultt = mysqli_query($conn, "select COUNT(id) AS count FROM `nametable`");
if(!$result) {
die('Error: ' . mysqli_error($link));
} else {
$num_rows = mysqli_fetch_assoc($resultt);
// echo it
echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
}
$connA = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connA->connect_error) {
die("Connection failed: " . $connA->connect_error);
}
$sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
$result = $connA->query($sql);
$resultt = mysqli_query($connA, "select COUNT(id) AS count FROM `nametable`");
if(!$result) {
die('Error: ' . mysqli_error($link));
} else {
$num_rows = mysqli_fetch_assoc($resultt);
// echo it
echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
}
$connB = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connB->connect_error) {
die("Connection failed: " . $connB->connect_error);
}
$sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
$result = $connB->query($sql);
$resultt = mysqli_query($connB, "select COUNT(id) AS count FROM `nametable`");
if(!$result) {
die('Error: ' . mysqli_error($link));
} else {
$num_rows = mysqli_fetch_assoc($resultt);
// echo it
echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
}
?>
<input type="submit" id="deletebutton" name="deleteall" value="Reset Show">
<br>
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if(!empty($row["name"]) ){
echo " ";
}
if(!empty($row["band2"]) && !empty($row["song2"]) ){
echo " ";
}
if(!empty($row["band3"]) && !empty($row["song3"]) ){
echo " ";
}
}
} else {
echo "0 Records";
}
$connB->close();
?> </p>
<p> </p>
<script>
$(document).ready(function(){
$("#deletebutton").click(function(){
if(confirm("Are you sure you want to delete all record?")){
/* user clicked "OK" */
location.href = "delete2.php";
}
else {
}
});
});
</script>
delete2.php
<?php
$servername = "localhost";
$username = "vbauyvmy_peredyrandom";
$password = "Destined1!2";
$dbname = "vbauyvmy_randomname";
$servernameA = "localhost";
$usernameA = "vbauyvmy_peredylinksbu";
$passwordA = "Destined1!2";
$dbnameA = "vbauyvmy_reqlinksbu";
$servernameB = "localhost";
$usernameB = "vbauyvmy_busendperedy";
$passwordB = "Destined1!2";
$dbnameB = "vbauyvmy_ppreq_busend";
$id = $_GET['id'];
//Connect DB
//Create query based on the ID passed from your table
//query : delete where id = $id
// on success delete : redirect the page to original page using header() method
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$connA = new mysqli($servername, $username, $password, $dbname);
if (!$connA) {
die("Connection A failed: " . mysqli_connect_error());
}
$connB = new mysqli($servername, $username, $password, $dbname);
if (!$connB) {
die("Connection B failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM nametable";
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
} else {
echo "Error deleting record";
}
if (mysqli_query($connA, $sql)) {
mysqli_close($connA);
} else {
echo "Error deleting record";
}
if (mysqli_query($connB, $sql)) {
mysqli_close($connB);
header('Location: delete1.php');
exit;
} else {
echo "Error deleting record";
}
?>
I searched around and got close, but no cigar. I can make this work with only one database, though I am attempting to delete all records (without deleting the row structure) from 3 databases with one button. When I try to do this with 3 databases, I get "Page is not working" and no error codes. Any thoughts on what I'm missing? Below is my codes and thank you all for any assistance.
The button is on delete1.php and passes to delete2.php
UPDATE 2: Okay so now with this update on both pages, I get zero errors however it will only list the entries in the first database, and also will only empty the entries on the first database. Any thoughts?
https://www.peredy1.com/select/delete1.php
Delete1.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<body text="#FFFFFF" bgcolor="#000000" link="#FFFF00" vlink="#FFFF00" alink="#FFFF33">
<font face="Verdana, Arial, Helvetica, sans-serif">
<meta http-equiv="refresh" content="15" ></font>
<p> <?php
$servername = "localhost";
$username = " ";
$password = " ";
$dbname = "vbauyvmy_randomname";
$servernameA = "localhost";
$usernameA = " ";
$passwordA = " ";
$dbnameA = "vbauyvmy_reqlinksbu";
$servernameB = "localhost";
$usernameB = " ";
$passwordB = " ";
$dbnameB = "vbauyvmy_ppreq_busend";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
$result = $conn->query($sql);
$resultt = mysqli_query($conn, "select COUNT(id) AS count FROM `nametable`");
if(!$result) {
die('Error: ' . mysqli_error($link));
} else {
$num_rows = mysqli_fetch_assoc($resultt);
// echo it
echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
}
$connA = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connA->connect_error) {
die("Connection failed: " . $connA->connect_error);
}
$sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
$result = $connA->query($sql);
$resultt = mysqli_query($connA, "select COUNT(id) AS count FROM `nametable`");
if(!$result) {
die('Error: ' . mysqli_error($link));
} else {
$num_rows = mysqli_fetch_assoc($resultt);
// echo it
echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
}
$connB = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($connB->connect_error) {
die("Connection failed: " . $connB->connect_error);
}
$sql = "SELECT id, name, band, song, extra, band2, song2, extra2, band3, song3, extra3 FROM nametable";
$result = $connB->query($sql);
$resultt = mysqli_query($connB, "select COUNT(id) AS count FROM `nametable`");
if(!$result) {
die('Error: ' . mysqli_error($link));
} else {
$num_rows = mysqli_fetch_assoc($resultt);
// echo it
echo "<font color='red'>Total Songs</font>: <font color='yellow'><b>" . $num_rows['count']."</font></b></br></br>";
}
?>
<input type="submit" id="deletebutton" name="deleteall" value="Reset Show">
<br>
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if(!empty($row["name"]) ){
echo " ";
}
if(!empty($row["band2"]) && !empty($row["song2"]) ){
echo " ";
}
if(!empty($row["band3"]) && !empty($row["song3"]) ){
echo " ";
}
}
} else {
echo "0 Records";
}
$connB->close();
?> </p>
<p> </p>
<script>
$(document).ready(function(){
$("#deletebutton").click(function(){
if(confirm("Are you sure you want to delete all record?")){
/* user clicked "OK" */
location.href = "delete2.php";
}
else {
}
});
});
</script>
delete2.php
<?php
$servername = "localhost";
$username = "vbauyvmy_peredyrandom";
$password = "Destined1!2";
$dbname = "vbauyvmy_randomname";
$servernameA = "localhost";
$usernameA = "vbauyvmy_peredylinksbu";
$passwordA = "Destined1!2";
$dbnameA = "vbauyvmy_reqlinksbu";
$servernameB = "localhost";
$usernameB = "vbauyvmy_busendperedy";
$passwordB = "Destined1!2";
$dbnameB = "vbauyvmy_ppreq_busend";
$id = $_GET['id'];
//Connect DB
//Create query based on the ID passed from your table
//query : delete where id = $id
// on success delete : redirect the page to original page using header() method
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$connA = new mysqli($servername, $username, $password, $dbname);
if (!$connA) {
die("Connection A failed: " . mysqli_connect_error());
}
$connB = new mysqli($servername, $username, $password, $dbname);
if (!$connB) {
die("Connection B failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "DELETE FROM nametable";
if (mysqli_query($conn, $sql)) {
mysqli_close($conn);
} else {
echo "Error deleting record";
}
if (mysqli_query($connA, $sql)) {
mysqli_close($connA);
} else {
echo "Error deleting record";
}
if (mysqli_query($connB, $sql)) {
mysqli_close($connB);
header('Location: delete1.php');
exit;
} else {
echo "Error deleting record";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我得到了它。非常感谢大家的帮助。
问题在于上面的所有评论和...
我忘了在两页的连接中编辑A和B,
谢谢大家,我对此学到了很多东西。我很奇怪!
I got it. And HUGE thanks to all of you for the help.
The problem was in all of the comments above AND...
I forgot to edit the A and B in the connections for both pages at
Thank you ALL SOOOOO MUCH I learned a lot on this one. I'm very greatful!
您可以尝试的一件事是添加一些内容以在文件
delete2.php
中删除。$ sql =“ delete from nametable”;
您缺少应删除的数据。在
delete
和中,您必须添加应删除的数据。
如果要删除所有内容,请尝试以下操作:
One thing you can try is to add something to delete in your file
delete2.php
.$sql = "DELETE FROM nametable";
you are missing the data that should be deleted.Between
DELETE
andFROM
you have to add the data that should be deleted.if you want to delete everything try this: