【问题标题】:Update multiple rows also single row using textarea with mysql and php使用带有mysql和php的textarea更新多行也单行
【发布时间】:2015-12-26 11:26:24
【问题描述】:

我想用一个提交按钮将三个 textarea 值更新到数据库中。但这对我不起作用。当我尝试更新单个 textarea 时,值会正确更新到数据库中,但是当我添加其他两个 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['submit'])) {

    $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(!empty($value)) { 它可以创造奇迹。
  • 请同时添加 html 代码,您是否尝试从一个表单提交所有这三个 textarea 值?
  • 是的,我正在尝试从一个表单提交所有这些 textarea 值。
  • 我以前的代码也可以正常工作。问题是当我尝试插入单个 textarea 时,我的其他两个 textarea 也没有更新。请检查此图像,然后您就会明白:i.imgur.com/SUgkCqp.png
  • @Dinidu 我已经添加了 HTML 代码。请您检查一下。

标签: php mysql textarea


【解决方案1】:

试试这个

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

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

【讨论】:

  • 但之前的代码也可以正常工作。问题是当我尝试插入单个 textarea 时,我的其他两个 textarea 也没有更新。请检查此图像,然后您就会明白:i.imgur.com/SUgkCqp.png
  • 您需要检查值是否为空白 isset 检查变量是否具有包含( False 、 0 或空字符串)的值。并且还使用 else 。 if (isset($_POST['bangla_head']) &amp;&amp; ($_POST['bangla_head']!='') ) { //your code } else if(second condition) { }.
【解决方案2】:

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

这样写你的代码:-

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

【讨论】:

  • 感谢您的解决方案。当我一次更新三个 textarea 时,它会成功更新。但是,当我更新单个 textarea 时,其他两个 textarea 字段就会被占用。此外,当我更新两个 textarea 时,其他字段变为空白。请检查此屏幕截图然后您就会明白 - i.imgur.com/SUgkCqp.png
  • 我无法对空字段进行验证。我仍然是 PHP 的初学者,所以我不确定该怎么做。我知道我的 PHP 哪里出了问题,我只是不知道解决它的方法。请帮忙。
  • 非常感谢您的回复。请查看我在之前评论中发布的屏幕截图,然后您就会了解实际问题。
  • 请参考此链接。它将帮助您理解验证。 w3schools.in/php/form-validation
  • 感谢您的链接。该链接对我来说真的很有用。请用截图查看我的第一条评论好吗?
猜你喜欢
  • 1970-01-01
  • 2014-01-15
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多