如何在单击按钮时执行mysql

How to execute mysql on button click(如何在单击按钮时执行mysql)
本文介绍了如何在单击按钮时执行mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)