Scriptcam base64使用jquery post发送到db

时间:2013-12-12 19:14:45

标签: javascript php jquery base64

此代码可让您在按下F按钮时在倒计时上拍照。我正在尝试将图像base64代码保存到我的数据库。我只收到500错误,但我不知道。 这是js:

         $(document).ready(function() {
            var max_time = 2;
            var cinterval;
            $("#webcam").scriptcam({
                width: 660,
                height: 460,
                showMicrophoneErrors:false,
                onError:onError,
                cornerRadius:20,
                cornerColor:'e3e5e2',
                onWebcamReady:onWebcamReady,
                uploadImage:'upload.gif',
                onPictureAsBase64:base64_tofield_and_image
            });

            $(document).keydown(function (eventObj){
                if (event.keyCode ==70){
                countdown_timer();
                }   
            });

            function base64_tofield() {
                $('#formfield').val($.scriptcam.getFrameAsBase64());
            };
            function base64_toimage() {
                $('#image').attr("src","data:image/png;base64,"+$.scriptcam.getFrameAsBase64());
            };
            function base64_tofield_and_image(b64) {
                $('#formfield').val(b64);
                $('#image').attr("src","data:image/png;base64,"+b64);
            };
            function changeCamera() {
                $.scriptcam.changeCamera($('#cameraNames').val());
            };
            function onError(errorId,errorMsg) {
                $( "#btn1" ).attr( "disabled", true );
                $( "#btn2" ).attr( "disabled", true );
                alert(errorMsg);
            };          
            function onWebcamReady(cameraNames,camera,microphoneNames,microphone,volume) {
                $.each(cameraNames, function(index, text) {
                    $('#cameraNames').append( $('<option></option>').val(index).html(text) )
                }); 
                $('#cameraNames').val(camera);
            };
            function countdown_timer(){
                cinterval = setInterval(function(){
                    max_time--;
                    document.getElementById('countdown').innerHTML = max_time;
                    if(max_time == 0){
                        max_time = 2;
                        document.getElementById('countdown').innerHTML = max_time;
                        clearInterval(cinterval);
                        base64_toimage();
                        post($.scriptcam.getFrameAsBase64());
                    }
                },1000)
            };
            function post(base64foto) {
                var strObj = JSON.stringify(base64foto);
                console.log(strObj);
                $.ajax ({
                  type:"POST",
                  url:"post.php",
                  contentType: "application/json",
                  dataType: "json",
                  async: false,
                  data: {"data": strObj},
                  success: function(){ alert("success")},
                  error: function(){ alert("error")}
                });
            };
        });

这是PHP部分:

<?php
header('Content-type: application/json');

$foto = json_decode ($_POST['data']);

$dbhost='********';
$dbuser='********';
$dbpass='********';
$dbname='********';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname);

mysql_query("INSERT INTO fotos (id,foto) VALUES ('','$foto')")or die(mysql_error()); 
?>

0 个答案:

没有答案