簡體   English   中英

需要進行以下驗證:在mySQL數據庫中將base64插入BLOB

[英]Clerification needed re: INSERT base64 to BLOB in mySQL database

我正在使用畫布元素在協議表單上收集簽名。 在提交表單之前,canvas元素將轉換為base64字符串,然后將其存儲在HIDDEN表單字段中。

當base64字符串命中正在處理的PHP頁面並將表單元素插入到我的數據庫中時,它看起來如下所示:

data:image/jpeg;base64,/9j/4AAQSkZJ......RgABAQAAS

我已經嘗試了許多不同的ENCODE和DECODE組合... ESCAPE_STRING ...獲取文件內容...以便將此base64字符串保存到我的數據庫中,但是數據從未進入數據庫。

我的將這種表格插入到數據庫中的PHP當前如下所示:

<?php 
include ('header.php');
include ('connect.php');
// prepare and bind
$stmt = $conn->prepare("INSERT INTO deposit (type, amount, created, agreement, signature, project_id) VALUES (?,?,?,?,?,?)");
$stmt->bind_param("ssssbs", $type, $amount, $created, $agreement, $signature, $project_id);
// set parameters
$created = date("Y-m-d H:i:s");
$amount = htmlspecialchars($_POST['amount']);
$type = htmlspecialchars($_POST['type']);
$agreement = $_POST['agreement'];


$hidden_data = $_POST['hidden_data'];
$escaped = mysqli_real_escape_string ($hidden_data);
$signature = base64_decode($escaped);


$project_id = $_POST['project_id'];
// execute 
$stmt->execute();
$deposit_id = ($stmt->insert_id);
echo "$amount - created successfully";
$stmt->close();
?>
<form action="project.php" method="post">
    <div class="wrapper">   
        <input name="project_id" value="<?php echo $project_id; ?>" type="hidden">
        <input type="submit" value="Go back to project">
    </div>  
</form>
<?php 
include ('disconnect.php');
include ('footer.php');
?>

如果我正確理解該過程,則需要:

1)從字符串的開頭刪除“ data:image / jpeg; base64”

2)轉換或解碼或編碼剩余數據

3)用INSERT將數據保存到BLOB

我在此過程中所獲得的信息中的許多內容看起來與此處的代碼設置有很大不同,以至於我不了解如何重新組織代碼以適合此處的內容。 有人可以為我澄清一下此過程在我在這里使用的CODE結構類型中如何工作嗎?

我在這個應用程序中確實還有另一個地方,可以從照相機收集圖像,將其存儲在文件中,並使用$ _FILE而不是$ _POST進行處理。 此過程有效,圖像存儲在數據庫BLOB中,以后我可以獲取信息以進行顯示。 該代碼如下所示:

$data = $conn->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));

我嘗試只是將$ _FILES更改為$ _POST,但這一切都中斷了。

預先感謝您的寶貴時間。

編輯1:

這是收集此處使用的數據的表格:

<?php 
include ('header.php');
include ('legal_deposit_agreement_display.php');
?>
<form action="legal_deposit_agreement_create.php" method="post" enctype="multipart/form-data" id="form" name="form">

    <input name="hidden_data" id='hidden_data' type="hidden"/>
    <input name="project_id" value="<?php echo $project_id; ?>" type="hidden">

    <h1>Deposit</h1>

    <div class="wrapper">
        <label>Ammount of Deposit</label><br>
        <input name="amount" value="<?php echo $amount; ?>" placeholder="Enter Ammount of deposit here" type="text" tabindex="1" required>
    </div>

    <div class="wrapper">   
        <label>Payment Type</label><br>
        <select name="type" tabindex="3">
            <option value="Cash/Credit"<?php if ($type == 'Cash/Credit') echo ' selected="selected"'; ?>>Cash/Credit</option>
            <option value="Gift Certificate"<?php if ($type == 'Gift Certificate') echo ' selected="selected"'; ?>>Gift Certificate</option>
        </select>
    </div>

    <div class="wrapper">
        <label>Deposit Agreement</label><br>
        <textarea id="agreement" name="agreement" style="overflow:hidden" TextMode="MultiLine" onkeyup="setHeight('textBox1');" onkeydown="setHeight('textBox1');" tabindex="4" value="<?php include 'text/depositAgreement.txt';?>"><?php include 'text/depositAgreement.txt';?></textarea>
        <script type="text/javascript">
            function setHeight(fieldId){
                document.getElementById(fieldId).style.height = document.getElementById(fieldId).scrollHeight+'px';
            }
            setHeight('agreement');
        </script>
    </div>

    <div id="signature-pad" class="signature-pad">
        <div class="signature-pad--body">
          <canvas id="canvas" name="canvas"></canvas>
        </div>
        <div class="signature-pad--footer">
          <div class="description">Sign above</div>

          <div class="signature-pad--actions">
            <div>
              <button type="button" class="button clear" data-action="clear">Clear</button>
            </div>
          </div>

        </div>
    </div>

    <script src="js/signature_pad.js"></script>
    <script src="js/app.js"></script>

    <div class="wrapper">
    <input type="button" onclick="convert()" value="I AGREE TO ALL OF THE TERMS DESCRIBED ABOVE">
    </div>

</form>

<script>
    function convert() {
        document.getElementById('hidden_data').value = canvas.toDataURL('image/jpeg', 0.5);
        document.forms["form"].submit();
    };
</script>

<?php include ('footer.php');?>

文件/ $_FILES<form>需要一個有效的enctype,即enctype="multipart/form-data" ,這不屬於您發布的內容。

那里應該有file的輸入類型,並使用它的named屬性,然后將該變量傳遞給$_FILES超全局變量。 然后,可以像在bind_param()一樣使用b

此行不包含您帖子中輸入的文件:

$data = $conn->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));

如果要將BLOB上傳到數據庫,請使用我在此處發布的內容並閱讀有關處理文件的手冊。

此行失敗,因為您沒有為其傳遞連接參數:

$escaped = mysqli_real_escape_string ($hidden_data);

內容應為:

$escaped = mysqli_real_escape_string ($conn, $hidden_data);

暫無
暫無

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

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