如何将表单数据插入数据库以防止PHP中出现空白记录?

时间:2017-10-22 20:09:41

标签: php mysqli

我对编码并不熟悉,但我需要一个简单的脚本来使用网络存档一些数据,我认为我接近成功,但我发现了一个错误。

使用一些教程,我创建了一个获取一些值的HTML表单,然后转到insert.php页面将它们存储到我已经拥有的数据库中并显示正/负消息。我的问题是,如果你只是浏览这个insert.php页面,你会在数据库中得到一个空白记录。 我知道这不是最安全的方式,但这个PHP脚本不会是#public;#34;。

有没有办法阻止人们浏览该页面或只是避免发送空白记录?

附件是我的网页https://pastebin.com/t1rEgu0F

的pastebin链接

INDEX.PHP页面

<!DOCTYPE html>
<html>
<head>
<title>Programma Vesuviana - Inserimento Scheda US </title>
<link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>

    <div id="header">
        <div id="header_cont">
        <div id="logo"><a href="index.php"><img src="images/logo.png" width="250px" height="60px"></a></div>
        <div id="head_cont2"><h2>Inserimento scheda US - Sintesi</h2></div>
        </div>
    </div>

    <div id="head_margin" style="height:150px;"></div>

    <div id="main">

        <p id="info">Dopo l'invio della scheda compilata, questa non sarà più modificabile. Si prega di controllare i dati inseriti prima dell'invio. In caso di errore, rinviare la scheda con il numero dell'us seguito dalla parola <b>edit</b>. <i>Es. XXXXXXedit</i> </p>
        <br>
<form action="insert.php" method="post">

<!-- Campi di inserimento -->   


<h3>Referenti topografici</h3>
<table>
<!-- Referenti topografici --> 
<tr>
    <th>Sito</th>
    <th>Regione</th>
    <th>CA</th>
    <th>CF</th>
    </tr>
<tr>
<td><input type="text" name="sito"></td>
<td><input type="text" name="regione"></td>
<td><input type="text" name="ca"></td>
<td><input type="text" name="cf"></td>
</tr>
<tr>
    <th>Marciapiede</th>
    <th>Tratta stradale</th>
    <th>Superfici_verticali_2</th>
    <th>SO</th>
    </tr>
<tr>
<tr>
<td><input type="text" name="marciapiede"></td>
<!-- Tratta stradale --> <td><input type="text" name=" "></td>
<td><input type="text" name="superfici_verticali_2"></td>
<td><input type="text" name="SO"></td>
</tr>
<tr>
    <th>Scavo</th>
    <th>Arredo</th>
    <th>superfici_verticali_1</th>
    <th>Struttura</th>
    </tr>
<tr>
<td><input type="text" name="scavo"></td>
<td><input type="text" name="arredo"></td>
<td><input type="text" name="superfici_verticali_1"></td>
<td><input type="text" name="struttura"></td>
</tr>
</table>

<br>
<h3>Scheda</h3>

<table class="normal_inputs">

        <tr>
        <th>US</th>
        <th>definizione e posizione</th>
        </tr>
        <tr>
        <td><input type="text" name="US" required></td>
        <td><input type="text" name="Definizione_posizione" required></td>
            </tr>
            </table>
            <br>
            <table>
    <tr>
    <th>Saggio</th>
    <th>Settore</th>
    <th>Anno</th>
    <th>Quote</th>
    </tr>
    <tr>
        <td><input type="text" name="saggio"></td>
        <td><input type="text" name="settore"></td>
        <td><input type="text" name="anno" required></td>
        <td><input type="text" name="quote"></td>

    </tr>

<tr>
    <th>Uguale a</th>
    <th>Gli si appoggia</th>
    <th>Coperto da</th>
    <th>Tagliato da</th>
    <th>Riempito da</th>
    <th>anteriore a</th>
    </tr>
    <tr>
        <td><input type="text" name="Uguale_a"></td>
        <td><input type="text" name="Gli_si_appoggia"></td>
        <td><input type="text" name="Coperto_da"></td>
        <td><input type="text" name="Tagliato_da"></td>
        <td><input type="text" name="Riempito_da"></td>
<!-- Anteriore a -->        <td><input type="text" name=""></td>
    </tr>
    <tr>
        <th>Si lega a</th>
        <th>Si appoggia a</th>
        <th>Copre</th>
        <th>Taglia</th>
        <th>Riempie</th>
        <th>Posteriore a</th>
    </tr>   
    <tr>
            <td><input type="text" name="Si_lega_a"></td>
            <td><input type="text" name="Si_appoggia_a"></td>
            <td><input type="text" name="Copre"></td>
            <td><input type="text" name="Taglia"></td>
            <td><input type="text" name="Riempie"></td>
            <!-- Posteriore a --><td><input type="text" name=""></td> 
    </tr>
</table>
<br>

<table class="tabella">
    <tr><th>Descrizione</th></tr>
    <tr><td><textarea type="text" class="textarea" name="descrizione"></textarea></td></tr>

</table>
<br>
<table class="tabella">
    <tr><th>Interpretazione</th></tr>
    <tr><td><textarea type="text" class="interpretazione" name="interpretazione"></textarea></td></tr>
</table>

<br>
<h3>Dati del compilatore</h3>

<table class="tabella">
        <tr>
        <th>Nome e Cognome</th>
        <th>Data di inserimento</th>
        </tr>
        <tr>
        <td><input type="text" name="" ></td>
        <td><input type="text" name="" ></td>
            </tr>
    </table>

