Php 更新表单,我做错了什么?

Php 更新表单,我做错了什么?,php,mysql,forms,Php,Mysql,Forms,我为我的MySQL数据库制作了一个更新表单,但由于某些原因,它没有更新我的数据库。它可以正常工作,但没有任何错误。。。。。 有人能告诉我我做错了什么吗 ***编辑 已更新脚本,但仍无法运行 <html> <head> <link rel="stylesheet" type="text/css" href="css/layout.css"/> </head> <body> &

我为我的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>

  • 配置Beheer

表单标记中没有输入元素

将开始表单标记移到输入标记上方

<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>

  • 配置Beheer

表单标记中没有输入元素

将开始表单标记移到输入标记上方

<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>

  • 配置Beheer

您错误地包含了
表单

输入元素必须用表单元素包围(如
…等)

像这样改变,

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

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

您错误地包含了
表单

输入元素必须用表单元素包围(如
…等)

像这样改变,

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

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

您需要将查询更改为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">

您需要将查询更改为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">

试试这个。在表单中包含文本字段

<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>

  • 配置Beheer

试试这个。在表单中包含文本字段

<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>

  • 配置Beheer
  • 您正在使用mysql\u close($connect);除了尝试更新,还要关闭mysql_($connect);更新之后

  • 此外,您的表单不包括“提交”按钮以外的其他字段

  • 表单输入值与以前相同,因此即使使用这些值进行更新,也不会看到差异

  • 您正在发布,但在这里您从GET
    $id=$\u GET['id']获取id,您需要在表单BTW中包含“id”字段

  • 在更新查询时,使用已发布的变量,而不是从数据库中检索到的值

  • 因此,请看区别:

    <?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);
    
    ?>
    
    
    
  • 您正在使用mysql\u close($connect);除了尝试更新,还要关闭mysql_($connect);更新之后

  • 此外,您的表单不包括“提交”按钮以外的其他字段

  • 表单输入值与以前相同,因此即使使用这些值进行更新,也不会看到差异

  • 您正在发布,但在这里您从GET
    $id=$\u GET['id']获取id,您需要在表单BTW中包含“id”字段

  • 在更新查询时,使用已发布的变量,而不是从数据库中检索到的值

  • 因此,请看区别:

    <?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);
    
    ?>
    

    代码中有很多错误:

  • 输入标记应与
    标记一起放置
  • 更新
    查询中,使用
    $\u POST['merknaam']
    代替
    $x['merknaam']
    (其他字段相同);不过要注意SQL注入
  • hardwareID
    可能是
    INT
    列;更新查询中的值不需要单引号
  • 更新代码应放在表单之前

  • $id=$\u GET['id']
    附近有一个逻辑错误;以
    POST
    方法发布表单将丢失
    $id
    ;再次执行SQL注入。在代码中添加大量错误:

  • 输入标记应与
    标记一起放置
  • 更新
    查询中,使用
    $\u POST['merknaam']
    代替
    $x['merknaam']
    (其他字段相同);不过要注意SQL注入
  • hardwareID
    可能是
    INT
    列;更新查询中的值不需要单引号
  • 更新代码应放在表单之前
  • $id=$\u GET['id']
    附近有一个逻辑错误;以
    POST
    方法发布表单将丢失
    $id
    ;再次执行SQL注入。添加一个
    
    
  • 将控制器与视图分开。至少,将所有PHP代码移到HTML之前

  • 在输入字段中使用名称,以便轻松区分内容

  • 在名称中使用方括号,以提交相同的输入,但用于不同的行/项目

  • 总是类型转换值,即整数和浮点数,将自动删除错误输入的问题。如果您没有使用PDO和绑定参数,请使用
    mysql\u real\u escape\u string
    确保用户不会破坏您的数据库

  • 不要让GET和POST出现在函数中,将它们发送到代码中不同部分的函数中

  • 用于数据库交互