【问题标题】:add unique id to php mysql upload向php mysql上传添加唯一ID
【发布时间】: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&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 表中的每个项目包含一个唯一代码?

非常感谢任何帮助!

谢谢 京东

【问题讨论】:

  • 他在说什么,有人吗?什么是“mysql上传”?
  • 对不起 - php mysql 插入.....

标签: php mysql unique-key


【解决方案1】:

您需要 2 个查询。

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

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

【讨论】:

  • 查看了我没有完全同意的 uniqid 功能并希望即时创建。
  • mysql_insert_id() 和自动递增的主键字段是做事的“正确”方式。为新插入生成 GUID 比它需要的复杂得多。
  • @JD2011 看看。您的产品代码字段是多余的。 'FUR000' 是脚本中硬编码的常量。 ID 存储在数据库中。因此,您可以在任何时候选择您的数据时获取您的产品代码。无需存储。
【解决方案2】:

您想使用uniqid,它会生成唯一的 ID。为了安全起见,我建议使用更多的熵。

【讨论】:

  • 正是我所追求的......不过我需要一些帮助来整合它!我已经包括 $productcode= printf("uniqid('', true): %s\r\n", uniqid('', true));进入我的页面顶部,然后使用原来的插入函数:mysql_query("INSERT INTO furnishings (title, desc, price, pandp, photo,productcode) VALUES ( '$title', '$desc', '$price', '$pandp', '$pic', '$productcode')") ; .它肯定是在表中添加了一些东西,但由于某种原因,它在 mysql 中返回了一个两位数 - productcode col 类型是 VARCHAR (255) - 这可能是问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-23
  • 2012-09-23
  • 2014-02-03
相关资源
最近更新 更多