简体   繁体   English

将按钮的名称和值存储在 mysql 表行中

[英]Store name and value of a Button in a mysql table row

I've been hanging on this problem for 3 days and just can't solve it.我已经在这个问题上挂了 3 天,但无法解决它。 It seems relatively simple, but unfortunately, I can't do it, because I'm a noob.看起来比较简单,可惜我做不到,因为我是菜鸟。

I would like to use the button's onclick function to get its 2 pieces of information我想使用按钮的 onclick 函数来获取它的 2 条信息

```html
<input id="'.$row['id'].'" type="button" value="Pause" name="'.$row['name'].'" onclick="pushDataToDB()">```
  1. name = "'. $ row [' name '].'" [the name reference from a database table] name = "'.$row [' name'].'" [来自数据库表的名称引用]
  2. and value="Pause"和价值=“暂停”

this two informations i would like to store in some other database table.这两个信息我想存储在其他一些数据库表中。 I have tried to solve this problem in different ways.我试图以不同的方式解决这个问题。

I have tried to store in different ways the event.target.name & event.target.event javascript output in PHP Variables and then use it in the mysql insert line but i failed.我试图以不同的方式在 PHP 变量中存储 event.target.name 和 event.target.event javascript 输出,然后在 mysql 插入行中使用它,但我失败了。

I have tried to post the value to the ajax.php then store the value in a PHP variable and use this as Value to push it to the database, but this also doesn't work我试图将值发布到 ajax.php 然后将该值存储在 PHP 变量中并将其用作值将其推送到数据库,但这也不起作用

index.php 索引.php
    <?php 
    
    include_once ('dbh.php');
    
    
    if(isset($_POST['name'])){
    $sqlAufPause = "INSERT INTO aktuellaufpause (name, pausenart) VALUES ('$name', 'BP')";
        
    }
    
    
    if ($conn->query($sqlAufPause) === TRUE) {
      echo "New record created successfully";
    } else {
      echo "Error: " . $sql . "<br>" . $conn->error;
    }
    
    
    
    ?>
    
script.js: 脚本.js:
 function pushDataToDB() { $.get("ajax.php"); return false; }
ajax.php ajax.php
 <?php include_once ('dbh.php'); if(isset($_POST['name'])){ $sqlAufPause = "INSERT INTO aktuellaufpause (name, pausenart) VALUES ('$name', 'BP')"; } if ($conn->query($sqlAufPause) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } ?>

I would say this is the reason, why the name Value is empty, but i don't know how to fix ist..我会说这就是为什么名称 Value 为空的原因,但我不知道如何修复它。

emptyNameValue空名称值

You can use JQuery + AJAX to perform asynchronous POST您可以使用 JQuery + AJAX 执行异步 POST

<?php
$con = mysqli_connect("localhost", "root", "", "testdb");
$query = mysqli_query($con, "SELECT * FROM mytable WHERE id = 1");
$row = mysqli_fetch_assoc($query);
print_r($row);
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>
    <div class="button2">
        <form id="frm">
            <input id="<?php echo $row['id'] ?>" type="button" value="Pause" name="<?php echo $row['name'] ?>" onclick="pushDataToDB()">
        </Form>
    </div>
</body>
<script type="text/javascript">
    function pushDataToDB() {
        var name = "<?php echo $row['name'] ?>"; 
        var value = "Pause";
        $.ajax({
            url: "ajax.php",
            type: "POST",
            data: {
                "name": name,
                "value": value
            },
            success: function(e) {
                if (e == "1") {
                    alert("Success!");
                } else {
                    alert("error!");
                }
            }
        });
    }
</script>

</html>

ajax.php ajax.php

<?php

$con = mysqli_connect("localhost", "root", "", "testdb");
$name = $_POST['name'];
$value = $_POST['value'];
$query = "INSERT INTO mytable(name, value) values('$name','$value')";

$result = mysqli_query($con, $query);

if ($result) {
    echo "1";
} else {
    echo "Error!";
}

EDITTTT****编辑部****

I changed我变了

var name = "<?php echo $row['id'] ?>"; 

to

var name = "<?php echo $row['name'] ?>"; 

since it is the "name" you want stored in the database因为它是您想要存储在数据库中的“名称”

I solved the problem this way.我是这样解决问题的。 Thank you so much!!!非常感谢!!!

index.php索引.php

 <?php
            $sql = "SELECT * FROM `DB-TABLENAME` ORDER BY `VALUE1`, `VALUE2`";
            $result = mysqli_query($conn, $sql);

            if (mysqli_num_rows($result) > 0) {
                while ($row = mysqli_fetch_assoc($result)){
                                echo "<tr>";
                    echo "<td>";
                    echo '<input style="margin: 0 auto 6px 17px;" type="checkbox" id="scales" name="scales">';
                    echo "</td>";
                    echo "<td>";
                    echo $row['VALUE1'];
                    echo "</td>";
                    echo "<td>";
                    echo $row['VALUE2'];
                    echo "</td>";
                    echo "<td>";
                    echo '<div class="button1">
                    <input id="'.$row['id'].'" type="button" value="BP" name="'.$row['VALUE2'].'" onclick="pushDataToDB()">
                    </div>';    
                    echo "</td>";
                    echo "<td>";
                    echo '<div class="button2">
                     <form id="frm">
                    <input id="'.$row['id'].'" type="button" value="Mittagspause" name="'.$row['name'].'" onclick="pushDataToDB()">
                    </form>
                    </div>';    
                    echo "</td>";
                    echo "</tr>";
                    }
            } else {
                echo "there are no comments";
                

            }?>

<script type="text/javascript">
    function pushDataToDB() {
        var name = event.target.name; 
        var value = event.target.value;
        $.ajax({
            url: "ajax.php",
            type: "POST",
            data: {
                "name": name,
                "value": value
            },
             success: function(e) {
                 if (e == "1") {
                     alert("Success!");
                 } else {
                     alert("error!");
                 }
             }
        });
    }
</script>

ajax.php ajax.php

<?php 

include_once('dbconnectioncredentials.php');

$con = mysqli_connect($servername, $username, $password, $dbname);

$name = $_POST['VALUE1'];
$value = $_POST['VALUE2'];
$query = "INSERT INTO aktuellaufpause (VALUE1, VALUE2) VALUES ('$name','$value')";

$result = mysqli_query($con, $query);

 if ($result) {
     echo "1";
 } else {
    echo "Error!";
}

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 mysql 从表单提交时存储图像名称/值(即与单选按钮相同) - How to store image name/value on submit in mysql from a form (i.e same as radio button) 如何在 mysql 数据库表中自动保存不同 class 和名称属性的多个单选按钮值 - how to autosave the multiple radio-button value of different class and name attribute in mysql database table 表的复制按钮,其中行作为ID和值 - Copy button for table with row as id and value MySQL使用Ajax返回表名而不是值 - MySQL returns table name instead of value with Ajax 如何分离从表行中获取的值并将其存储在雪花中的数组中 - How to separate a value taken from a table row and store it in an array in a snowflake 将垂直数组值水平存储到mysql表中的不同列 - vertical array value store into mysql table different columns horizontally 根据行名和单元格值突出显示相应的表格单元格 - Highlight corresponding table <TD> cell based on row name and cell value 如何从javascript中的表中获取列名和第一行值? - How to get column name and first row value from table in javascript? 通过传递特定行的列名来查找 html 表格单元格的值 - Find the value of html table cell by passing column name for a particular row 基于同一表行中另一个值的单选按钮选择状态 - State of Radio Button selection based on another value in same table row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM