想要在不单击按钮的情况下从 HTML 输入文本更新数据库值

我有包含设置点的输入文本框,我在数据库中的表具有默认值但需要如果我更改了文本框中的设置点将更新数据库表中的值,而无需单击按钮!


这是 HTML 代码:


 <input class="tempSet" id="ACSet" type="text"  value="35">

和 PHP 代码,但无法将文本框与 PHP 连接以选择新值:


  <?php

     $DATABASE_HOST = 'localhost';

     $DATABASE_USER = 'user';

     $DATABASE_PASS = 'pass';

     $DATABASE_NAME = 'name';

     // Try and connect using the info above.

     $db = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS,                   

       $DATABASE_NAME);

     if (!$db){


die("Connection Failed: ". mysqli_connect_error());


     }


     $db_update = "UPDATE temp_setpoint SET set_point='??' WHERE id=1";

     $result = mysqli_query($db, $db_update);

      ?>


子衿沉夜
浏览 124回答 1
1回答

明月笑刀无情

我有包含设置点的输入文本框,我在数据库中的表具有默认值但需要如果我更改了文本框中的设置点将更新数据库表中的值,而无需单击按钮!这是 HTML 代码:&nbsp;<input class="tempSet" id="ACSet" type="text"&nbsp; value="35">和 PHP 代码,但无法将文本框与 PHP 连接以选择新值:&nbsp; <?php&nbsp; &nbsp; &nbsp;$DATABASE_HOST = 'localhost';&nbsp; &nbsp; &nbsp;$DATABASE_USER = 'user';&nbsp; &nbsp; &nbsp;$DATABASE_PASS = 'pass';&nbsp; &nbsp; &nbsp;$DATABASE_NAME = 'name';&nbsp; &nbsp; &nbsp;// Try and connect using the info above.&nbsp; &nbsp; &nbsp;$db = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;$DATABASE_NAME);&nbsp; &nbsp; &nbsp;if (!$db){die("Connection Failed: ". mysqli_connect_error());&nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;$db_update = "UPDATE temp_setpoint SET set_point='??' WHERE id=1";&nbsp; &nbsp; &nbsp;$result = mysqli_query($db, $db_update);&nbsp; &nbsp; &nbsp; ?>文件.php<?php$DATABASE_HOST = 'localhost';$DATABASE_USER = 'user';$DATABASE_PASS = 'pass';$DATABASE_NAME = 'name';// Try and connect using the info above.$db&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);if (!$db) {&nbsp; &nbsp; die("Connection Failed: " . mysqli_connect_error());}$data = array();$acset = intval($_POST['acset']) ?? null; // value from form$db_update = "UPDATE temp_setpoint SET set_point= ?&nbsp; WHERE id=1";$stmt&nbsp; &nbsp; &nbsp; = $db->prepare($db_update);$stmt->bind_param("i", $acset);if ($stmt->execute()) {&nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; "status" => 'success',&nbsp; &nbsp; &nbsp; &nbsp; 'message' => "Data updated sucess"&nbsp; &nbsp; );} else {&nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; "status" => "fail",&nbsp; &nbsp; &nbsp; &nbsp; 'message' => "Updated fail, please try again later"&nbsp; &nbsp; );&nbsp; &nbsp; error_log($stmt->error);}echo json_encode($data);?>
打开App,查看更多内容
随时随地看视频慕课网APP