php 如何在按钮单击时执行 mysql
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16660099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute mysql on button click
提问by pattyd
PHP:
How would I execute SQL when a button is clicked?
Would I do it with the JavaScript onClick()
function, or some other way?
I am trying to do this inside of a loop, and only execute the sql on the row that the button is clicked on...
Thanks!
PHP:
单击按钮时如何执行 SQL?我会用 JavaScriptonClick()
函数还是其他方式来做?我正在尝试在循环内执行此操作,并且仅在单击按钮的行上执行 sql ......谢谢!
Code for @PHPnoObto help: Okay, so now I have all of that sorted out, but when the button is clicked it executes once for each row... not just once for the row the button was clicked on! I only want the row that the button was clicked on to be queried... my code:
@PHPnoOb的帮助代码:好的,现在我已经整理好了所有这些,但是当单击按钮时,它会为每一行执行一次......不仅仅是单击按钮的行执行一次!我只想查询单击按钮的行...我的代码:
while($row = mysqli_fetch_array($result)) {
//FAULTY CODE!!! EXECUTES ONCE FOR EACH ROW!
//I WANT IT TO ONLY EXECUTE FOR THE ROW THE
//BUTTON WAS CLICKED ON
if(isset($_POST['delete'])){
echo "You clicked on: ".$row['subject'];
//eventually i will have sql query up here
}
//echo all the results into a table... messy
echo"
<tr><td><div align='center'><font color='grey'>".$row['date']."</font></div></td><td><div align='center'> ".$row['sender']."</div></td><td><div align='center'> ".$row['subject']."</div></td><td><div align='center'><input type='button' value='open' onClick='window.alert(\"On ".$row['date']." ".$row['sender']." wrote:\n".$row['message']."\")'/></div></td><td><div align='center'><form method='post'><input type='submit' value='delete' name='delete'/></form></div></td></tr>
";
}
echo '</table>'; //ends the table.
回答by samayo
Update: 2017
Update: 2017
This answer is outdated, please use better library like PDOto accomplish the below feature.
此答案已过时,请使用更好的库(如PDO)来完成以下功能。
Ok, just copy/paste the whole code in a plain PHP text. It works, I tried it just now.
All you need is a table called test
, with fileds id
,username
, or you can costumize the script, however you like. and don't forget to change databse password, username details..
好的,只需将整个代码复制/粘贴到纯 PHP 文本中即可。它有效,我刚刚试过了。您所需要的只是一个名为test
、带有文件id
、的表username
,或者您可以随意修改脚本。并且不要忘记更改数据库密码,用户名详细信息..
<form action='' method='POST'>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('test');
$query = "SELECT * FROM users";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo " To delete user <b>" . $row['username'] . "</b> Click on the number <input type='submit' name='delete' value='" . $row['id'] . "' /><br/>";
}
if (isset($_POST['delete'])) {
$user = $_POST['delete'];
$delet_query = mysql_query("DELETE FROM users WHERE id = $user ") or die(mysql_error());
if ($delet_query) {
echo 'user with id ' . $user . ' is removed from your table, to refresh your page, click' . '<a href=' . $_SERVER["PHP_SELF"] . ' > here </a>';
}
}
?>
</form>
回答by samayo
You can do something like this to save a value from an input/submit btn.
您可以执行这样的操作来保存输入/提交 btn 中的值。
<!-- your html form -->
<form action="POST">
<input type='text' name='username' />
<input type='text' value='submit' />
</form>
<?php
// your php code
if($_POST && isset($_POST['username'])){
$db = new \PDO('......'); // enter your db details
$stmt = $db->prepare("INSERT INTO table (username) VALUES (?)");
$result = $stmt->execute(array($_POST['username']);
echo $result->rowCount() ? 'Username saved in db' : 'Unknown error occured';
}
回答by hd1
Use an AJAX libraryto issue a request to a server-side script that executes your query. Put this as your onClick() handler in Javascript, and presto, Bob's your uncle!
使用AJAX 库向执行查询的服务器端脚本发出请求。把它作为你的 onClick() 处理程序在 Javascript 中,并且很快,鲍勃是你的叔叔!