php - 将唯一ID添加到php mysql上传

标签 php mysql unique-key

有没有一种快速方法可以将唯一 ID 添加到 php mysql 上传 - 我浏览了这些论坛,但希望有一种更简单的方法来实现我的目标。

基本上,我有一个完美的上传 - 我希望将产品代码添加到将使用 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&amp;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 表中的每个项目包含一个唯一代码?

非常感谢任何帮助!

谢谢 京东

最佳答案

为此你需要 2 个查询。

首先,插入没有产品代码的数据。
接下来,使用 mysql_insert_id()
获取 ID 最后,创建您的产品代码并使用这个新生成的 ID 更新您的表

但是,我认为在这样的领域没有意义。为什么不即时创建它?

关于php - 将唯一ID添加到php mysql上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7850273/

相关文章:

php - 如何使用 smarty 从 CDN 源包含 javascript

php - Grocery Crud - 无法区分返回查询表中的属性

javascript - Bootstrap.min.js 的 src 不工作

php - 使用 php 中的管理面板从 mysql 数据库中的 Web 应用程序创建数据库

php - 将 sql 表内容从一个表复制到另一个表 - 有条件

Mysql - 汇总报告 - 删除重复项 - 列的总和

java - 架构中现有字段的 Solr 复合唯一键

仅当行不存在时 MySQL INSERT,否则选择重复行

php - 如何使用 PHP 代码将日期从数据库表加载到 fullcalender? [没有 Ajax ]