Hello, I would like to know how to do a mysql insert, but the way I did it, I just used the javascript just to change the imput URL to the File, that is, I am trying to work on the same imput name, however I have doubts what to do in the case if the person does not want to use the upload and just insert the url, or otherwise, below the code, it may not be correct, if someone can help me, thank you.
$("#pre").click(function() {
var icon = $(this).find("i");
$("#jc_preview").fadeOut("fast");
if (icon.hasClass("fa-cloud-upload")) {
$(this).fadeOut("fast", function() {
icon.removeClass("fa-cloud-upload").addClass("fa-chain");
$("#jc_preview").prop("type", "file").fadeIn("fast");
$(this).fadeIn("fast");
});
} else {
$(this).fadeOut("fast", function() {
icon.removeClass("fa-chain").addClass("fa-cloud-upload");
$("#jc_preview").prop("type", "url").fadeIn("fast");
$(this).fadeIn("fast");
});
}
});
<?php
if (isset($_POST['create'])) {
$subtitle = mysqli_real_escape_string($conn, $_POST['subtitle']);
$preview = mysqli_real_escape_string($conn, $_POST['preview']);
$preview = mysqli_real_escape_string($conn, $_FILES["preview"]["tmp_name"];);
$title = mysqli_real_escape_string($conn, $_POST['title']);
$date = mysqli_real_escape_string($conn, date('Y-m-d H:i:s'));
$new_name = uniqid(); // Novo nome aleatório do arquivo
$extension = strtolower(pathinfo($_FILES["preview"]["name"], PATHINFO_EXTENSION)); // Pega extensão de arquivo e converte em caracteres minúsculos.
$folder = "imagens";
move_uploaded_file($preview, $folder."/".$new_name.".".$extension);
$subtitle = strlen($subtitle) > 0 ? "'$subtitle'" : "NULL";
$preview = strlen($preview) > 0 ? "'$preview'" : "NULL";
$vsl = "INSERT INTO 'files' ('id', 'subtitle', 'preview', 'title', 'date')VALUES(NULL, $subtitle, $preview, '$title', '$date')";
$rsl = mysqli_query($conn, $vsl);
header("Location: ../admin/add.php");
exit();
}
?>
<form action="../actions/create_player.php" method="post" enctype="multipart/form-data">
<label>Title: <i class="fa fa-info-circle" data-toggle="tooltip" title="" data-original-title="Insert Title For This Player"></i></label>
<input name="title" class="form-control" type="text" required="required">
<label for="preview" class="control-label">Custom Preview:</label> <i class="fa fa-info-circle text-muted" data-toggle="tooltip" title="Insert Custom Preview URL For This Video"></i><span class="pull-right"><span class="label label-info cp" id="pre"><i class="fa fa-cloud-upload"></i></span></span>
<input type="url" class="form-control" id="jc_preview" name="preview" placeholder="Insert Custom Preview URL">
<br>
<label>Subtitle: <i class="fa fa-info-circle" data-toggle="tooltip" title="" data-original-title="Subtitle For Player"></i></label>
<input name="subtitle" class="form-control" placeholder="Subtitles in VTT or Srt Formats" type="url">
<button type="submit" name="create" class="btn btn-primary btn-block"><i class="fa fa-check"></i> Publish</button>
</form>