如何将JavaScript数据插入mysql数据库?

时间:2013-06-29 02:31:13

标签: php javascript

我想要插入的数据是blob或文件 这是针对学校项目的,讲师说要暂时将其插入数据库。

这就是我现在所拥有的

function stop() {
  cancelAnimationFrame(rafId);
  endTime = Date.now();
  $('#stop-me').disabled = true;
  document.title = ORIGINAL_DOC_TITLE;

  toggleActivateRecordButton();

  console.log('frames captured: ' + frames.length + ' => ' +
              ((endTime - startTime) / 1000) + 's video');

  embedVideoPreview();
};

function embedVideoPreview(opt_url) {
  var url = opt_url || null;
  var video = $('#video-preview video') || null;
  var downloadLink = $('#video-preview a[download]') || null;

  if (!video) {
    video = document.createElement('video');
    video.autoplay = true;
    video.controls = true;
    video.loop = true;
    //video.style.position = 'absolute';
    //video.style.top = '70px';
    //video.style.left = '10px';
    video.style.width = canvas.width + 'px';
    video.style.height = canvas.height + 'px';
    $('#video-preview').appendChild(video);

    downloadLink = document.createElement('a');
    downloadLink.download = 'capture.webm';
    downloadLink.textContent = '[ download video ]';
    downloadLink.title = 'Download your .webm video';
    var p = document.createElement('p');
    p.appendChild(downloadLink);

    $('#video-preview').appendChild(p);

  } else {
    window.URL.revokeObjectURL(video.src);
  }

  if (!url) {
    var webmBlob = Whammy.fromImageArray(frames, 1000 / 60);
    url = window.URL.createObjectURL(webmBlob);
  }

  video.src = url;
  downloadLink.href = url;

这就是我从同一页面插入数据库的方式,我也不确定视频blob的创建位置。

  <?php
    require("connect.php");
    $namey = video;
    $up = mysql_query("INSERT INTO video VALUES ($namey)");
  ?>

1 个答案:

答案 0 :(得分:0)

好的,所以说你有一个输入供用户上传视频......

<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="VideoToUpload" />
    <input type="submit" value="Upload Video" />
</form>

然后在您的新upload.php中,您需要处理并保存文件

$allowedExts = array("mp4", "mov");
$extension = end(explode(".", $_FILES["VideoToUpload"]["name"]));
if (($_FILES["VideoToUpload"]["type"] == "video/mp4") || ($_FILES["VideoToUpload"]["type"] == "video/quicktime")):
    if($_FILES["VideoToUpload"]["error"] > 0):
        echo "Error: " . $_Files["VideoToUpload"]["error"];
    else:
        move_uploaded_file($_FILES["VideoToUpload"]["tmp_name"], dirname(__FILE__) . "/FolderWhereIWantMyVideoSaved/" . $_FILES["VideoToUpload"]["name"]);
    endif;
endif;
$VideoURL = "http://domain/and/path/to/FolderWhereIWantMyVideoSaved/" . $_FILES["VideoToUpload"];
$mysqli = new mysqli('ip.of.data.base', 'DatabaseName', 'Password', 'Username');    
$stmt = $mysqli->prepare("INSERT INTO Videos (Name, Type, URL) VALUES (?,?,?)") or die ($mysqli->error);
$stmt->bind_param('sss', $_FILES["VideoToUpload"]["name"],$_FILES["VideoToUpload"]["type"], $VideoURL);
$stmt->execute() or die ($mysqli->error);
$stmt->close();
$mysqli->close();

现在文件保存在服务器上,其中的URL在数据库中。所以你要做的就是显示视频。

$mysqli = new mysqli('ip.of.data.base', 'DatabaseName', 'Password', 'Username');    
$stmt = $mysqli->prepare("SELECT Name, Type, URL FROM Videos WHERE ID=?") or die ($mysqli->error);
$stmt->execute() or die($mysqli->error);
$stmt->bind_result($Name, $Type, $URL);
$stmt->store_result();
$stmt->fetch();
...
<video width="320" height="240" controls>
    <source src="<?= $URL ?>" type="<?= $Type ?>">
    Your browser does not support the video tag.
</video>
...
$stmt->close();
$mysqli->close();