douzi3756 2017-09-01 21:27 采纳率: 100%
浏览 66
已采纳

用于运行SQL命令的HTML按钮

I'm a bit of a noob at php/sql (well all languages in general), I am trying to get an sql command to run on the click of a html button.

The sql command that I am trying to run is UPDATE supplies SET quantity = quantity + 1 WHERE Id=1

This is what I have at the moment:

dp.php

<?php
$servername = "localhost";
$dbname = "tonor";
$username = "root";
$password = "password";

    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
    }
?>

index.php

<!DOCTYPE html>
<html>
    <head>
    <?php 
        require_once 'db.php';

        if(isset($_POST['data'])){
            $sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=1");
            $sth->execute();
        }
    ?>
    <title>Homepage</title>
    <link rel="stylesheet" type="text/css" href="style/main.css">
</head>
<body>
    <h1>testpage</h1>
    <form method="POST" action="index.php">
        <input type="submit" name="data" value="1"/>
    </form>
  • 写回答

2条回答 默认 最新

  • douduan5073 2017-09-01 21:44
    关注

    You should try this out, but you need to read, and practice further.

    PDO manual: http://php.net/manual/en/book.pdo.php

    It's a lot to look at, but just take a look at their examples to get an idea of how it works.

    Read this as well to make it easier: https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

    PHP is a server side language.

    I any case, the following should be enough to get you started.

     <?php
        $servername = "localhost";
        $dbname = "databasename";
        $username = "username";
        $password = "password";
    
        try {
            $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch(PDOException $e) {
            echo "Connection failed: " . $e->getMessage();
        }
    
        return $conn;
    ?>
    
    
    <!DOCTYPE html>
    <html>
        <head>
        <?php 
            require 'db.php';
    
            if(isset($_POST['data'])){
                $sth = $conn->prepare("UPDATE supplies SET quantity = quantity + 1 WHERE Id=1");
                $sth->execute();
            }
        ?>
        <title>Homepage</title>
        <link rel="stylesheet" type="text/css" href="style/main.css">
    </head>
    <body>
        <h1>testpage</h1>
        <form method="POST" action="">
            <input type="submit" name="data" value="1"/>
        </form>
    
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!