<br>
<input type="submit" value="Invia Scheda US"><br>
</form>
</div>
<div id="disclaimer">I dati inseriti nel programma appartengono solo al programma Vesuviana ecc ecc</div>
</body>
</html>

INSERT.php页面

<!DOCTYPE html>
<html>
<head>
<title>Programma Vesuviana - Inserimento Scheda US </title>
<link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
    <div id="header">
        <div id="header_cont">
        <div id="logo"><img src="images/logo.png" width="250px" height="60px"></div>
        <div id="head_cont2"><h2>Inserimento scheda US - Sintesi</h2></div>
        </div>
    </div>

    <div id="head_margin" style="height:150px;"></div>
    <div id="main">
<?php

require 'database.php';
$conn    = Connect();
$US    = $conn->real_escape_string($_POST['US']);
$sito   = $conn->real_escape_string($_POST['sito']);
$regione    = $conn->real_escape_string($_POST['regione']);
$ca = $conn->real_escape_string($_POST['ca']);
//$tratta stradale = $conn->real_escape_string($_POST['tratta stradale']);
$superfici_verticali_2 = $conn->real_escape_string($_POST['superfici_verticali_2']);
$SO = $conn->real_escape_string($_POST['SO']);
$scavo = $conn->real_escape_string($_POST['scavo']);
$arredo = $conn->real_escape_string($_POST['arredo']);
$superfici_verticali_1 = $conn->real_escape_string($_POST['superfici_verticali_1']);
$struttura = $conn->real_escape_string($_POST['struttura']);
$Definizione_posizione = $conn->real_escape_string($_POST['Definizione_posizione']);
$saggio = $conn->real_escape_string($_POST['saggio']);
$settore = $conn->real_escape_string($_POST['settore']);
$anno = $conn->real_escape_string($_POST['anno']);
$quote = $conn->real_escape_string($_POST['quote']);
$Uguale_a = $conn->real_escape_string($_POST['Uguale_a']);
$Gli_si_appoggia = $conn->real_escape_string($_POST['Gli_si_appoggia']);
$Coperto_da = $conn->real_escape_string($_POST['Coperto_da']);
$Tagliato_da = $conn->real_escape_string($_POST['Tagliato_da']);
$Riempito_da = $conn->real_escape_string($_POST['Riempito_da']);
//$Anteriore_a = $conn->real_escape_string($_POST['Anteriore_a']);
$Si_lega_a = $conn->real_escape_string($_POST['Si_lega_a']);
$Si_appoggia_a = $conn->real_escape_string($_POST['Si_appoggia_a']);
$Copre = $conn->real_escape_string($_POST['Copre']);
$Taglia = $conn->real_escape_string($_POST['Taglia']);
$Riempie = $conn->real_escape_string($_POST['Riempie']);
//$Posteriore_a = $conn->real_escape_string($_POST['Posteriore_a']);
$descrizione = $conn->real_escape_string($_POST['descrizione']);
$interpretazione = $conn->real_escape_string($_POST['interpretazione']);


$query   = "INSERT into Scheda_US (US,sito,regione,ca,superfici_verticali_2,SO,scavo,arredo,superfici_verticali_1,struttura,Definizione_posizione,saggio,settore,anno,quote,Uguale_a,Gli_si_appoggia,Coperto_da,Tagliato_da,Riempito_da,Si_lega_a,Si_appoggia_a,Copre,Taglia,Riempie,descrizione,interpretazione) VALUES(
'" . $US . "',
'" . $sito . "',
'" . $regione . "',
'" . $ca . "',
'" . $superfici_verticali_2 . "',
'" . $SO . "','" . $scavo . "',
'" . $arredo . "',
'" . $superfici_verticali_1 . "',
'" . $struttura . "',
'" . $Definizione_posizione . "',
'" . $saggio . "',
'" . $settore . "',
'" . $anno . "','" . $quote . "',
'" . $Uguale_a . "',
'" . $Gli_si_appoggia . "',
'" . $Coperto_da . "',
'" . $Tagliato_da . "',
'" . $Riempito_da . "',
'" . $Si_lega_a . "',
'" . $Si_appoggia_a . "',
'" . $Copre . "',
'" . $Taglia . "',
'" . $Riempie . "',
'" . $descrizione . "',
'" . $interpretazione . "')";
$success = $conn->query($query);

if (!$success) {
    die("Scheda non inserita".$conn->error);

}

echo '<div id="success"><p>Scheda inserita.</p>

</div>';

$conn->close();

?>

<p style="text-align:right;"><a style="text-decoration: none; color: black; font-size:15pt;" href="index.php">&#8592; Torna indietro</a></p>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

执行服务器端验证不要信任客户端验证

我在处理表单时使用AJAX。我所做的是当用户点击提交按钮时,我首先使用以下方法检查所需字段是否为空

if(isset($_POST['sitio']) || !empty($_POST['sitio'])) {
    // your query here
}
else {
    // validation failed. You can use die() if you want to use ajax and JSON
    // Here's my example
    die(json_encode(["valid"=>0, "message"=>"Sitio is empty!"]));
}

您还可以使用strlen等一些验证来检查用户的输入长度和其他验证,例如filter_var($var, FILTER_VALIDATE_EMAIL)验证输入是否为电子邮件格式。

此外,在使用bind_param命令时使用INSERT。您的项目很容易进行SQL注入。我对mysqli了解不多,因为我经常使用PDO,我喜欢使用它。希望这会有所帮助。