如何创建一个在数据库中保存文本并在服务器上上传文件的表单

时间:2017-06-05 10:57:22

标签: javascript php jquery html5 forms

好的,我正试图为监狱访问制作表格。我必须在oracle数据库中保存名字,姓氏,id,囚犯代码,关系,访问目的,日期,小时和图像名称。此外,我需要在服务器上上传图像。我已经制作了2个表单和2个php文件来处理2个表单和一个javascript按钮,它们都发送它们,但第二个PHP与图片一起加载第一个,所以第一个的错误不会出现。我试图把第二个代码放在第一个但仍然没有。你能告诉我怎样才能解决这个问题吗?下面是我的代码。输入的名称是罗马尼亚语,但如果你看一下表格和php,你就会想出来。这是代码:

Html文件:

<!DOCTYPE html>
<html>
<head>
    <title>CerereVizita</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="cerereV.css">
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0">
    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="jquery.min.js"></script>
    <script src="jquery-ui.min.js"></script>

    <script>
        $(document).ready(function() {
            $("#datepicker").datepicker();
        });
    </script>
</head>

<body>
<div class="Container">
    <img src="persoana2.png" alt="Avatar" class="avatar">
    <div class="Container2">

        <iframe name="votar" style="display:none;"></iframe>
        <form action="cerereVizita.php" method="POST" id="form1" target="votar">


            <label><b>Nume:</b></label>
            <input type="text" placeholder="Ex: Popescu" name="last name" required>



            <label><b>Prenume:</b></label>
            <input type="text" placeholder="Ex: Marius" name="prenume" required>



            <label><b>CNP:</b></label>
            <input type="text" placeholder="Ex: 1900101125356" name="cnp" required>

            <label><b>Cod Detinut:</b></label>
            <input type="text" placeholder="Ex: 1234" name="cod-detinut" required>

            <label><b>Tipul de relatie:</b></label>
            <input type="text" placeholder="Ex: Sot" name="relatie" required>



            <label><b>Natura vizitei:</b></label>
            <input type="text" placeholder="Ex: Discutie" name="natura-vizitei" required>


            <label><b>Data:</b></label>
            <input id="datepicker" name="data" required>

            <label for="myList"> <b>Ora:</b></label>
            <div>
                <select name="ora" id = "myList" form="form1" required>
                    <option value = "12:00">12:00</option>
                    <option value = "13:00">13:00</option>
                    <option value = "14:00">14:00</option>
                    <option value = "15:00">15:00</option>
                </select>
            </div>

        </form>

        <form action="testU.php" method="post" enctype="multipart/form-data" target="votar">

            <label> <b>Poza:</b>
                <input type="file" name="fileToUpload" id="fileToUpload">
            </label>

        </form>

        <button onclick="submitForms()">Submit!</button>
    </div>
    <div class="item">
        <h3> Va rugam completati toate spatiile si verificati de 2 ori inainte sa trimiteti! </h3>
    </div>

    <div class="Container3">
        <a href="faq.html">FAQ</a>
        <a href="contact.html">Contact</a>
    </div>
    <img class="Poza" src="asd.jpg" alt="Prison image">
</div>
</body>

</html>

<script language="javascript">
    function submitForms(){
        $('form').submit();
    }
</script>

form1的Php:

<?php
//Oracle DB user name
$username = 'TW';

// Oracle DB user password
$password = 'TW';

// Oracle DB connection string
$connection_string = 'localhost/xe';

//Connect to an Oracle database
$connection = oci_connect(
    $username,
    $password,
    $connection_string
);

$var1 = $_POST['nume'];
$var2 = $_POST['prenume'];
$var3 = $_POST['cnp'];
$var4 = $_POST['cod-detinut'];
$var5 = $_POST['relatie'];
$var6 = $_POST['natura-vizitei'];
$var7 = $_POST['data'];
$var8 = $_POST['ora'];

if(is_numeric($var4)==true) {
//stid4
    $stid4 = oci_parse($connection, 'SELECT COUNT(ID) as "count" FROM DETINUTI WHERE ID=:codD');

    if (!$stid4) {
        $e = oci_error($connection);
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }

    oci_bind_by_name($stid4, ':codD', $var4);
    $r4 = oci_execute($stid4);
    if (!$r4) {
        $e = oci_error($stid4);
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }
    $row4 = oci_fetch_array($stid4, OCI_ASSOC + OCI_RETURN_LOBS);
    $count2 = $row4['count'];

    if (is_numeric($var3) == true AND strlen($var3) == 13) {

//stid2
        $stid2 = oci_parse($connection, 'SELECT COUNT(*) AS "count" FROM CEREREVIZITE WHERE CNP = :cnp AND DATA_VIZITA = to_date(:data_vizita,\'MM/DD/YYYY\')');

        if (!$stid2) {
            $e = oci_error($connection);
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
        oci_bind_by_name($stid2, ':cnp', $var3);
        oci_bind_by_name($stid2, ':data_vizita', $var7);

        $r2 = oci_execute($stid2);
        if (!$r2) {
            $e = oci_error($stid2);
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
        $row2 = oci_fetch_array($stid2, OCI_ASSOC + OCI_RETURN_LOBS);
        $count = $row2['count'];

//stid3

        $stid3 = oci_parse($connection, 'SELECT MAX(ID) AS "ID" FROM CEREREVIZITE');

        if (!$stid3) {
            $e = oci_error($connection);
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
        $r3 = oci_execute($stid3);
        if (!$r3) {
            $e = oci_error($stid3);
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
        }
        $row3 = oci_fetch_array($stid3, OCI_ASSOC + OCI_RETURN_LOBS);
        $max = $row3['ID'];

        if ($count2 == 0) {
            $message = "Detinutul cu id-ul " . $var4 . " nu exista.";
            echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
        } else {
            if ($count == 0) {

                //stid1

                $stid1 = oci_parse($connection, 'INSERT INTO CEREREVIZITE VALUES (:id+1,:nume,:prenume,:cnp,:cod_detinut,:relatie,:natura,to_date(:data_vizita,\'MM/DD/YYYY\'),:ora,\'poza\')');

                if (!$stid1) {
                    $e = oci_error($connection);
                    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
                }


                oci_bind_by_name($stid1, ':id', $max);
                oci_bind_by_name($stid1, ':nume', $var1);
                oci_bind_by_name($stid1, ':prenume', $var2);
                oci_bind_by_name($stid1, ':cnp', $var3);
                oci_bind_by_name($stid1, ':cod_detinut', $var4);
                oci_bind_by_name($stid1, ':relatie', $var5);
                oci_bind_by_name($stid1, ':natura', $var6);
                oci_bind_by_name($stid1, ':data_vizita', $var7);
                oci_bind_by_name($stid1, ':ora', $var8);

                $r1 = oci_execute($stid1);
                if (!$r1) {
                    $e = oci_error($stid1);
                    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
                }

                $message = "Datele au fost inregistrate cu succes. ";
                echo "<script type='text/javascript'>alert('$message'); window.location.href = \"guest.html\";</script>";
            } else {

                $message = "Aveti deja o vizita inregistrata in data de " . $var7;
                echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
            }
        }
    } else {
        $message = "CNP invalid.";
        echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
    }
}
else{
    $message = "Cod invalid! ";
    echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
}
oci_close($connection);
?>

forp2 for form2(又名图片上传表格):

<?php
$target_dir = "C:/Apache24/htdocs/dwnds/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $message = "File is an image - " . $check["mime"] . ".";
        echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
        $uploadOk = 1;
    } else {
        $message = "File is not an image.";
        echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    $message = "Sorry, file already exists.";
    echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    $message = "Sorry, your file is too large.";
    echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
    $message = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    $message = "Sorry, your file was not uploaded.";
    echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        $message = "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        echo "<script type='text/javascript'>alert('$message'); window.location.href = \"guest.html\";</script>";
    } else {
        $message = "Sorry, there was an error uploading your file.";
        echo "<script type='text/javascript'>alert('$message'); window.location.href = \"cerereV.html\";</script>";
    }
}
?>

1 个答案:

答案 0 :(得分:1)

无需使用两个html表单和两个php文件。您可以组合两种形式。只需在使用html表单传输文件时添加enctype="multipart/form-data",就像在第二种表单中一样。它看起来像这样 -

<form action="cerereVizita.php" method="POST" id="form1" target="votar" enctype="multipart/form-data">

  <label><b>Nume:</b></label>
  <input type="text" placeholder="Ex: Popescu" name="last name" required>

  <label><b>Prenume:</b></label>
  <input type="text" placeholder="Ex: Marius" name="prenume" required>

  <label><b>CNP:</b></label>
  <input type="text" placeholder="Ex: 1900101125356" name="cnp" required>

  <label><b>Cod Detinut:</b></label>
  <input type="text" placeholder="Ex: 1234" name="cod-detinut" required>

  <label><b>Tipul de relatie:</b></label>
  <input type="text" placeholder="Ex: Sot" name="relatie" required>

  <label><b>Natura vizitei:</b></label>
  <input type="text" placeholder="Ex: Discutie" name="natura-vizitei" required>


  <label><b>Data:</b></label>
  <input id="datepicker" name="data" required>

  <label for="myList"> <b>Ora:</b></label>
  <div>
    <select name="ora" id="myList" form="form1" required>
        <option value = "12:00">12:00</option>
        <option value = "13:00">13:00</option>
        <option value = "14:00">14:00</option>
        <option value = "15:00">15:00</option>
    </select>
  </div>

  <label> <b>Poza:</b></label>
  <input type="file" name="fileToUpload" id="fileToUpload">
</form>

之后将PHP逻辑合并到一个文件中。