如何在单击“ HTML”按钮时执行存储过程?

我需要执行数据库上的存储过程,才能单击Html按钮!


存储过程的名称: DeleteRow


    <form action="">

    <input type="button" value="Check" onclick="">

    </form>

我如何使用php执行?是否可以?


** SQL查询:**


CALL `DeleteRow`();


Helenr
浏览 258回答 2
2回答

梦里花落0921

您可以尝试如下操作:HTML:<input type='button' value='Call Procedure' id='BtnId'>jQuery的:$(document).ready(function(){&nbsp; &nbsp; $("#BtnId").on( 'click', function(){&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type : "POST",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url : "callsp.php",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success : function(text){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert(text);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; });&nbsp;});PHP: (callsp.php)<?php// connect to database$connection = mysqli_connect("hostname", "username", "password", "db", "port");// run the query$result = mysqli_query($connection, "CALL DeleteRow") or die("Query Failed: " . mysqli_error());// loop the result setwhile( $row = mysqli_fetch_array($result) ) {&nbsp; print_r( $row );}?>

郎朗坤

假设您的存储过程类似DELIMITER $$CREATE PROCEDURE getDetails()BEGIN&nbsp; &nbsp;SELECT * FROM tablename;END$$现在,您可以通过以下方式执行此操作 PHPtry {&nbsp; $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);&nbsp; // execute the stored procedure&nbsp; $sql = 'CALL getDetails()';&nbsp; // call the stored procedure&nbsp; $q = $pdo->query($sql);&nbsp; $q->setFetchMode(PDO::FETCH_ASSOC);} catch (PDOException $e) {&nbsp; die("Error occurred:" . $e->getMessage());}while ($r = $q->fetch()){&nbsp; /*&nbsp; * Do something&nbsp; */}单击时,您可以实施Ajax以使其正常运行。您可以按照PHP Ajax示例
打开App,查看更多内容
随时随地看视频慕课网APP