【发布时间】:2011-10-21 13:43:41
【问题描述】:
有没有一种快速的方法可以为 php mysql 上传添加一个唯一的 id - 我浏览了这些论坛,但希望有一个更简单的方法来实现我的目标。
基本上,我有一个完美的上传文件 - 我希望将产品代码添加到将使用 mysql 中自动递增的唯一 id 字段生成的每个项目。
到目前为止,我有以下 php:
<?php include 'dbc.php'; page_protect();
if(!checkAdmin()) {header("Location: login.php");
exit();
}
$host = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
$path = rtrim($login_path, '/\\');
foreach($_GET as $key => $value) {
$get[$key] = filter($value);
}
foreach($_POST as $key => $value) {
$post[$key] = filter($value);
}
?>
<?php
if($_FILES['photo']) //check if we uploading a file
{
$target = "images/furnishings/";
$target = $target . basename( $_FILES['photo']['name']);
$title = mysql_real_escape_string($_POST['title']);
$desc = mysql_real_escape_string($_POST['desc']);
$price = mysql_real_escape_string($_POST['price']);
$pandp = mysql_real_escape_string($_POST['pandp']);
$pic = "images/furnishings/" .(mysql_real_escape_string($_FILES['photo']['name']));
$productcode = "FUR000" .(mysql_real_escape_string($_POST['id']));
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
mysql_query("INSERT INTO `furnishings` (`title`, `desc`, `price`, `pandp`, `photo`,`productcode`) VALUES ('$title', '$desc', '$price', '$pandp', '$pic', '$productcode')") ;
echo "The product has been added to the furnishings category";
}
else
{
echo "Please fill out the specifications and select the respective file to upload for the main image";
}
}
?>
还有以下 HTML:
<form enctype="multipart/form-data" action="addfurn.php" method="POST">
<table width="100%" border="2" cellpadding="5"class="myaccount">
<tr>
<td>Title: </td>
<td><input type="text" name="title" /></td>
</tr>
<tr>
<td>Description: </td>
<td><input type="text" name = "desc" /></td>
</tr>
<tr>
<td>Price: </td>
<td><input type="text" name = "price" /></td>
</tr>
<tr>
<td>P&P: </td>
<td><input type="text" name = "pandp" /></td>
</tr>
<tr>
<td>Main Image: </td>
<td><input type="file" name="photo" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" class="CMSbutton" value="Add" /></td>
</tr>
</table>
</form>
现在一切正常 - 代码中唯一的“问题行”是:
$productcode = "FUR000" .(mysql_real_escape_string($_POST['id']));
假设 id 尚未生成,因此无法将其添加到插入查询中 - 因此 mysql 中的表只为添加的每个新项目返回 FUR000。
有没有办法以与添加新行类似的方式修改这一行以在 mysql 中自动递增 - 还是我必须为我的 HTML 表中的每个项目包含一个唯一代码?
非常感谢任何帮助!
谢谢 京东
【问题讨论】:
-
他在说什么,有人吗?什么是“mysql上传”?
-
对不起 - php mysql 插入.....
标签: php mysql unique-key