簡體   English   中英

如何將表單數據插入到數據庫中以防止 PHP 中的空白記錄?

[英]How to insert form data into the database preventing blank records in PHP?

我對編碼不是很熟悉,但我需要一個簡單的腳本來使用網絡存檔一些數據,我以為我已經接近成功了,但我發現了一個錯誤。

使用一些教程,我創建了一個 HTML 表單,它獲取一些值,然后轉到 insert.php 頁面將它們存儲到我已經擁有的數據庫中並顯示正面/負面消息。 我的問題是,如果你只是瀏覽這個 insert.php 頁面,你會在數據庫中得到一條空白記錄。 我知道這不是最安全的方法,但是這個 php 腳本不會是“公開的”。

有沒有辦法阻止人們瀏覽該頁面或避免它發送空白記錄?

附件是我的頁面的 pastebin 鏈接https://pastebin.com/t1rEgu0F

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>

進行服務器端驗證 不要相信客戶端驗證

我在處理表單時使用 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)驗證輸入是否為電子郵件格式。

而且,在使用INSERT命令時使用bind_param 您的項目容易受到 SQL 注入。 我對mysqli不太了解,因為我mysqli使用PDO並且我喜歡使用它。 希望這可以幫助。

暫無
暫無

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

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