写入 MYSQL 的按钮
Button writing to MYSQL
我有一个 html 简单按钮,我试图连接到 MYSQL 数据库,这样当用户按下它时,一个 1 将被写入 table。该站点是使用 elementor 的 wordpress,将使用 elementor 中的短代码小部件调用代码。我打算使用 php 代码片段插件,它允许您输入 php 然后生成一个短代码,可以调用它来执行代码。
到目前为止,按钮代码如下所示:
<button style="background-color: red;" type="button">
Shutdown
</button>
MYSQLtable也很简单,只有两列,其中一列只是时间戳的函数。我只想将 1 写入整数类型的名为“command”的列。
数据库信息如下
主持人:'localhost',
用户:'myuser',
密码:'mypassword',
数据库:'motordata'
table: 'MotorON'
我怎样才能做到这一点?看来我可以使用 JavaScript 或 PHP。对于使用 node.js 的数据库插入,我使用的是下面的代码,但它对这个应用程序可行吗?我看到 PHP post 函数也可能是一条路线。
var con = mysql.createConnection({
host : 'localhost',
user : 'myuser',
password : 'mypassword',
database : 'mydb'
//database connection
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = 'INSERT INTO beta (temperature, pressure) VALUES ('+temp+', '+press+')';
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
这是一个非常简单的 PHP 脚本,可以完成您正在寻找的内容:
<?php
//if the submit button is clicked, follow logic to insert record
if (isset($_POST['submit'])) {
$insert_value = 1;
//make a database connection
$conn = mysqli_connect('localhost', 'myuser', 'mypassword', 'motordata');
//prepared statement to insert the record
$stmt_insert_record = $conn->prepare("INSERT INTO MotorOn (command) VALUES (?)");
$stmt_insert_record->bind_param("i", $insert_value);
$stmt_insert_record->execute();
$stmt_insert_record->close();
}
?>
<!-- Form that will submit to the document's address ie. the same page-->
<form method="post">
<input type="submit" value="Shutdown" style="background-color: red;">
</form>
@Andrew 的第一个回答让我朝着正确的方向前进;但是,它在当前形式下不起作用。应该注意的是,在我进行代码修改的同时,我继续向我的数据库 table.
添加了一个名为“extra”的额外列
HTML 按钮代码:
<form method="post">
<input name="submit123" type="submit" value="Shutdown" style="background-color: red;">
</form>
PHP代码:
<?php
//if the submit button is clicked, follow logic to insert record
if (isset($_POST['submit123'])) {
echo "button pressed";
//make a database connection
$conn = new mysqli('localhost', 'myuser', 'mypass', 'mydb');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// set parameters and execute
$command = 1;
$extra = 1;
$stmt = $conn->prepare("INSERT INTO motoron2 (command, extra) VALUES (?,?)");
$stmt->bind_param("ii", $command, $extra);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
}
?>
因为我使用的是 wordpress 网站和代码片段插件,它将 php 代码放入主题的 function.php 中,所以我必须将 php 代码添加到代码中以下。然后我将在我的网页中使用简码 [phpshutdownkey]
引用它
function shutdownkey( $atts ) {
//if the submit button is clicked, follow logic to insert record
//insert php code from above here
}
add_shortcode('phpshutdownkey', 'shutdownkey');
我有一个 html 简单按钮,我试图连接到 MYSQL 数据库,这样当用户按下它时,一个 1 将被写入 table。该站点是使用 elementor 的 wordpress,将使用 elementor 中的短代码小部件调用代码。我打算使用 php 代码片段插件,它允许您输入 php 然后生成一个短代码,可以调用它来执行代码。
到目前为止,按钮代码如下所示:
<button style="background-color: red;" type="button">
Shutdown
</button>
MYSQLtable也很简单,只有两列,其中一列只是时间戳的函数。我只想将 1 写入整数类型的名为“command”的列。
数据库信息如下 主持人:'localhost', 用户:'myuser', 密码:'mypassword', 数据库:'motordata' table: 'MotorON'
我怎样才能做到这一点?看来我可以使用 JavaScript 或 PHP。对于使用 node.js 的数据库插入,我使用的是下面的代码,但它对这个应用程序可行吗?我看到 PHP post 函数也可能是一条路线。
var con = mysql.createConnection({
host : 'localhost',
user : 'myuser',
password : 'mypassword',
database : 'mydb'
//database connection
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
var sql = 'INSERT INTO beta (temperature, pressure) VALUES ('+temp+', '+press+')';
con.query(sql, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
这是一个非常简单的 PHP 脚本,可以完成您正在寻找的内容:
<?php
//if the submit button is clicked, follow logic to insert record
if (isset($_POST['submit'])) {
$insert_value = 1;
//make a database connection
$conn = mysqli_connect('localhost', 'myuser', 'mypassword', 'motordata');
//prepared statement to insert the record
$stmt_insert_record = $conn->prepare("INSERT INTO MotorOn (command) VALUES (?)");
$stmt_insert_record->bind_param("i", $insert_value);
$stmt_insert_record->execute();
$stmt_insert_record->close();
}
?>
<!-- Form that will submit to the document's address ie. the same page-->
<form method="post">
<input type="submit" value="Shutdown" style="background-color: red;">
</form>
@Andrew 的第一个回答让我朝着正确的方向前进;但是,它在当前形式下不起作用。应该注意的是,在我进行代码修改的同时,我继续向我的数据库 table.
添加了一个名为“extra”的额外列HTML 按钮代码:
<form method="post">
<input name="submit123" type="submit" value="Shutdown" style="background-color: red;">
</form>
PHP代码:
<?php
//if the submit button is clicked, follow logic to insert record
if (isset($_POST['submit123'])) {
echo "button pressed";
//make a database connection
$conn = new mysqli('localhost', 'myuser', 'mypass', 'mydb');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// set parameters and execute
$command = 1;
$extra = 1;
$stmt = $conn->prepare("INSERT INTO motoron2 (command, extra) VALUES (?,?)");
$stmt->bind_param("ii", $command, $extra);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
}
?>
因为我使用的是 wordpress 网站和代码片段插件,它将 php 代码放入主题的 function.php 中,所以我必须将 php 代码添加到代码中以下。然后我将在我的网页中使用简码 [phpshutdownkey]
引用它function shutdownkey( $atts ) {
//if the submit button is clicked, follow logic to insert record
//insert php code from above here
}
add_shortcode('phpshutdownkey', 'shutdownkey');