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

时间:2015-12-26 11:26:24

标签: php mysql textarea

我想用一个提交按钮将三个textarea值更新到数据库。但这不适合我。当我尝试更新单个textarea时,值正确地更新到数据库,但是当我添加其他两个textareas时,它再次无效。其他两个textareas得到空白值。我知道这是一个基本问题。但这让我感到疯狂。有人请帮助我实现这个目标吗?

这是我的数据库表,其名称是可选的,它有一些默认值:

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

这是我的三个文本区域,它们读取选项表的默认值。输入新文本后,它将根据选项名称插入到选项表中。

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

这是我的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>
&#13;
&#13;
&#13;

这是我的提交文件代码。

if(isset($ _ POST [&#39; submit&#39;])){

    $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' ");
        }

    }
}

2 个答案:

答案 0 :(得分:0)

试试这个

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

isset()检查变量是否包含值(False,0或空字符串),但不是NULL。如果变量存在则返回TRUE,否则返回FALSE。

答案 1 :(得分:0)

您必须对空白字段进行验证。

通过这种方式编写代码: -

$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 ");
 }