【问题标题】:How to trigger a sql query by clicking a button in PHP?如何通过单击 PHP 中的按钮触发 sql 查询?
【发布时间】:2014-08-27 17:51:27
【问题描述】:

所以我正在尝试建立一个在线商店,基本上不起作用的是在购买点击“购买”按钮时执行查询。查询是:

$sql = mysql_query("INSERT INTO vehicles (model,owner) VALUES ('$vehid','$id')");   

按钮是

<form action=\"\" method=\"post\">   
    <input type=\"submit\" value=\"BUY\">
</form>

整个代码:

<?php
$id = $_SESSION['SESS_MEMBER_ID'];


include ('config2.php');

$result = mysql_query("select * from shop_vehicule ORDER BY id DESC");
$result2 = mysql_query("select * from accounts where id = '$id'");
while($row = mysql_fetch_array($result2))
$credit = $row['credits'];
while($row = mysql_fetch_array($result)){
    $name = $row['nume'];
    $price = $row['pret'];
    $left = $credit - $price; 
    $vehid = $row['vehid'];

    echo "<p><center><b>$name</b> | $price </center>
        <a href=\"#\" class=\"topopup\">More information about $name</a></p>

    <div id=\"toPopup\"> 

        <div class=\"close\"></div>
        <span class=\"ecs_tooltip\">Press Esc to close <span class=\"arrow\"></span></span>
        <div id=\"popup_content\"> <!--your content start-->
            <p>
The $name costs $price, after you'll have $left !</p>

<form action=\"\" method=\"post\">   
    <input type=\"submit\" value=\"BUY\">
</form>

        </div> 

    </div> 

    <div class=\"loader\"></div>
    <div id=\"backgroundPopup\"></div>";
$sql = mysql_query("INSERT INTO vehicles (model,owner) VALUES ('$vehid','$id')");   
}

mysql_close();
?>

【问题讨论】:

  • 您提出了不好的问题,并有可能失去您的提问权限。 You should read this before you post your next one.
  • mysql_query 是一个过时的接口,不应在新应用程序中使用,并将在未来的 PHP 版本中删除。像PDO is not hard to learn 这样的现代替代品。如果您是 PHP 新手,PHP The Right Way 之类的指南可以帮助您解释最佳实践。
  • 您提出的这类问题表明缺乏基础知识。您对 PHP 有哪些参考资料?一本好书或参考网站将对您的学习大有帮助。
  • 完全同意你@tadman。

标签: php jquery mysql sql


【解决方案1】:

这是我的帮助尝试,我没有测试代码,但它应该可以工作。请阅读代码中的 cmets。它解释了它的作用。

$id = $_SESSION['SESS_MEMBER_ID'];

/* To use PDO the following line must be included in your config2.php

    define('DB_HOST', 'localhost');
    define('DB_NAME', 'database');
    define('DB_USER', 'username');
    define('DB_PASS', 'password');
    $db = new PDO('mysql:host='. DB_HOST .';dbname='. DB_NAME, DB_USER, DB_PASS);

    You can either use define or put the info straight into the PDO() function but I like it when it's easy to read and modify if needed.
*/
include ('config2.php');

$query = $db->prepare("SELECT * FROM accounts WHERE id = :id"); //Please use PDO or MySQLi, MySQL is outdated and unsecure. For this example, I am using my favorite method which is PDO.
$query->execute(array(':id' => $id));
$account = $query->fetchObject(); //Since we only need one line, we're going to use fetchObject object.

$query2 = $db->prepare("SELECT * FROM shop_vehicule ORDER BY id DESC");
$query2->execute();
$vehicules = $query2->fetchAll(); //I am using fetchAll due to multiple row will be returned.

foreach ($vehicules as $row) {
    echo '<p><center><b>'.$row['nume'].'</b> | '.$row['pret'].' </center>
        <a href="#" class="topopup">More information about $name</a></p>

    <div id="toPopup">
        <div class="close"></div>
        <span class="ecs_tooltip">Press Esc to close <span class="arrow"></span></span>
        <div id="popup_content"> <!--your content start-->
            <p>The '.$row['nume'].' costs '.$row['pret'].', after you\'ll have '.$account->credit - $row['pret'].' !</p>
            <a href="?purchase='.$row['vehid'],'">BUY</a>
        </div>
    </div> 
    <div class="loader"></div>
    <div id="backgroundPopup"></div>';
}

// Basically what this part does is whenever the user click on the link, purchase will be set and it'll trigger the query to insert into the vehicule table then return a message if it was successful or not.
if ( isset($_GET['purchase']) ) {
    $query = $db->prepare("INSERT INTO vehicles (model,owner) VALUES (':vehid',':id');");
    $query->execute(array(':vehid' => $_GET['purchase'], ':id' => $id));

    if ($query) {
        echo 'Congratulations! You have successfully purchased the vehicule!';
    } else {
        echo 'An error has occured, the purchase was not complete.';
    }
}

【讨论】:

    【解决方案2】:

    在表单标签中使用 action=$_SERVER['PHP_SELF'] 并在 isset($_POST['Buy']) 为 true 的条件下编写 MySQL 插入代码。

    【讨论】:

      【解决方案3】:

      您可以在 php 中执行此操作,但在 2 个不同的文件中。 第一个将有表单,第二个将读取 POST 值并执行查询

      示例(请填写缺失的部分)

      文件 1 。 php

          <form action="file2.php" method="post">   
              <input type="hidden" value=<?php echo $vehid;?>" name="vehid">
              <input type="hidden" value=<?php echo $id;?>" name="id">
              <input type="submit" value="BUY">
          </form>
      

      文件2.php

      $vehid=$_POST['model'];
      $id=$_POST['id'];
      $sql = mysql_query("INSERT INTO vehicles (model,owner) VALUES ('$vehid','$id')"); 
      

      完整教程见http://www.w3schools.com/php/php_mysql_insert.asp

      【讨论】:

      • do not link to w3schools。这有许多危险的过时示例,并使一些有害的不良编程习惯永久存在。它弊大于利。
      猜你喜欢
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      相关资源
      最近更新 更多