如何在 php 中运行 mysql 脚本?

我有一个 mysql 脚本需要在 php 中运行,我尝试过,但没有成功。有人可以帮我吗?


我也尝试使用 mysqli_multi_query,但它也不起作用。


<?php 

include 'connection.php';

if(isset($_GET["action"])&&($_GET["action"]=="versave")){ 

$gp_name = $_GET['gp_name'];

$version = $_GET['version'];

$query_vers = "CREATE TEMPORARY TABLE tmp SELECT * from `gp_info` WHERE `gp_name`= $gp_name";

$query_vers .= "UPDATE tmp SET `version` = $version";

$query_vers .= "ALTER TABLE tmp DROP id";

$query_vers .= "INSERT INTO `gp_info` SELECT 0, tmp.* FROM tmp";

$query_vers .= "DROP TABLE tmp";


mysqli_multi_query($connect, $query_vers);

header("Location: gp_data.php");

}

?>


<form id="verForm" name="versaveForm" method="get">

<input type="hidden" name="action" value="versave">

<div>

<input type="text" id="gp_name" name="gp_name">

<input type="text" id="version" name="version">

</div>

<input type="submit" name="vers" value="Version" id="versave">

</form>


牧羊人nacy
浏览 188回答 3
3回答

翻阅古今

如果您使用的是 GET 方法,请使用 $_GET 来获取提交表单数据。<?php$servername = "localhost";$username = "root";$password = "";$dbname = "test";session_start();// Create connection$conn = new mysqli($servername, $username, $password, $dbname);// Check connectionif ($conn->connect_error) {&nbsp; &nbsp; die("Connection failed: " . $conn->connect_error);}&nbsp;if(isset($_GET["action"])&&($_GET["action"]=="versave")){&nbsp;$gp_name = $_GET['gp_name'];$version = $_GET['version'];$query_vers = "CREATE TEMPORARY TABLE IF NOT EXISTS tmp SELECT * from `gp_info` WHERE `gp_name`= '$gp_name';UPDATE tmp SET `version` = '$version';ALTER TABLE tmp DROP id;INSERT INTO `gp_info` SELECT 0, tmp.* FROM tmp";if(mysqli_multi_query($conn, $query_vers)){&nbsp; &nbsp; echo "query exicute";}&nbsp;//header("Location: gp_data.php");}?><form id="verForm" name="versaveForm" method="get">&nbsp; &nbsp; <input type="hidden" name="action" value="versave">&nbsp; &nbsp; <div>&nbsp; &nbsp; <input type="text" id="gp_name" name="gp_name">&nbsp; &nbsp; <input type="text" id="version" name="version">&nbsp; &nbsp; </div>&nbsp; &nbsp; <input type="submit" name="vers" value="Version" id="versave"></form>

守着星空守着你

使用mysqli_query代替mysql_query。它在新版本中已被弃用,php并且仅使用了 1 个参数。我们没有在mysql_query.mysql_query($connect,&nbsp;$query_vers);将此行更新为&nbsp;mysqli_querymysqli_query($connect,&nbsp;$query_vers);

慕田峪7331174

使用 PDO 而不是mysql_query()<?phpif(isset($_POST["action"])&&($_POST["action"]=="versave")) {&nbsp; &nbsp; $query_vers = "CREATE TEMPORARY TABLE tmp SELECT * from `gp_info` WHERE `gp_name`= gp_name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UPDATE tmp SET `version` = version;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ALTER TABLE tmp DROP id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; INSERT INTO `gp_info` SELECT 0, tmp.* FROM tmp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DROP TABLE tmp";&nbsp; &nbsp; $db = new PDO('mysql:host=localhost;dbname=test', 'username', 'passwd');&nbsp; &nbsp; $st = $db->exec($query_vers);}
打开App,查看更多内容
随时随地看视频慕课网APP