我正在使用 Canvas 元素收集协议(protocol)表单上的签名。在提交表单之前,canvas 元素被转换为 base64 字符串,然后存储在隐藏表单字段中。
当 base64 字符串到达正在处理表单元素并将其插入到我的数据库中的 PHP 页面时,它看起来如下所示:
data:image/jpeg;base64,/9j/4AAQSkZJ......RgABAQAAS
我已经尝试了许多不同的编码和解码组合...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
我发现的有关此过程的大部分信息看起来与我此处代码的设置方式大不相同,以至于我不了解如何重新组织它以适应此处的内容。有人可以为我澄清这个过程是如何在我在这里使用的代码结构类型中工作的吗?
我在这个应用程序中确实有另一个地方,它从相机收集图像,将其存储在文件中,并使用 $_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
在那里并为其使用命名属性,然后将该变量传递给 $_FILES
超全局的。然后您可以使用 b
正如您在 bind_param()
中所做的那样.
此行不包含在您的帖子中为其输入的文件:
$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);
关于php - 需要重新认证 : INSERT base64 to BLOB in mySQL database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48406055/