简体   繁体   English

如何使用提交按钮更新mysql数据库

[英]How to update mysql database using a submit button

How do you update mysql database when i click on the update function. 当我单击更新功能时,如何更新mysql数据库。 The fields that i want to update are in the textfield that i have created. 我要更新的字段在我创建的文本字段中。

<script>

     function yourfunction() 
     {
         document.getElementById("name").readOnly = true;
         document.getElementById("number").readOnly = true;
         document.getElementById("status").readOnly = true;
         document.getElementById("expertise").readOnly = true;
         document.getElementById("remark").readOnly = true;

     }

    window.onload = yourfunction;

    function myfunction()
    {
        document.getElementById("name").readOnly = false;
        document.getElementById("number").readOnly = false;
        document.getElementById("status").readOnly = false;
        document.getElementById("expertise").readOnly = false;
        document.getElementById("remark").readOnly = false;
        document.getElementById("edit").style.visibility='hidden';
    }

So at first i am able to click on edit button so that my textfields can be edited. 因此,起初我可以单击“编辑”按钮,以便可以编辑我的文本字段。 once i edited them, i can click on the update button to update those fields in mysql database. 一旦我编辑了它们,我可以单击更新按钮来更新mysql数据库中的那些字段。

    <?php
    $name = $_GET['name'];

    $connect = mysqli_connect("localhost", "root", "","test1");
    $output='';
    $sql = "SELECT Name, Number, Expertise, Status, Remarks FROM particulars WHERE Name ='". $name."'";

    $result = mysqli_query($connect, $sql);

    while($row= mysqli_fetch_array($result))
{
    $name = $row['Name'];
    $number =$row['Number'];
    $Expertise =$row['Expertise'];
    $status =$row['Status'];
    $remarks =$row['Remarks'];
} 

    echo "<label>Name</label> <input id = 'name' type='text' value='" .$name. "'/> <br>";
    echo "<label>Number</label><input id = 'number' type='text' value='" .$number. "'/><br>";
    echo "<label>Expertise</label><input id = 'expertise' type='text' value='" .$Expertise. "'/><br>";
    echo "<label>Status</label><input id = 'status' type='text' value='" .$status. "'/><br>";
    echo "<label>Remarks</label><input id = 'remark' type='text' value='" .$remarks. "'/><br>";
    ?>

<br><br><br>
<input type="button" id="edit" value = "Edit" onclick="myfunction()">
<input type="submit" id="update" value = "Update">

I would not mind using either a submit button or using ajax to solve this problem. 我不介意使用提交按钮或使用Ajax解决此问题。 Thank you very much!:) 非常感谢你!:)

First a note on your while loop, that passes through all all rows. 首先在您的while循环中记录所有行。 In your case it should be just one. 在您的情况下,它应该只是一个。 This works since there is one row only but you should use an approach that shouldn't use a while loop. 这行得通,因为只有一行,但是您应该使用不应使用while循环的方法。

Back to your quest. 返回您的任务。 You need a form (simplest way) 您需要一个表格(最简单的方法)

echo "<form action='mypage.php' method='POST'>"; // Start Form (Forms use names not ids btw)
echo "<label>Name</label> <input name = 'name' type='text' value='" .$name. "'/> <br>";
echo "<label>Number</label><input name = 'number' type='text' value='" .$number. "'/><br>";
echo "<label>Expertise</label><input name = 'expertise' type='text' value='" .$Expertise. "'/><br>";
echo "<label>Status</label><input name = 'status' type='text' value='" .$status. "'/><br>";
echo "<label>Remarks</label><input name = 'remark' type='text' value='" .$remarks. "'/><br>";
echo "<input type='submit' id='update' value = 'Update'>"; // This will send the data
echo "</form>"; // End form

mypage.php mypage.php

$sql = "Update particulars  set Number = " .$_POST['number'] etc....

It is extremely ineffecient to have Name as a primary key. 将Name作为主键是极其无效的。 Numbers are alot easier to process not to mention multiple people can have same names. 数字很​​容易处理,更不用说多个人可以拥有相同的名字。

Have Id as primary key and set it to auto_increment. 将ID作为主键,并将其设置为auto_increment。

you can add it to your form like this 您可以像这样将其添加到表单中

<input name = 'id' type='hidden' value='" .$id. "'/>

尝试这个:

UPDATE particulars SET Name = $name, Number = $number, Expertise = $expertise, Status = $status, Remarks = $remarks WHERE Name = $name;

暂无
暂无

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

相关问题 MYSQL-PHP-使用提交按钮传递的变量更新数据库中的值 - MYSQL - PHP - Update value in database by using variable passed from submit button 如何使用onclick甚至CHECKBOX更新数据而无需在PHP和MySQL中提交按钮 - How to update data using onclick even CHECKBOX without button submit in php and mysql 如何使用 angular 中的相同提交按钮更新和提交反应式表单? - How to update and submit a reactive form using the same submit button in angular? 选择多个复选框并使用提交按钮更新数据库 - Select multiple checkboxes and update the database using submit button 如何从一个提交按钮使用nodejs将javascript变量推入mysql数据库? - How to push javascript variables into a mysql database with nodejs from a submit button? 如何在没有提交按钮的情况下提交“文件”输入,然后在同一脚本中执行mysql更新 - How do I submit a “file” input without submit button and then do a mysql update in the same script 如何使用按钮的onclick事件更新MySQL数据库? - How do I update MySQL database using a button's onclick event? 如何使用 JS eventListener 更新 Firestore 数据库以提交表单 - How to update Firestore Database using JS eventListener to submit form 如何使用 xampp 将单选按钮值提交到数据库 - how to submit radio button values to database using xampp 在表单提交之前将使用AJAX更新数据库,从而允许我将新编辑的数据插入到新的mysql表中 - Will using AJAX to update database before form submit allowing me to insert newly edited data into new mysql table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM