-4

I have a mysql script need to run in php, I have try, but not success. Can someone help me out?

I also try using mysqli_multi_query, but it doesn't work either.

<?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>
henrik
  • 43
  • 1
  • 8
  • https://www.php.net/manual/en/function.mysql-query.php , Warning This extension was deprecated in PHP 5.5.0, mysql_query() sends a unique query (multiple queries are not supported) – FatFreddy May 22 '19 at 07:24

3 Answers3

0

Use PDO instead of mysql_query() (See also: Loading .sql files from within PHP)

<?php

if(isset($_POST["action"])&&($_POST["action"]=="versave")) {
    $query_vers = "CREATE TEMPORARY TABLE 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;
            DROP TABLE tmp";

    $db = new PDO('mysql:host=localhost;dbname=test', 'username', 'passwd');
    $st = $db->exec($query_vers);
}
RyanNerd
  • 3,059
  • 1
  • 22
  • 28
0

Use mysqli_query instead of mysql_query. It is deprecated in new versions of php and it used only 1 parameter. We didn't pass connection variable in mysql_query.

mysql_query($connect, $query_vers);

Update this line to mysqli_query

mysqli_query($connect, $query_vers);
Yogendra
  • 1,208
  • 5
  • 18
  • 38
0

If you are using the GET method, us $_GET to get the submit form data.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
session_start();
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


if(isset($_GET["action"])&&($_GET["action"]=="versave")){ 
$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)){
    echo "query exicute";
}
 //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>
Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11
  • sorry, but it doesn't work. I have try to use mysqli_multi_query, but it doesn't work either...I have edit the code. Thanks – henrik May 22 '19 at 10:38
  • it is a bit weird that when first time I use your code is fine, but after that I try again it doesn't generate in mysql =.=? – henrik May 22 '19 at 11:09
  • Hi again! I wonder you could help me again with this https://stackoverflow.com/questions/56341597/how-to-save-multiple-ckeditor-in-php – henrik May 28 '19 at 12:03
  • Hi , I fixed that issue. When you get a chance please have a look at that. – Shivendra Singh May 30 '19 at 01:26