简体   繁体   English

需要进行以下验证:在mySQL数据库中将base64插入BLOB

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

I am collecting a signature on an agreement form with a canvas element. 我正在使用画布元素在协议表单上收集签名。 Before the form is submitted the canvas element is converted to a base64 string that is then stored in a HIDDEN form field. 在提交表单之前,canvas元素将转换为base64字符串,然后将其存储在HIDDEN表单字段中。

When the base64 string hits the PHP page that is processing and INSERT-ing the form elements into my database it looks like the following: 当base64字符串命中正在处理的PHP页面并将表单元素插入到我的数据库中时,它看起来如下所示:

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

I have tried many different combos of ENCODE and DECODE... ESCAPE_STRING ... GET FILE CONTENTS... to get this base64 string saved into my database, but the data never makes it to the database. 我已经尝试了许多不同的ENCODE和DECODE组合... ESCAPE_STRING ...获取文件内容...以便将此base64字符串保存到我的数据库中,但是数据从未进入数据库。

My PHP that is INSERT-ing this form into my database currently looks like: 我的将这种表格插入到数据库中的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');
?>

If I am understanding the process correctly, I need to: 如果我正确理解该过程,则需要:

1) strip the "data:image/jpeg;base64," from the beginning of the string 1)从字符串的开头删除“ data:image / jpeg; base64”

2) convert or decode or encode the remaining data 2)转换或解码或编码剩余数据

3) save the data to the BLOB with an INSERT 3)用INSERT将数据保存到BLOB

Much of what I have found for information on this process looks so much different from how my code here is set up that I am not understanding how to re-organize it to fit within what I have here. 我在此过程中所获得的信息中的许多内容看起来与此处的代码设置有很大不同,以至于我不了解如何重新组织代码以适合此处的内容。 Can someone please clarify for me how this process works within the type of CODE structure I am using here? 有人可以为我澄清一下此过程在我在这里使用的CODE结构类型中如何工作吗?

I have do have another place in this app that collects an image from a camera, stores it in a file and is handled using $_FILE instead of $_POST. 我在这个应用程序中确实还有另一个地方,可以从照相机收集图像,将其存储在文件中,并使用$ _FILE而不是$ _POST进行处理。 This process works, the image is stored in the database BLOB and I am able to GET the info later for display. 此过程有效,图像存储在数据库BLOB中,以后我可以获取信息以进行显示。 That code looks like: 该代码如下所示:

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

I tried just changing $_FILES to $_POST but that just broke everything. 我尝试只是将$ _FILES更改为$ _POST,但这一切都中断了。

Thank you in advance for your time. 预先感谢您的宝贵时间。

EDIT 1: 编辑1:

Here is the form that collects the data used here: 这是收集此处使用的数据的表格:

<?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 / $_FILES require a valid enctype in <form> , being enctype="multipart/form-data" and that isn't part of what you posted. 文件/ $_FILES<form>需要一个有效的enctype,即enctype="multipart/form-data" ,这不属于您发布的内容。

There should be an input type of file in there and using the named attribute for it, then passing that variable assigned to the $_FILES superglobal. 那里应该有file的输入类型,并使用它的named属性,然后将该变量传递给$_FILES超全局变量。 You can then use the b as you did in your bind_param() . 然后,可以像在bind_param()一样使用b

This line doesn't contain the file input for it in your post: 此行不包含您帖子中输入的文件:

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

If you want to upload a BLOB to your database, then use what I posted here and read the manual on handling files. 如果要将BLOB上传到数据库,请使用我在此处发布的内容并阅读有关处理文件的手册。

This line failed because you didn't pass the connection argument for it: 此行失败,因为您没有为其传递连接参数:

$escaped = mysqli_real_escape_string ($hidden_data);

which should read as: 内容应为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM