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

标签 php mysql textarea

我想使用一个提交按钮将三个文本区域值更新到数据库。但这对我不起作用。当我尝试更新单个文本区域时,该值会正确更新到数据库,但是当我添加其他两个文本区域时,它再次不起作用。其他两个文本区域获得空白值。我知道这是一个基本问题。但这让我发疯。有人可以帮助我实现这一目标吗?

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

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>

这是我提交的文件代码。

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

    }
}

最佳答案

试试这个

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

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

关于php - 使用带有 mysql 和 php 的 textarea 更新多行和单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34470885/

相关文章:

php - 编辑表的特定字段

php - 从选定的保管箱创建变量

php - 如果条件正确,如何插入数据

javascript - 如何从textarea ejs文件中获取数据库的数组?

javascript - 使用输入框替换文本区域中的部分字符串(属性)

php - 简单的PHP数据库连接

php - 使用keyby集合方法检索eloquent api资源

mysql - 在数据库中按视频时间保存视频观看统计数据

mysql - 适用于超大型查询的最快 MySQL 引擎

javascript - 使用正则表达式获取标签中的第一个单词