簡體   English   中英

更新表格,我在做什么錯?

[英]Update form, what am I doing wrong?

我為MySQL數據庫制作了一個更新表格,但是由於某種原因它沒有更新我的數據庫。 它可以正常工作,但不會發生任何錯誤.....有人可以告訴我我在做什么錯嗎?

***編輯

更新了腳本,但仍然無法正常工作.....

    <html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/layout.css"/>
    </head>
    <body>
        <div id="menu">
            <div id="menu_wrapper">
                <ul>
                    <li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
                        <ul>
                            <li><a href="configuratiebeheer_hardware.php">Lijst hardware</a></li>
                            <li><a href="hardware_toevoegen.php">Hardware toevoegen</a></li>
                            <li><a href="hardware_verwijderen.php">Hardware verwijderen</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>      
<?php
            $connect=mysql_connect("localhost", "root","");
            mysql_select_db("helpdesk_middenpolder", $connect);
            $id=$_GET['id'];
            $q="SELECT * FROM hardware WHERE hardwareID=$id";

            $r=mysql_query($q);
            echo  "<form method='post'>";
            echo    "<table border='1'>";
            echo    "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";
            while   ($x=mysql_fetch_array($r)){
                echo "<tr>";
                echo "<td>";
                echo "<input type='text' value='".$x['merknaam']."'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' value='".$x['producttype']."'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' value='".$x['hardwaretype']."'>";
                echo "</td>";
                echo "</tr>";
            }
            echo "</table>";


?>
<?php
            if(isset($_POST['updatehardware'])){ 
            $query = "UPDATE hardware SET merknaam='".$_POST['merknaam']."', producttype='".$_POST['producttype']."', hardwaretype='".$_POST['hardwaretype']."' WHERE hardwareID=".$id."";
            mysql_query($query);
            }
            ?>
<?php
mysql_close($connect);
?>


    <input type="submit" name="updatehardware" value="Hardware updaten">
    </form>
    </body>
</html>

提前致謝!

嘗試這個。 在表單中包括文本字段。

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/layout.css"/>
    </head>
    <body>
        <div id="menu">
            <div id="menu_wrapper">
                <ul>
                    <li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
                        <ul>
                            <li><a href="configuratiebeheer_hardware.php">Lijst hardware</a></li>
                            <li><a href="hardware_toevoegen.php">Hardware toevoegen</a></li>
                            <li><a href="hardware_verwijderen.php">Hardware verwijderen</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>      
<?php
            $connect=mysql_connect("localhost", "root","");
            mysql_select_db("helpdesk_middenpolder", $connect);
            $id=$_GET['id'];
            $q="SELECT * FROM hardware WHERE hardwareID=$id";


            $r=mysql_query($q);
            echo  "<form method='post'>";
            echo    "<table border='1'>";
            echo    "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";
            while   ($x=mysql_fetch_array($r)){
                echo "<tr>";
                echo "<td>";
                echo "<input type='text' value='".$x['merknaam']."'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' value='".$x['producttype']."'>";
                echo "</td>";
                echo "<td>";
                echo "<input type='text' value='".$x['hardwaretype']."'>";
                echo "</td>";
                echo "</tr>";
            }
            echo "</table>";

        mysql_close($connect);
?>
<?php
    if(isset($_POST['updatehardware'])){ 
        $query = "UPDATE hardware SET merknaam='".$_POST['merknaam']."', producttype='".$_POST['producttype']."', hardwaretype='".$_POST['hardwaretype']."' WHERE hardwareID='".$id."'";
        }
        mysql_query($query);
?>



    <input type="submit" name="updatehardware" value="Hardware updaten">
    </form>
    </body>
</html>

並使用$_POST[]獲取輸入字段中的值。

您需要將查詢更改為POST值:

if(isset($_POST['updatehardware'])){ 
    $query = "UPDATE hardware SET merknaam='".$_POST['merknaam']."', producttype='".$_POST['producttype']."', hardwaretype='".$_POST['hardwaretype']."' WHERE hardwareID='".$id."'";
    }
    mysql_query($query);

還為您的表單元素設置名稱

<input type='text' value='".$x['merknaam']."' name="merknaam">
  1. 您正在使用mysql_close($ connect); 並且比嘗試更新,執行mysql_close($ connect); 更新后。

  2. 此外,您的表單除了“提交”按鈕之外,不包括其他字段。

  3. 您的表單輸入值與以前相同,因此即使您使用這些值進行更新-您也不會看到差異。

  4. 您正在發布,但是在這里您從GET $id=$_GET['id']; ,則需要在表單BTW上添加“ id”字段。

  5. 更新查詢時,請使用POSTed變量,而不要使用從數據庫中檢索到的值。

所以看看區別:

<?php
$connect=mysql_connect("localhost", "root","");
mysql_select_db("helpdesk_middenpolder", $connect);

$id = $_POST['id']; 

$q="SELECT * FROM hardware WHERE hardwareID = $id";

$r=mysql_query($q);

echo "<form method='post'>";
echo "<table border='1'>";
echo "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";

while   ($x=mysql_fetch_array($r)){
    echo "<tr>";
    echo "<td>";
        echo "<input type='text' value='".$x['merknaam']."'>";
    echo "</td>";

    echo "<td>";
        echo "<input type='text' value='".$x['producttype']."'>";
    echo "</td>";

    echo "<td>";
        echo "<input type='text' value='".$x['hardwaretype']."'>";
    echo "</td>";
    echo "</tr>";
}
echo "</table>";
echo '<input type="hidden" name="id" value="' . $id . '">';
echo '<input type="submit" name="updatehardware" value="Hardware updaten">';
echo "</form>";

if(isset($_POST['updatehardware'])){ 
$query = "UPDATE hardware SET merknaam='".$_POST['merknaam']."', producttype='".$_POST['producttype']."', hardwaretype='".$_POST['hardwaretype']."' WHERE hardwareID='".$id."'";
}

mysql_query($query);

mysql_close($connect);

?>

還要注意的是使用表內形式是困難的,因為2號答案最好的解釋在這里

請記住,如果硬件ID不是varchar,則需要用單引號將varchars和其他類型(數字類型除外)括起來,請執行此操作

$query = "UPDATE hardware SET merknaam='$_POST['merknaam']', producttype='$_POST['producttype']', hardwaretype='$_POST['hardwaretype']' WHERE hardwareID=$id ";

其他

$query = "UPDATE hardware SET merknaam='$_POST['merknaam']', producttype='$_POST['producttype']', hardwaretype='$_POST['hardwaretype']' WHERE hardwareID='$id' ";

您沒有在form標記內輸入元素。

在輸入標簽上方移動開始表格標簽

<html>
<head>
<link rel="stylesheet" type="text/css" href="css/layout.css"/>
</head>
<body>
<div id="menu">
    <div id="menu_wrapper">
    <ul>
        <li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
        <ul>
            <li><a href="configuratiebeheer_hardware.php">Lijst hardware</a></li>
            <li><a href="hardware_toevoegen.php">Hardware toevoegen</a></li>
            <li><a href="hardware_verwijderen.php">Hardware verwijderen</a></li>
        </ul>
        </li>
    </ul>
    </div>
</div>
<form method="post">
<?php
        $connect=mysql_connect("localhost", "root","");
        mysql_select_db("helpdesk_middenpolder", $connect);
        $id=$_GET['id'];
        $q="SELECT * FROM hardware WHERE hardwareID=$id";


        $r=mysql_query($q);

        echo    "<table border='1'>";
        echo    "<th>merknaam</th><th>producttype</th><th>hardwaretype</th>";
        while   ($x=mysql_fetch_array($r)){
        echo "<tr>";
        echo "<td>";
        echo "<input type='text' value='".$x['merknaam']."'>";
        echo "</td>";
        echo "<td>";
        echo "<input type='text' value='".$x['producttype']."'>";
        echo "</td>";
        echo "<td>";
        echo "<input type='text' value='".$x['hardwaretype']."'>";
        echo "</td>";
        echo "</tr>";
        }
        echo "</table>";

    mysql_close($connect);
?>
<?php
    if(isset($_POST['updatehardware'])){ 
    $query = "UPDATE hardware SET merknaam='".$x['merknaam']."', producttype='".$x['producttype']."', hardwaretype='".$x['hardwaretype']."' WHERE hardwareID='".$id."'";
    }
    mysql_query($query);
?>


    <input type="submit" name="updatehardware" value="Hardware updaten">
    </form>
    </body>
</html>

您錯誤地包含了form

輸入元素必須被表單元素包圍(例如<input /><select /> ...等)

像這樣改變

<form method="post">
  <!-- while loop stuff -->

  <input type="submit" name="updatehardware" value="Hardware updaten">
</form>
  1. 將控制器與視圖分開。 至少,將所有PHP代碼移到HTML之前。

  2. 在輸入字段中使用名稱,因此您可以輕松區分不同的內容。

  3. 名稱中使用方括號可提交相同的輸入,但可用於不同的行/項目。

  4. 總是類型轉換值,是整數和浮點數,它們將自動消除輸入錯誤的問題。 如果您沒有使用PDO和綁定參數,請使用mysql_real_escape_string來確保用戶不會破壞您的數據庫。

  5. 將GET和POST保留在函數之外,將它們發送到函數中代碼的不同部分。

  6. 對於數據庫交互,請使用PDO或MySQLi,以避免出現舊問題。

