gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:52:23 25 4
gpt4 key购买 nike

有没有一种快速方法可以将唯一 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com