未提交Php / sql表单的人口值

时间:2011-10-22 16:45:50

标签: php html sql

你有一个简单的问题。目前我正在填写一个包含sql查询数据的表单。因此,例如,他们选择第二项,并且表格填充来自数据库的项目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>

2 个答案:

答案 0 :(得分:0)

误读帖子,抱歉。重新评估...

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

在HTML中,添加空格。 NAME = “CurrentLevel” ID = “CurrentLevel” 值= “”

答案 1 :(得分:0)

我没有看到代码中设置/填充$ _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;
}