简体   繁体   English

使用带有 mysql 和 php 的 textarea 更新多行也单行

[英]Update multiple rows also single row using textarea with mysql and php

I want to update three textarea values to the database with a single submit button.我想用一个提交按钮将三个 textarea 值更新到数据库。 But It is not working for me.但它对我不起作用。 When I try to update a single textarea then the value is updated to the database correctly, But when I add other two textareas, again it is not working.当我尝试更新单个文本区域时,该值会正确更新到数据库中,但是当我添加其他两个文本区域时,它再次不起作用。 Other two textareas getting blank values.I know it is a basic question.其他两个文本区域获得空白值。我知道这是一个基本问题。 But this drives me insane.但这让我发疯。 Would anybody please help me to achieve this?有人可以帮助我实现这一目标吗?

Here is my database table which name is optional and it has some default values:这是我的数据库表,名称是可选的,它有一些默认值:

http://i.stack.imgur.com/afhXJ.png http://i.stack.imgur.com/afhXJ.png

Here is my three text areas which read the options table default values.这是我的三个文本区域,它们读取选项表默认值。 When enter a new text it will then insert into the options table according to the option name.当输入新文本时,它将根据选项名称插入到选项表中。

http://i.stack.imgur.com/XUFrW.png http://i.stack.imgur.com/XUFrW.png

Here is my HTML code:这是我的 HTML 代码:

 <form name="settings" role="form" method="post" action="bangla_insert_submit.php"> <h5>Insert Bangla Head Here:</h5> <textarea name="bangla_head" style="width: 100%"></textarea> <h5>Insert Chamber Head Here:</h5> <textarea name="chamber_head" style="width: 100%"></textarea> <h5>Insert English Head Here:</h5> <textarea name="english_head" style="width: 100%"></textarea> <input name="submit" type="submit" class="btn btn-default" value="Submit" /> </form>

Here is my submited file code.这是我提交的文件代码。

if (isset($_POST['submit'])) {如果(isset($_POST[‘提交’])){

    $bangla_head  = $_POST['bangla_head'];
    $chamber_head = $_POST['chamber_head'];
    $english_head = $_POST['english_head'];

    $result = mysql_query( "SELECT option_id, option_name, option_value FROM options" );

    while( $row = mysql_fetch_assoc($result)){

        if (isset($_POST['bangla_head']) && $row['option_name'] == 'bangla_head'){

            //If the option is now yes (isset checks returns true if the box is selected) and the option in the db is N then update.
            mysql_query( "UPDATE options SET option_value = '$bangla_head' WHERE  option_id ='20' ");
        }
        if (isset($_POST['chamber_head']) && $row['option_name'] == 'chamber_head'){

            //If the option is now yes (isset checks returns true if the box is selected) and the option in the db is N then update.
            mysql_query( "UPDATE options SET option_value = '$chamber_head' WHERE  option_id ='21' ");
        }
        if (isset($_POST['english_head']) && $row['option_name'] == 'english_head'){

            //If the option is now yes (isset checks returns true if the box is selected) and the option in the db is N then update.
            mysql_query( "UPDATE options SET option_value = '$english_head' WHERE  option_id ='22' ");
        }

    }
}

Try this试试这个

 if (isset($_POST['bangla_head']) && ($_POST['bangla_head']!='') ) 

isset() checks if a variable has a value including ( False, 0, or empty string), but not NULL. isset() 检查变量的值是否包括(False、0 或空字符串),但不包括 NULL。 Returns TRUE if variable exists otherwise returns FALSE.如果变量存在则返回 TRUE,否则返回 FALSE。

You must put validation for blank fields.您必须对空白字段进行验证。

Write your code by this way:-用这种方式编写代码:-

$bangla_head  = $_POST['bangla_head'];
$chamber_head = $_POST['chamber_head'];
$english_head = $_POST['english_head'];    


 $arr = [
    '20'=>$bangla_head,
    '21'=>$chamber_head,
    '22'=>$english_head
 ];

 foeach($arr as $key => $value){
     mysql_query( "UPDATE options SET option_value = '$value' WHERE  option_id =$key ");
 }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM