php mysql_fetch_array 是否适用于 html 输入框?

发布于 2024-08-25 18:28:26 字数 1718 浏览 3 评论 0原文

这是我的整个PHP代码:

<?php if(empty($_POST['selid']))
        {echo "no value selected";  }
        else 
        {
            $con = mysql_connect("localhost","root","");
            if(mysql_select_db("cdcol", $con))
                {
                    $sql= "SELECT * FROM products where Id = '$_POST[selid]'";

                    if($result=mysql_query($sql))
                    {   

                        echo "<form name=\"updaterow\" method=\"post\" action=\"dbtest.php\">";
                        while($row = mysql_fetch_array($result))
                        { 
                        echo "Id :<input type=\"text\" name=\"ppId\" value=".$row['Id']." READONLY></input></br>";
                        echo "Name :<input type=\"text\" name=\"pName\" value=".$row['Name']."></input></br>";
                        echo "Description :<input type=\"text\" name=\"pDesc\" value=".$row['Description']."></input></br>";
                        echo "Unit Price :<input type=\"text\" name=\"pUP\" value=".$row['UnitPrice']."></input></br>";
                        echo "<input type=\"hidden\" name=\"mode\" value=\"Update\"/>";

                        }
                        echo "<input type=\"submit\" value=\"Update\">";
                        echo "</form>";
                    }
                    else {echo "Query ERROR";}
                }
        }
?>

问题这里是,...如果我使用mysql_fetch_array($result)从数据库获取的值是这样的:(说描述是 “我的产品”

:)那么 ; 在输入框中仅显示“我的”单词(或数字) “SPACE”(即空格)之后不显示? 像上面这样的输入框可以显示两个或多个单词(以空格分隔)的数据吗?

this is my entire PHP code:

<?php if(empty($_POST['selid']))
        {echo "no value selected";  }
        else 
        {
            $con = mysql_connect("localhost","root","");
            if(mysql_select_db("cdcol", $con))
                {
                    $sql= "SELECT * FROM products where Id = '$_POST[selid]'";

                    if($result=mysql_query($sql))
                    {   

                        echo "<form name=\"updaterow\" method=\"post\" action=\"dbtest.php\">";
                        while($row = mysql_fetch_array($result))
                        { 
                        echo "Id :<input type=\"text\" name=\"ppId\" value=".$row['Id']." READONLY></input></br>";
                        echo "Name :<input type=\"text\" name=\"pName\" value=".$row['Name']."></input></br>";
                        echo "Description :<input type=\"text\" name=\"pDesc\" value=".$row['Description']."></input></br>";
                        echo "Unit Price :<input type=\"text\" name=\"pUP\" value=".$row['UnitPrice']."></input></br>";
                        echo "<input type=\"hidden\" name=\"mode\" value=\"Update\"/>";

                        }
                        echo "<input type=\"submit\" value=\"Update\">";
                        echo "</form>";
                    }
                    else {echo "Query ERROR";}
                }
        }
?>

PROBLEM here is, ....if the value i am getting from database using mysql_fetch_array($result) is like:(say Description is:) "my product"

then;
in input box it shows only "my" the word(or digit)
after "SPACE"(ie blank space) doesn't get displayed?
can input box like above can display the data with two or more words(separated by blank spaces)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-09-01 18:28:26
echo "Unit Price :<input type=\"text\" name=\"pUP\" value=\"".$row['UnitPrice']."\"></input></br>";

您需要将 value 用引号引起来。

echo "Unit Price :<input type=\"text\" name=\"pUP\" value=\"".$row['UnitPrice']."\"></input></br>";

You need to enclose value in quotes.

硬不硬你别怂 2024-09-01 18:28:26

html 解析器无法知道 表示 value=abc def。它必须将其解析为两个属性,即具有值 abc 的属性 value 和不具有值的属性 def

您必须将值括在引号中,例如
您还必须在值内将 " 编码为 " 。否则 html 解析器将再次感到困惑,因为它无法知道 value="abc"def" 中的第二个 " 不是分隔符,而是您可以使用 htmspecialchars() 例如

while($row = mysql_fetch_array($result))
{
  printf('
    Id :<input type="text" name="ppId" value="%s" READONLY></input><br>
    Name :<input type="text" name="pName" value="%s"></input><br>
    Description :<input type="text" name="pDesc" value="%s"></input><br>
    Unit Price :<input type="text" name="pUP" value="%s"></input><br>";
    <input type="hidden" name="mode" value="Update"/>',
    htmlspecialchars($row['Id']),
    htmlspecialchars($row['Name']),
    htmlspecialchars($row['Description']),
    htmlspecialchars($row['UnitPrice'])
  );
}

The html parser has no way of knowing that <input value=abc def...> means value=abc def. It has to parse it as two attributes, the attribute value with the value abc and the attribute def without a value.

You have to enclose the value in quotes, e.g. <input value="abc def" ...>
You also have to encode " as " within the value. Otherwise the html parser will get confused again, since it has no way of knowing that the second " in value="abc"def" is not a delimeter but part of the content. You can use htmspecialchars() for this.

e.g.

while($row = mysql_fetch_array($result))
{
  printf('
    Id :<input type="text" name="ppId" value="%s" READONLY></input><br>
    Name :<input type="text" name="pName" value="%s"></input><br>
    Description :<input type="text" name="pDesc" value="%s"></input><br>
    Unit Price :<input type="text" name="pUP" value="%s"></input><br>";
    <input type="hidden" name="mode" value="Update"/>',
    htmlspecialchars($row['Id']),
    htmlspecialchars($row['Name']),
    htmlspecialchars($row['Description']),
    htmlspecialchars($row['UnitPrice'])
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文