使用PHP获取添加文件的ID

时间:2015-08-28 07:02:51

标签: php mysql file post mysqli

有下表名为filescontent

ID  |  version  |  fileContent  | ownerID

我需要获取用户通过POST请求添加的文件的ID,然后我必须操纵它。你能帮我解决这个问题吗?

if($_POST) {
    $name = mysqli_real_escape_string($connection, $_FILES['uploadFile']['name']);
    $data = mysqli_real_escape_string($connection, file_get_contents($_FILES ['uploadFile']['tmp_name']));

    $select_version = mysqli_query($connection, "SELECT version FROM filescontent ORDER BY fileID DESC LIMIT 1");
    $fetch_version = mysqli_fetch_assoc($select_version);
    $increment_version = $fetch_version['version'] + 1;
    echo '<br>'.$increment_version .' версия';

    $select_fileID = mysqli_query($connection, "SELECT fileID FROM filescontent ORDER BY fileID DESC LIMIT 1");
    $fetch_fileID = mysqli_fetch_assoc($select_fileID);
    $increment_fileID = $fetch_fileID["fileID"] + 1;
    echo '<br>'. $increment_fileID.'ID';

    $selectName = mysqli_query($connection, "SELECT fileName FROM files WHERE fileName='$name'");
    $fetchName = mysqli_fetch_assoc($selectName);
    $fetchName = $fetchName['fileName'];

    $selectIDname= "SELECT fileID FROM files WHERE fileName = '$name'";
    $selectIDnameQuery = mysqli_query($connection, $selectIDname);
    $row = mysqli_fetch_array($selectIDnameQuery);
    $selectIDname = $row['fileID'];
    echo '<br><pre>'.print_r($row, true).'</pre>';

    $a = mysqli_error($connection);
    echo '<br>'.$a.'<br>';  

    if(!strcmp($name, $fetchName)){
        echo 'The file exists';
        $query1 = mysqli_query($connection, "INSERT INTO filescontent (fileID, version, fileContent, author) VALUES ('$selectIDname', '$increment_version', '$data', 2)");
    }
    else{
        echo 'The file does not exist'; 
        $query2= mysqli_query($connection, "INSERT INTO filescontent (fileID, version, fileContent, author) VALUES ('$increment_fileID', 1, '$data', 2)");
        $query3 = mysqli_query($connection, "INSERT INTO files (fileID, fileName, ownerID) VALUES ('$increment_fileID', '$name', 2)");
    }

    $a = mysqli_error($connection);
    echo '<br>'.$a.'<br>';  
}

1 个答案:

答案 0 :(得分:-1)

使用下面提到的代码,我已经为您编写了代码,让我知道它是否适合您:

if($_POST) {
    $name = mysqli_real_escape_string($connection, $_FILES['uploadFile']['name']);
$data = mysqli_real_escape_string($connection, file_get_contents($_FILES ['uploadFile']['tmp_name']));

$select_version = mysqli_query($connection, "SELECT version FROM filescontent ORDER BY fileID DESC LIMIT 1");
$fetch_version = mysqli_fetch_assoc($select_version);
$increment_version = (isset($fetch_version['version'])?$fetch_version['version']:0) + 1;
echo '<br>'.$increment_version .'<br>';

$select_fileID = mysqli_query($connection, "SELECT MAX(fileID) as fileID FROM filescontent ");
$fetch_fileID = mysqli_fetch_assoc($select_fileID);
$increment_fileID = (isset($fetch_fileID["fileID"])?$fetch_fileID["fileID"]:0) + 1;
echo '<br>'. $increment_fileID.'ID';

$selectIDname= "SELECT fileID FROM files WHERE fileName = '$name'";
$selectIDnameQuery = mysqli_query($connection, $selectIDname);
$row = mysqli_fetch_array($selectIDnameQuery);
$selectIDname = (isset($row['fileID']) ? $row['fileID']:null);
echo '<br><pre>'.print_r($row, true).'</pre>';

$a = mysqli_error($connection);
echo '<br>'.$a.'<br>';  

if(!empty($selectIDname)){
    echo 'The file exists';
    $query1 = mysqli_query($connection, "INSERT INTO filescontent (fileID, version, fileContent, author) VALUES ('$selectIDname', '$increment_version', '$data', 2)");
}
else{
    echo 'The file does not exist'; 
    $query2= mysqli_query($connection, "INSERT INTO filescontent (fileID, version, fileContent, author) VALUES ('$increment_fileID', 1, '$data', 2)");
    $query3 = mysqli_query($connection, "INSERT INTO files (fileID, fileName, ownerID) VALUES ('$increment_fileID', '$name', 2)");
}

$a = mysqli_error($connection);
echo '<br>'.$a.'<br>';  
}