2012-01-27 67 views
-1

我需要什麼幫助? - 當我將圖像上載到數據庫時,我想將用戶的ID鏈接到我的SQL的正確字段中。不幸的是,當我上傳圖像時,沒有任何內容被輸入到ID字段中,因此似乎是它沒有正確捕獲它。使用MYSQL和PHP將會話ID鏈接到數據庫

所以分解:當用戶登錄時,他有一個唯一的ID,即管理員ID是1.當他在他的用戶面板上時,他點擊上傳第二張圖片:然後他被導向到這個表格。

一旦在窗體上,他將輸入一個描述,圖像,他的ID應該從_SESSION中採取。

如果需要更多信息,我很樂意多寫。

由於提前,

所以...繼承人的代碼:

// // FORM

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 

<form enctype="multipart/form-data" id="form1" name="form1" method="post" action="secondPic.php"> 
    <p> 
    <label for="name1">Fav Location Name: </label> 
    <input type="text" name="name1" id="name1" /> 
    </p> 
    <p> 
    <label for="photo1">Fav Location Photo: </label> 
<input type="file" name="photo1"><br> 
    </p> 
    <p> 
    <label for="id">ID: <? echo $rows['id']; ?> </label> 
    <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> 
    </p> 
    <p> 
    <input type="submit" name="submit" id="submit" value="Submit" /> 
    </p> 
</form> 



</body> 
</html> 

//輸入到數據庫中//

<?php 
include "common.php"; 
$secondid = $_GET['id']; 
DBConnect(); 



$Link = mysql_connect($Host, $User, $Password); 

//This is the directory where images will be saved 
$target = "second/"; 
$target = $target . basename($_FILES['photo1']['name']); 


$favname = $_POST["name1"]; 
$pic2=($_FILES['photo1']['name']); 
$id = $_POST["$id"]; 



$Query ="INSERT into $Table_2 values ('0', '$id', '$favname', '$pic2')"; 

if (mysql_db_query ($DBName, $Query, $Link)){ 
print ("A record was created <br><a href=index.php> return to index </a>\n"); 

// Connects to your Database 
//mysql_connect("localhost", "jonathon_admin", "hello123") or die(mysql_error()) ; 
//mysql_select_db("jonathon_admin1") or die(mysql_error()) ; 


//Writes the photo to the server 
if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."; 
} 


} else { 

print (" - Your Record was not created"); 
} 

mysql_close($Link); 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
</body> 
</html> 

下面是我輸入數據到DB時的表格:

**s_id id favname   pic2 
    1  0 testing the db piccy.png** 
+3

你有沒有試過測試過你的任何代碼?你真的應該試着弄清楚代碼到底有多遠,哪裏是..等等,然後發佈它,而不是發佈整個事情。 – Dave 2012-01-27 17:05:03

+0

你在使用會話嗎?將ID存儲在一個會話中,並在您的帖子中使用它 – Drewdin 2012-01-27 17:12:31

+0

我敢打賭,您的問題是在'INSERT'查詢的第一個字段處出現'0'。如果這對應於PK,則不會插入任何後續行。你應該指定一個沒有PK的字段列表,或者提供'NULL'(* not *''NULL'')來解決這個問題。 – DaveRandom 2012-01-27 17:13:53

回答

0

您的隱藏字段沒有輸入名稱。它看起來像你在你的隱藏的輸入標籤中使用id =「id」而不是name =「id」。

解決這個問題,它應該工作。

+0

已經改變了它,但它確實有name ='id'因此不應該有所作爲我不認爲,但改變了一切。 – 2012-01-27 17:37:46

0

我想,這有什麼不對的第一個文件:

<label for="id">ID: <? echo $rows['id']; ?> </label> 
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> 

的$行變量沒有在該文件中定義可言,因此,沒有任何顯示。 你提到,該ID是從_session變量得到的,也許你犯了一個錯誤,這將解決這個問題:

<label for="id">ID: <? echo $_SESSION['id']; ?> </label> 
    <input name="id" type="hidden" id="id" value="<? echo $_SESSION['id']; ?>"> 

不要忘記寫:

session_start(); 

在開始時在html代碼之前。

+0

謝謝你,現在會測試這個,我想我有! – 2012-01-27 17:22:42

+0

是啊,仍然沒有運氣的隊友,但我想我確實需要,而不是$行肯定是一個錯誤。 – 2012-01-27 17:29:34