上传文件(id by id)

时间:2011-09-15 10:55:55

标签: php mysql database file-upload

我想通过我的php上传文件,我的代码是这样的:

$hasil = mysql_query("select ID from userownfile order by ID DESC");
$dt = mysql_fetch_array($hasil);
$old_id = substr($dt['ID'],2);

$newId = $old_id[0] + 1;

$base = $_REQUEST['image'];
$filename = $newId . ".jpg";
$buffer = base64_decode($base);
$path = "img/" . $filename . ".jpg";
$handle = fopen($path, 'wb');
$numbytes = fwrite($handle, $buffer);
fclose($handle);
$conn = mysql_connect("localhost","root","");
mysql_select_db("db_bloodglucose");
$sql = "insert into userownfile(ID, file) values('" . $newId . "','" . $path . "')";
$r = mysql_query($sql);

首次上传时,我已经成功上传了一个名为1.jpg的文件,但是当我想第二次上传时,它没有显示在我的数据库上,它一直显示1.jpg,我想知道为什么数字没有增加..任何人都可以帮助我?

之前谢谢

我一直在改变我的代码:

<?php

$hasil = mysql_query("select ID from userownfile order by ID DESC");
$dt = mysql_fetch_array($hasil);
$old_id = substr($dt['ID'],2);

$newId = mysql_insert_id() + 1;

$base = $_REQUEST['image'];
$filename = $newId . ".jpg";
$buffer=base64_decode($base);
$path = "img/".$filename.".jpg";
$handle = fopen($path, 'wb');
$numbytes = fwrite($handle, $buffer);
fclose($handle);
$conn=mysql_connect("localhost","root","");
mysql_select_db("db_bloodglucose");
$sql = "insert into userownfile(ID, file) values('" . $newId . "','" . 

$path . "')";
$r=mysql_query($sql);
?>

但是,它一直坚持1,数字没有增加,任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我猜错误就在这里:

$dt = mysql_fetch_array($hasil)
$old_id = substr($dt['ID'],2);
$newId = $old_id[0]+1;

可能是

$newId = $dt[0]+1;

或只是

$newId = $old_id+1;

但更好的是使用mysql_insert_id,它会为您提供插入记录 id 以避免并发问题。