2011-10-21 2 views
1

PHP에 고유 ID를 추가하는 빠른 방법이 있습니까?이 포럼을 통해 스크롤했지만 내 목표를 달성하는 훨씬 간단한 방법이 있기를 바랍니다.PHP mysql upload에 고유 한 ID를 추가하십시오.

기본적으로 완벽하게 작동하는 업로드가 있습니다. mysql에서 자동 증가 고유 ID 필드를 사용하여 생성되는 각 항목에 제품 코드를 추가하려고합니다. 유일한 "문제의 라인을"코드에 -

이제 주어진 모든 작품
<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> 

가 :

<?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 :

지금까지 나는 다음과 같은 PHP를

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

ID가 아직 생성되지 않았으므로 삽입 쿼리에 추가 할 수 없다고 가정하므로 내 테이블을 sql은 추가 된 각 새 항목에 대해 FUR000을 반환합니다.

새로운 라인을 추가하는 것과 비슷한 방식으로이 라인을 mysql에서 자동 증분하도록 수정하는 방법이 있습니까? 아니면 HTML 테이블의 각 항목에 고유 한 코드를 포함해야합니까?

많은 도움을 주셨습니다.

감사 JD

+0

그는 무엇에 관해 말하고 있습니까? "mysql upload"란 무엇입니까? –

+0

미안 해요 - PHP는 MySQL의 삽입 ..... – JD2011

답변

0

당신은 고유 ID를 생성 uniqid를 사용하고 싶습니다. 안전한쪽으로 가기 위해 더 많은 엔트로피를 사용하는 것이 좋습니다.

+0

그냥 내가 뭘 .... .... 나는 그것을 통합하는 데 도움이 필요해! % productcode = printf ("uniqid ('', true) : % s \ r \ n", uniqid ('', true)); 내 페이지 상단에 다음 삽입 원래 함수를 사용 : mysql_query ("INSERT INTO'가구'('제목','desc','가격','pandp','사진','productcode') VALUES ('$ title', '$ desc', '$ price', '$ pandp', '$ pic', '$ productcode') "); . 분명히 테이블에 뭔가를 추가하고 있지만 어떤 이유로 mysql에서 2 자리 숫자를 반환합니다. productcode col 유형은 VARCHAR (255)입니다. 이것이 문제가 될 수 있습니까? – JD2011

1

이 경우 2 개의 검색어가 필요합니다.

먼저 제품 코드없이 데이터를 삽입하십시오.
다음, ID 사용에 mysql_insert_id를 얻을 수()
마지막으로, 당신의 제품 코드를 생성하고

그러나, 나는 그런 분야에서 어떤 점을 볼이 새로 생성 된 ID를 사용하여 테이블을 업데이트합니다. 왜 파리에서 그것을 만들지 않습니까?

+0

완전히 uniqid 기능을 검토하는 데 완전히 동의하고 즉석에서 만들기를 찾고 있습니다. – JD2011

+0

uniqid가 도움이되지 않습니다. –

+0

은 Col Shrapnel입니다. – JD2011

관련 문제