简体   繁体   English

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

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

I made a update form for my MySQL database but for some reason it isn't updating my database. 我为MySQL数据库制作了一个更新表格,但是由于某种原因它没有更新我的数据库。 It works without errors but it doesn't do anything..... Could someone tell me what I'm doing wrong? 它可以正常工作,但不会发生任何错误.....有人可以告诉我我在做什么错吗?

***EDIT ***编辑

updated the script but it still isn't working..... 更新了脚本,但仍然无法正常工作.....

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

Thanks in advance! 提前致谢!

Try this. 尝试这个。 Include text fields in 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>      
<?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>

and use $_POST[] for getting values in input fields.. 并使用$_POST[]获取输入字段中的值。

You need to change your query to POST values: 您需要将查询更改为POST值:

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

Also set names for yuour form elements 还为您的表单元素设置名称

<input type='text' value='".$x['merknaam']."' name="merknaam">
  1. You are using the mysql_close($connect); 您正在使用mysql_close($ connect); and than trying to update, do the mysql_close($connect); 并且比尝试更新,执行mysql_close($ connect); after the update. 更新后。

  2. Also your form does not include other fields than Submit button . 此外,您的表单除了“提交”按钮之外,不包括其他字段。

  3. Your form input values are the same as before , so even if you update with these values - you wont see the difference. 您的表单输入值与以前相同,因此即使您使用这些值进行更新-您也不会看到差异。

  4. You are POSTing but here you are getting the id from GET $id=$_GET['id']; 您正在发布,但是在这里您从GET $id=$_GET['id']; , you need to include the 'id' field on the form BTW. ,则需要在表单BTW上添加“ id”字段。

  5. On update query use the POSTed variable rather than the retrieved values from DB. 更新查询时,请使用POSTed变量,而不要使用从数据库中检索到的值。

So see the difference : 所以看看区别:

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

?>

Also notice that using form inside a table is difficult as explained on the 2nd answer best here 还要注意的是使用表内形式是困难的,因为2号答案最好的解释在这里

Remember you need to enclose varchars and other types except numerical types by single quotation if hardware id is not varchar do this 请记住,如果硬件ID不是varchar,则需要用单引号将varchars和其他类型(数字类型除外)括起来,请执行此操作

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

else 其他

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

You don't have your input elements within the form tag. 您没有在form标记内输入元素。

Move the opening form tag ABOVE the input tags 在输入标签上方移动开始表格标签

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

You include the form incorrectly. 您错误地包含了form

An input elements must surrounded with form elements(like <input /> , <select /> ... etc) 输入元素必须被表单元素包围(例如<input /><select /> ...等)

Change like this, 像这样改变

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

  <input type="submit" name="updatehardware" value="Hardware updaten">
</form>
  1. Separate controller from view. 将控制器与视图分开。 At least, move all of your PHP code BEFORE HTML. 至少,将所有PHP代码移到HTML之前。

  2. Use names for your input fields, so you can differ what is what with ease. 在输入字段中使用名称,因此您可以轻松区分不同的内容。

  3. Use square brackets in names, to submit same inputs, but for different rows/items. 名称中使用方括号可提交相同的输入,但可用于不同的行/项目。

  4. ALWAYS typecast values, that are integers and floats, that will automatically remove issues with wrong inputs. 总是类型转换值,是整数和浮点数,它们将自动消除输入错误的问题。 If you are not using PDO and bound parameters, use mysql_real_escape_string to make sure, user will not spoil your database. 如果您没有使用PDO和绑定参数,请使用mysql_real_escape_string来确保用户不会破坏您的数据库。

  5. Keep GET's and POST's out of functions, send them to functions in separate part of code. 将GET和POST保留在函数之外,将它们发送到函数中代码的不同部分。

  6. For database interactions use PDO or MySQLi, to avoid old issues. 对于数据库交互,请使用PDO或MySQLi,以避免出现旧问题。

Without solving 6th issue, my solution would look something like this: 如果不解决第六个问题,我的解决方案将如下所示:

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

Haven't tested this code, but it should make you understand the way, it should be done, or at least path, that you should start to move towards. 尚未测试此代码,但它应该使您理解应该开始的方向,应该完成的过程,或者至少是路径。

Try to write clean code, don't be afraid to use function and classes, and always learn from your mistakes. 尝试编写简洁的代码,不要害怕使用函数和类,并始终从错误中学习。

Lots of errors in the codes: 代码中有很多错误:

  1. input tags should be place withing <form> tag 输入标签应与<form>标签一起放置
  2. instead of $x['merknaam'] in the UPDATE query, use $_POST['merknaam'] (same for other fields); 而不是在UPDATE查询中使用$x['merknaam'] ,请使用$_POST['merknaam'] (其他字段相同); watch out for SQL Injection though. 提防SQL注入。
  3. hardwareID is probably an INT column; hardwareID可能是一个INT列; no single quote is needed to surround the value in the UPDATE query 不需要单引号将UPDATE查询中的值引起来
  4. the update codes should be placed before the form 更新代码应放在表格之前
  5. there is a logic error near $id = $_GET['id'] ; $id = $_GET['id']附近有逻辑错误; posting the form as POST method will lose the $id ; 将表单发布为POST方法将丢失$id SQL Injection again . 再次进行 SQL注入。 Add a <input type="hidden" name="id" value="<?php echo $id; ?>" /> inside your form tag 在表单标签内添加<input type="hidden" name="id" value="<?php echo $id; ?>" />
  6. (minor) where is your DOCTYPE? (次要)您的DOCTYPE在哪里?
  7. stop using deprecated mysql_* libraries; 停止使用不推荐使用的mysql_*库; use PDO / MySQLi instead. 请改用PDO / MySQLi。
  8. (minor) where is your <title> tag? (次要)您的<title>标签在哪里? and the whole <head> as well ! 还有整个<head>
  9. border='1' is deprecated; border='1'已弃用; use CSS instead 改用CSS
  10. you perform a mysql_query() AFTER mysql_close() , which will cause error 您在mysql_close() mysql_query()之后执行mysql_query() mysql_close() ,这将导致错误
  11. if you don't see error, error reporting must be turned OFF; 如果没有看到错误,则必须关闭错误报告; turn it back ON by ini_set('display_errors', '1'); 通过ini_set('display_errors', '1');将其重新打开ini_set('display_errors', '1');
  12. highly suggest you to check whether mysql connection is established successfully or not, by checking the return value of mysql_connect() 强烈建议您通过检查mysql_connect()的返回值来检查mysql连接是否成功建立
  13. (minor) normally you don't need a name for submit button, but it's fine to have it. (次要)通常不需要提交按钮的name ,但是可以使用它。
  14. Your <th> are not surrounded by <tr> 您的<th>没有被<tr>包围

Fix these. 修复这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM