【问题标题】:Php/sql form population values not submitted未提交 php/sql 表单填充值
【发布时间】:2013-03-07 05:47:29
【问题描述】:

您好,有一个简单的问题。目前我正在用来自 sql 查询的数据填写表格。因此,例如,他们选择了第 2 项,表格中填充了数据库中第 2 项的数据。然后我希望能够按下我的更新按钮并提交表单中的数据,以便如果它在表单中更改,它会在数据库中更改。 例如:

  • 从下拉列表中选择项目 1
  • 按下回车键
  • 页面被刷新,表单中填充了第一次提交时 sql 语句中的数据
  • ^^^ 这行得通

我知道表单中有信息,并且希望能够点击 ubdate 按钮,以便传递旧数据或输入值并将其替换。问题是表单中存储的变量和数据没有通过

这是页面源代码

enter code here


<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="css/main.css" type="text/css" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
    <title>Inventory</title>

</head>
<body>

<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <link rel="stylesheet" href="css/headercss.css" type="text/css" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
    <title>Inventory</title>

</head>
<body>

<div id="navigation">
        <ul>
            <li>
            <a href="">Total Inventory</a>
               <ul>
                <li><a href="TotalInventory.php">Total Inventory</a></li>
                <li><a href="NewItem.php">Add new Item</a></li>
                <li><a href="SpecificItem.php">View Specific Item</a></li>

                <li><a href="UpdateItem.php">Update Item</a></li>
            </ul>
            </li>

            <li>
            <a href="">House Inventory</a>
               <ul>
                <li><a href="#">House Inventory</a></li>

                <li><a href="#">Add new Item</a></li>

            </ul>
            </li>


        </ul>
    </div>

</body> <br />
    <form id="addItem" action="/workspace/iventory2/UpdateItem.php" method="post" >

        <select name="items" id="items">

        <option value"sdgssr5y">sdgssr5y</option><option value"a">a</option><option value"sf">sf</option><option value"gsh">gsh</option><option value"cat">cat</option><option value""></option><option value"afdsf">afdsf</option><option value"15">15</option><option value"zzz">zzz</option><option value"nnn">nnn</option><option value"dsdsgfsg">dsdsgfsg</option>     </select>
        <input class="submit" type="submit" value="Enter" name="submit"/>

        <input class="update" type="submit" value="update" name="update"/>      
    </form>
    <form method="post">
        <br />


    <form id="addItem" action="/workspace/iventory2/UpdateItem.php" method="post" autocomplete="off" >

        <label>Item Name: <input name="ItemName" id="ItemName" value="15"/></label><label>CostPrice: <input name="CostPrice"id="CostPrice"value="10"/></label><label>CurrentLevel:<input name="CurrentLevel"id="CurrentLevel"value="10"/></label>   


        <input class="submit" type="submit" value="Enter" name="submit"/>   
    </form>

    <form method="post">

</body>

【问题讨论】:

  • 不清楚什么不起作用,但您的 HTML 不正确。您需要在属性之间留一些空格:.
  • 如果您发布表单生成的 HTML 代码可能会有所帮助。
  • 您是指IDE的代码还是来自本地主机源代码?
  • 在浏览器中右键单击并选择“查看 HTML 源代码”时获得的代码。这是 PHP 脚本生成的代码。

标签: php html sql


【解决方案1】:

看错帖子了,抱歉。重新评估...

2 建议尝试,一个是编码练习,如果可能的话不要使用文本作为 WHERE,分配一个 ID 并传递它。如果使用文本,则必须考虑大小写等。

在您的 HTML 中,添加空格。 name="CurrentLevel"id="CurrentLevel"value=""

【讨论】:

  • 要捕获 Html 错误,最好使用像 validator.w3.org 这样的 Html 验证器运行您的输出
  • 最后我检查了如果引用正确,属性之间不需要有空格。唯一的必需空格是在标签名称之后。
  • 除了可怕的编码实践之外,当更新按钮被传递时,会导致值不被传递吗?例如,我要传递的值是行 ['ItemName'],但它没有被设置为值?当它得到检查以查看它是否已设置时,它会失败。
  • 下一个问题,你的表单ID都是一样的......无论如何,在打开的php标签之后的文件顶部,当然,把print_r($_POST);并查看单击按钮时正在处理的内容。它将帮助您调试问题。除非我有涉及的文件/所有代码,否则我无法真正测试您的代码。
【解决方案2】:

我没有看到 $_SESSION['item'] 变量在您的代码中的设置/填充位置,但我会将该值存储在您表单中的 HIDDEN INPUT 中。

表格代码:

<form action="" method="post"><!-- Form action="" forces form to submit to itself -->
<input type="hidden" name="update" value="true" /><!-- Tell our script to process -->
<?php
$work = mysql_query("
SELECT *
FROM Inventory WHERE `ItemName` LIKE '{$_SESSION['item']}' LIMIT 1
");

if ( $work && ( mysql_num_rows($work) > 0 ) ) {

$row = @mysql_fetch_assoc( $work );

echo '<input type="hidden" name="item_to_update" value="' .
$row['ItemName'] . '" />' .
'<label>Item Name: <input name="ItemName" id="ItemName" value="' .
htmlentities($row['ItemName']) .
'"/></label>' .
'<label>CostPrice: <input name="CostPrice"id="CostPrice"value="' .
htmlentities($row['CostPrice']) .
'"/></label>' .
'<label>CurrentLevel:<input name="CurrentLevel"id="CurrentLevel"value="' .
htmlentities($row['CurrentLevel']) .
'"/></label>';
}

及处理代码:

<?php
if ( isset( $_REQUEST['update'] ) ) {

$ItemName = mysql_real_escape_string( $_REQUEST['ItemName'] );
$CostPrice = mysql_real_escape_string( $_REQUEST['CostPrice'] );
$CurrentLevel = mysql_real_escape_string( $_REQUEST['CurrentLevel'] );
$ItemToUpdate = mysql_real_escape_string( $_REQUEST['item_to_update'] );

mysql_query("
UPDATE Inventory 
SET ItemName='$ItemName',CostPrice='$CostPrice',CurrentLevel='$CurrentLevel'
WHERE  ItemName='$ItemToUpdate'
LIMIT 1
");

$_SESSION['item'] = $ItemName;
}

【讨论】:

    猜你喜欢
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多