duanhua5523 2017-06-05 10:57
浏览 143
已采纳

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

Ok so I am trying to make a form for a prison visit. I must save in an oracle database the first name, last name, id, inmate code, relationship, purpose of visit, date, hour and the name of the image. Moreover I need to upload the image on the server. I have made 2 forms and 2 php files that process the 2 forms and a javascript button that send them both but the second PHP which works with the picture loads after the first one so the errors from the first one don't appear. I tried to put the code from the second in the first one but still nothing. Can you please tell me how can I do to solve this? Below there is my code. The name of the inputs are in romanian but if you look at the form and the php you will figure it out. Here is the code:

Html file:

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

Php for form1:

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

Php for form2 (aka picture upload form):

<?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条回答 默认 最新

  • douniwan_0025 2017-06-05 11:32
    关注

    There is no need to use two html forms and two php files. You can combine two forms. Just add enctype="multipart/form-data" when you are transmitting files using html form as you have done in the 2nd form. It will look like this -

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

    After this combine your PHP logic in one file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条