私はphp/sql(一般的にはすべての言語)でnoobのビットですが、htmlボタンのクリックでSQLコマンドを実行しようとしています。SQLコマンドを実行するHTMLボタン
私が実行しようとしているSQLコマンドは、これは私が現時点で持っているものであるUPDATE supplies SET quantity = quantity + 1 WHERE Id=1
です:
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>
、それは、最初のサーバー・プロセスであるPHPコードとブラウザに結果を送信します。あなたが望むようなものを作る古典的な方法は、POSTを使って2段階のプロセスでPOSTリクエストをPHPで処理する方法を検索することです。 –