为 php mysql upload 添加唯一 id

发布于 2024-12-11 08:29:18 字数 3017 浏览 3 评论 0原文

有没有一种快速方法可以将唯一的 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 表中的每个项目包含唯一的代码?

非常感谢任何帮助!

谢谢 京东

Is there a quick method to add a unique id to a php mysql upload- I have scrolled through these forums but was hoping there is a much simpler method to achieve my aim.

Essentially, I have an upload that works perfectly - and I am hoping to add a product code to each item that will be generated using the auto-incremented unique id field in mysql.

So far I have the following 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"; 

}
} 
?> 

And the following 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>

Now given everything works - the only "problem line" in the code is:

$productcode = "FUR000" .(mysql_real_escape_string($_POST['id']));

assuming that as the id hasnt yet been generated it cannot add it to the insert query - therefore the table in mysql simply returns FUR000 for each new item added.

Is there a way to amend this line to auto-increment in mysql in a similar fashion to the addition of new lines - or do I have to include a unique code for each item in my HTML table?

Any help much appreciated!

Thanks
JD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-12-18 08:29:18

为此你需要 2 个查询。

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

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

you need 2 queries for this.

first, insert your data without productcode.
next, get id using mysql_insert_id()
finally, create your productcode and update your table using this newly generated id

however, I see no point in such a field. Why not to create it on the fly?

梦幻的味道 2024-12-18 08:29:18

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

You want to use uniqid, which generates unique ids. I'd recommend using it with more entropy to be on the safe side.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文