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 テーブルの各項目に一意のコードを含める必要がありますか?
どんな助けでも大歓迎です!
ありがとうございます