php – 使用提交按钮更新数据库数据

前端之家收集整理的这篇文章主要介绍了php – 使用提交按钮更新数据库数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想用新数据更新数据库,这样当您将文本放在文本框中然后单击提交按钮时,数据将被发送到具有特定ID的数据库.我要发送的只是亮度,代码如下.当我写这样的东西,然后运行它时,我收到403错误禁止访问.我怎样才能解决这个问题?
<?PHP
   function updater($value,$id){
// Create connection
   $conn = new MysqLi( 'localhost','user_name','','data_base_name' );
// Check connection
   if ($conn->connect_error) {
       die("Connection Failed: " . $conn->connect_error);
   }
   $sql = "UPDATE table_name SET name=$value WHERE id=$id";
   if ($conn->query($sql) === TRUE) {
       echo "Record updated successfully";
   } else {
       echo "Error updating record: " . $conn->error;
   }
//$conn->close();
}
?>

<!DOCTYPE html>
<html>
<header>
</header>
<body>
    <form action="<?PHP updater($_POST['name'],1); ?>" method="post" style="height:50px;width:50px;">
        <input type="text" name="name" /><br><br>
        <input type="submit" /><br/>
    </form>
</body>
</html>
像这样:
<?PHP
function updater($value,$id){
    // Create connection
    $conn = new MysqLi( 'localhost','pass','data_base_name' );
    $value =MysqLi_real_escape_string($conn,$value);
    $id =MysqLi_real_escape_string($conn,$id);
    // Check connection

    if ($conn->connect_error) {
        die("Connection Failed: " . $conn->connect_error);
    }   
    $sql = "UPDATE table_name SET name='{$value}' WHERE id='{$id}'";
    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $conn->close();
}   

if(isset($_POST['name'])){
    updater($_POST['name'],$_POST['id'])
}
?>

<!DOCTYPE html>
<html>
<header>
</header>
<body>
<form action="" method="post" style="height:50px;width:50px;">
    <input type="hidden" name="id" value="1" />           
    <input type="text" name="name" /><br><br>
    <input type="submit" /><br/>
</form>
</body>
</html>
原文链接:https://www.f2er.com/php/136894.html

猜你在找的PHP相关文章