Linking session ID to database using MYSQL & PHP


Linking session ID to database using MYSQL & PHP

我需要什么帮助?-当我将图像上传到数据库时,我想将用户的ID链接到SQL的正确字段中。不幸的是,当我上传图像时,ID字段中没有输入任何内容,因此似乎是它没有正确捕捉到图像。

所以细分一下:当用户登录时,他有一个唯一的ID,即管理员的ID是1。当他在用户面板上时,他点击上传第二张图片:然后他被引导到这个表单

在表单上,他将输入描述、图像,并且他的ID应取自_SESSION。

如果需要更多的信息,我很乐意写更多。

提前感谢

所以。。。代码在这里:

//表单//

<!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>

以下是我将数据输入数据库时的表格:

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

您的隐藏字段没有输入名称。看起来您在隐藏的输入标记中使用的是id="id"而不是name="id"。

修复它,它应该会起作用。

我认为第一个文件有问题:

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

$rows变量根本没有在该文件中定义,因此没有任何内容可显示。您已经提到,id是从_session变量中获取的,也许您犯了一个错误,这将修复它:

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

别忘了写:

session_start();

在文件的开头,在html代码之前。