如果不解決第六個問題,我的解決方案將如下所示:

<?php

// This is your "library" of functions
function getConnection() {
    $connect=mysql_connect("localhost", "root","");
    mysql_select_db("helpdesk_middenpolder", $connect);
    return $connect;
}
function endConnection() {
    mysql_close($connect);
}
function updateProducts($products) {
    $query = '';
    foreach ($products as $productId => $productData) {
        $query .= "UPDATE hardware SET merknaam='".mysql_real_escape_string($productData['merknaam'])."', producttype='".mysql_real_escape_string($productData['producttype'])."', hardwaretype='".mysql_real_escape_string($productData['hardwaretype'])."' WHERE hardwareID='".(int) $productId."'; ";
    }
    mysql_query($query);
}
function getProducts($id) {
    $q = "SELECT * FROM hardware WHERE hardwareID=$id";
    $r = mysql_query($q);
    $products = array();
    while ($x=mysql_fetch_array($r)) {
        $products[] = $x;
    }
    return $products;
}

// This is your controller
$connect = getConnection();;
if (isset($_POST['products'])) {
    updateProducts($_POST['products']);
}
$input_id = (isset($_GET['id']) ? (int) $_GET['id'] : 0);
if ($input_id > 0) {
    $products = getProducts($input_id);
}
endConnection();

// This is your view
?><html>
    <head>
        <link rel="stylesheet" type="text/css" href="css/layout.css" />
    </head>
    <body>
        <div id="menu">
            <div id="menu_wrapper">
                <ul>
                    <li>Configuratiebeheer<img src="afb/pijltje.png" width="10"/></a>
                        <ul>
                            <li><a href="configuratiebeheer_hardware.php">Lijst hardware</a></li>
                            <li><a href="hardware_toevoegen.php">Hardware toevoegen</a></li>
                            <li><a href="hardware_verwijderen.php">Hardware verwijderen</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
        <form method="get">
            <h2>Get product by ID</h2>
            <div>
                <label for="productId">ID</label> <input id="productId" type="text" name="id" value="<?php echo ($input_id > 0 ? $input_id : ''); ?>" />
            </div>
            <input type="submit" value="Get item" />
        </form>
        <?php if (isset($products) && !empty($products)): ?>
        <h2>Products found by ID</h2>
        <form method="post">
            <table border="1">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>merknaam</th>
                        <th>producttype</th>
                        <th>hardwaretype</th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($products as $product): ?>
                    <tr>
                        <td><?php echo $product['hardwareID']; ?></td>
                        <td><input type="text" name="product[<?php echo $product['hardwareID']; ?>][merknaam]" value="<?php echo $product['merknaam']; ?>" /></td>
                        <td><input type="text" name="product[<?php echo $product['hardwareID']; ?>][producttype]" value="<?php echo $product['producttype']; ?>" /></td>
                        <td><input type="text" name="product[<?php echo $product['hardwareID']; ?>][hardwaretype]" value="<?php echo $product['hardwaretype']; ?>" /></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
            <input type="submit" value="Hardware updaten" />
        </form>
        <?php endif; ?>
    </body>
</html>

尚未測試此代碼,但它應該使您理解應該開始的方向,應該完成的過程,或者至少是路徑。

嘗試編寫簡潔的代碼,不要害怕使用函數和類,並始終從錯誤中學習。

代碼中有很多錯誤:

  1. 輸入標簽應與<form>標簽一起放置
  2. 而不是在UPDATE查詢中使用$x['merknaam'] ,請使用$_POST['merknaam'] (其他字段相同); 提防SQL注入。
  3. hardwareID可能是一個INT列; 不需要單引號將UPDATE查詢中的值引起來
  4. 更新代碼應放在表格之前
  5. $id = $_GET['id']附近有邏輯錯誤; 將表單發布為POST方法將丟失$id 再次進行 SQL注入。 在表單標簽內添加<input type="hidden" name="id" value="<?php echo $id; ?>" />
  6. (次要)您的DOCTYPE在哪里?
  7. 停止使用不推薦使用的mysql_*庫; 請改用PDO / MySQLi。
  8. (次要)您的<title>標簽在哪里? 還有整個<head>
  9. border='1'已棄用; 改用CSS
  10. 您在mysql_close() mysql_query()之后執行mysql_query() mysql_close() ,這將導致錯誤
  11. 如果沒有看到錯誤,則必須關閉錯誤報告; 通過ini_set('display_errors', '1');將其重新打開ini_set('display_errors', '1');
  12. 強烈建議您通過檢查mysql_connect()的返回值來檢查mysql連接是否成功建立
  13. (次要)通常不需要提交按鈕的name ,但是可以使用它。
  14. 您的<th>沒有被<tr>包圍

修復這些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM