【问题标题】:Editing SQL table with PHP from HTML form从 HTML 表单使用 PHP 编辑 SQL 表
【发布时间】:2019-10-20 18:01:08
【问题描述】:

我在这个项目上工作了一天,现在我的大脑无法帮助我完成。我希望有人能帮助我!我还在学习,所以你会发现错误。我正在为我的队友建立一个数据库。我刚刚创建了一个链接到 SQL 数据库的大表单。有用。还有一个页面,他们可以在其中查看 html 表中的所有数据。现在我正在编辑类似表单中的表值。

这是php表单

<?php
$link = mysqli_connect("localhost", "", "", "");

if($link === false) {
    die("ERROR: " . mysqli_connect_error());
}

$n_ordine = mysqli_real_escape_string($link, $_REQUEST['n_ordine']);
$cliente = mysqli_real_escape_string($link, $_REQUEST['cliente']);
$data = mysqli_real_escape_string($link, $_REQUEST['data']);
$paese = mysqli_real_escape_string($link, $_REQUEST['paese']);
$operatore = mysqli_real_escape_string($link, $_REQUEST['operatore']);
$n_spedizione = mysqli_real_escape_string($link, $_REQUEST['n_spedizione']);
$fornitore = mysqli_real_escape_string($link, $_REQUEST['fornitore']);
$ord_forn = mysqli_real_escape_string($link, $_REQUEST['ord_forn']);
$n_fornitore = mysqli_real_escape_string($link, $_REQUEST['n_fornitore']);
$corriere = mysqli_real_escape_string($link, $_REQUEST['corriere']);
$n_corriere = mysqli_real_escape_string($link, $_REQUEST['n_corriere']);
$riserva = mysqli_real_escape_string($link, $_REQUEST['riserva']);
$marrara = mysqli_real_escape_string($link, $_REQUEST['marrara']);
$note = mysqli_real_escape_string($link, $_REQUEST['note']);
$esito = mysqli_real_escape_string($link, $_REQUEST['esito']);
$rientro = mysqli_real_escape_string($link, $_REQUEST['rientro']);
$spese_forn = mysqli_real_escape_string($link, $_REQUEST['spese_forn']);
$spese_corr = mysqli_real_escape_string($link, $_REQUEST['spese_corr']);
$sostituzione = mysqli_real_escape_string($link, $_REQUEST['sostituzione']);
$n_fatt_orig = mysqli_real_escape_string($link, $_REQUEST['n_fatt_orig']);
$n_storno = mysqli_real_escape_string($link, $_REQUEST['n_storno']);

$sql = "INSERT INTO databasename (n_ordine, cliente, data, paese, operatore, n_spedizione, fornitore, ord_forn, n_fornitore, corriere, n_corriere, riserva, marrara, note, esito, rientro, spese_forn, spese_corr, sostituzione, n_fatt_orig, n_storno) VALUES ('$n_ordine', '$cliente', '$data', '$paese', '$operatore', '$n_spedizione', '$fornitore', '$ord_forn', '$n_fornitore', '$corriere', '$n_corriere', '$riserva', '$marrara', '$note', '$esito', '$rientro', '$spese_forn', '$spese_corr', '$sostituzione', '$n_fatt_orig', '$n_storno')";
if(mysqli_query($link, $sql)){
    echo "OK";
} else{
    echo "ERROR: $sql. " . mysqli_error($link);
}

mysqli_close($link);
?>

然后我创建了这个来显示/编辑表格值

<?php

$link = mysqli_connect("localhost", "", "", "");

if (mysqli_connect_error()) {
    die ("There was an error connecting to the database");
} 

$query="SELECT * FROM databasename";

if ($result = mysqli_query($link, $query)) {
    while ($row=mysqli_fetch_array($result)) {
        echo ("<tr><td>$row[n_ordine]</td>");
        echo ("<td>$row[cliente]</td>");
        echo ("<td>$row[data]</td>");
        echo ("<td>$row[paese]</td>");
        echo ("<td>$row[operatore]</td>");
        echo ("<td>$row[n_spedizione]</td>");
        echo ("<td>$row[fornitore]</td>");
        echo ("<td>$row[ord_forn]</td>");
        echo ("<td>$row[n_fornitore]</td>");
        echo ("<td>$row[corriere]</td>");
        echo ("<td>$row[n_corriere]</td>");
        echo ("<td>$row[riserva]</td>");
        echo ("<td>$row[marrara]</td>");
        echo ("<td>$row[note]</td>");
        echo ("<td>$row[esito]</td>");
        echo ("<td>$row[rientro]</td>");
        echo ("<td>$row[spese_forn]</td>");
        echo ("<td>$row[spese_corr]</td>");
        echo ("<td>$row[sostituzione]</td>");
        echo ("<td>$row[n_fatt_orig]</td>");
        echo ("<td>$row[n_storno]</td>");

echo ("<td><a href=\"edit.php?n_ordine=$row[n_ordine]\">Edit</a></td></tr>");
    }
    echo "</table>";
}
?>

单击“编辑”按钮,我将获得具有正确值的表单

<?php

$link = mysqli_connect("localhost", "", "", "");

if($link === false) {
    die("ERROR " . mysqli_connect_error());
}

$num = $_GET['n_ordine']; 
$query = "SELECT * FROM databasename WHERE n_ordine = '$num'"; 
$result = mysqli_query($link,$query);
$row = mysqli_fetch_array($result);
?>

<form action="edit.php" method="post">

<table>

<tr>
<td>N°Ordine:</td>
<td><input type="text" name="n_ordine" value="<?php echo "$row[n_ordine]"; ?>"></td>
</tr>

<tr>
<td>Cliente:</td>
<td><input type="text" name="cliente" value="<?php echo "$row[cliente]"; ?>"></td>
</tr>

<tr>
<td>Data:</td>
<td><input type="text" name="data" value="<?php echo "$row[data]"; ?>"></td>
</tr>

<tr>
<td>Paese:</td>
<td><input type="text" name="paese" value="<?php echo "$row[paese]"; ?>"></td>
</tr>

<tr>
<td>Operatore:</td>
<td><input type="text" name="operatore" value="<?php echo "$row[operatore]"; ?>"></td>
</tr>

    <tr>
<td>Numero Spedizione:</td>
<td><input type="text" name="n_spedizione" value="<?php echo "$row[n_spedizione]"; ?>"></td>
</tr>

    <tr>
<td>Fornitore:</td>
<td><input type="text" name="fornitore" value="<?php echo "$row[fornitore]"; ?>"></td>
</tr>

    <tr>
<td>Ordine Fornitore:</td>
<td><input type="text" name="ord_forn" value="<?php echo "$row[ord_forn]"; ?>"></td>
</tr>

    <tr>
<td>Note Fornitore:</td>
<td><input type="text" name="n_fornitore" value="<?php echo "$row[n_fornitore]"; ?>"></td>
</tr>

    <tr>
<td>Corriere:</td>
<td><input type="text" name="corriere" value="<?php echo "$row[corriere]"; ?>"></td>
</tr>

    <tr>
<td>Note Corriere:</td>
<td><input type="text" name="n_corriere" value="<?php echo "$row[n_corriere]"; ?>"></td>
</tr>

    <tr>
<td>Riserva:</td>
<td><input type="text" name="riserva" value="<?php echo "$row[riserva]"; ?>"></td>
</tr>

    <tr>
<td>Marrara:</td>
<td><input type="text" name="marrara" value="<?php echo "$row[marrara]"; ?>"></td>
</tr>

    <tr>
<td>Note:</td>
<td><input type="text" name="note" value="<?php echo "$row[note]"; ?>"></td>
</tr>

    <tr>
<td>Esito:</td>
<td><input type="text" name="esito" value="<?php echo "$row[esito]"; ?>"></td>
</tr>

    <tr>
<td>Rientro:</td>
<td><input type="text" name="rientro" value="<?php echo "$row[rientro]"; ?>"></td>
</tr>

    <tr>
<td>Spese da Addebitare al Fornitore:</td>
<td><input type="text" name="ospese_forn" value="<?php echo "$row[spese_forn]"; ?>"></td>
</tr>

    <tr>
<td>Spese da Addebitare al Corriere:</td>
<td><input type="text" name="spese_corr" value="<?php echo "$row[spese_corr]"; ?>"></td>
</tr>

    <tr>
<td>Sostituzioni fornitori / Note di Credito e/o Sconti:</td>
<td><input type="text" name="sostituzione" value="<?php echo "$row[sostituzione]"; ?>"></td>
</tr>

    <tr>
<td>N°Fattura Originaria:</td>
<td><input type="text" name="n_fatt_orig" value="<?php echo "$row[n_fatt_orig]"; ?>"></td>
</tr>

    <tr>
<td>N°Nota a Storno:</td>
<td><input type="text" name="n_storno" value="<?php echo "$row[n_storno]"; ?>"></td>
</tr>

</table>
<input type="submit" value="Update">
</form>

然后我卡住了,因为我尝试在 edit.php 文件中使用这样的查询:

sql = "UPDATE gestionale SET n_ordine = ' ".$n_ordine." ', cliente = ' ".$cliente." ', data = ' ".$data." ', paese = ' ".$paese." ', operatore = ' ".$operatore." ', n_spedizione = ' ".$n_spedizione." ', fornitore = ' ".$fornitore." ', ord_forn = ' ".$ord_forn." ', n_fornitore = ' ".$n_fornitore." ', corriere = ' ".$corriere." ', n_corriere = ' ".$n_corriere." ', riserva = ' ".$riserva." ', marrara = ' ".$marrara." ', note = ' ".$note." ', esito = ' ".$esito." ', rientro = ' ".$rientro." ', spese_forn = ' ".$spese_forn." ', spese_corr = ' ".$spese_corr." ', sostituzione = ' ".$sostituzione." ', n_fatt_orig = ' ".$n_fatt_orig." ', n_storno = ' ".$n_storno." ' WHERE n_ordine = '".$_POST['n_ordine']."'";

if(mysqli_query($link, $sql)){

    echo "Update Done Correctly";

} else{

    echo "ERROR: $sql. " . mysqli_error($link);
}


mysqli_close($link);

而且,当我更新所有内容时,它给了我“正确更新完成”,而不是一个错误,但是,如果我检查 SQL 表,则不会发生任何更改。在“WHERE”条件下可能是非常简单的事情,但我看不到它。我也尝试以这种方式更改代码 sql = "UPDATE gestionale SET n_ordine = ' ".$POST['n_ordine']." ' 等等但相同。

我希望任何人都可以看到我的错误在哪里。

非常感谢

【问题讨论】:

  • 看来你的更新命令不正确。
  • 请更改 .我的意思是,你有 $row = 数组,所以要访问某个键,你应该调用 $row['key'] - key 作为字符串值。

标签: php sql html-table


【解决方案1】:

1) 在将其传递给 SQL 查询之前,您应该清除此值

$num = $_GET['n_ordine']; 
$query = "SELECT * FROM databasename WHERE n_ordine = '$num'"; 

2) 你应该在从数组传递字符串键时使用引号:

$row[n_ordine] -> $row['n_ordine']

3) 您可以尝试使用 PDO 并将参数绑定到查询中,因为您现在这样做的方式并不安全。 https://www.php.net/manual/en/pdostatement.execute.php 它应该工作

4) 如果您不想在最后一个文件中使用 PDO,只需 var_dump($sql); 并粘贴整个查询 - 我们将能够检查出了什么问题。您也可以粘贴 SELECT * from gestionale 的结果 - 这会很有帮助

【讨论】:

  • 您好,谢谢!我必须在edit.php文件中清除它吗?我知道现在不安全,我会尽快查看您的建议!现在我正在尝试解决这个问题):谢谢!
猜你喜欢
  • 1970-01-01
  • 2020-03-25
  • 2010-12-05
  • 2011-10-29
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多