PHP:
单击按钮时如何执行 SQL?我会使用 JavaScript onClick()
函数还是其他方式?我正在尝试在循环中执行此操作,并且仅在单击按钮的行上执行 sql ...谢谢!
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!
@PHPnoOb 的代码可提供帮助:好的,所以现在我已经整理好了所有这些,但是当单击按钮时,它会为每一行执行一次……而不仅仅是单击按钮的那一行!我只想查询单击按钮的行...我的代码:
Code for @PHPnoOb to 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:
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.
您可以执行类似的操作来保存输入/提交 btn 中的值.
You can do something like this to save a value from an input/submit 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';
}
这篇关于如何在单击按钮时执行mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!