简体   繁体   English

我需要使用PHP上传图片并在数据库中插入文件名

[英]I need to upload an image with PHP and insert file names in database

I have a PHP form that updates records in a database. 我有一个PHP表单,用于更新数据库中的记录。 It looks something like this. 看起来像这样。

//update a record    
$query1 = 'UPDATE mytable SET name="'.$name.'", description="'.$desc.'", 
 img="'.$img.'" WHERE id="'.$id.'" ';

mysqli_query($con,$query);

//get record set
$query2 = 'SELECT * FROM mytable WHERE id="'$id'"';
$result = mysqli_query($con,$query2);

echo  '<form action="my-update-page.php" method="post">';

//table heading row
echo '<table width="1000" border="1" cellspacing="0" cellpadding="1">';
  echo  '<tr>';
  echo   '<td>ID</td>';
  echo   '<td>NAME</td>';
  echo   '<td>description</td>';
  echo   '<td>Image</td>';
  echo  '</tr>';

//display data
while($row = mysqli_fetch_array($result))
  {


  echo '<input type="hidden" name="id" value="' . $row['id'] . '" />';
  echo  '<tr>';
  echo   '<td>'. $row['id'] . '</td>';
  echo   '<td><input type="text" name="name" value="'. $row['name'].'" /></td>';
  echo   '<td><textarea name="description">'.$row['description'].'</textarea></td>';
  echo   '<td><input type="text" size="3" name="img" value="'. $row['img'].'"/>;
  echo   '<a href="upload.php">Upload Image</a></td>';
  echo '</tr>';

  }

//closing tag for table  
echo '</table>';
echo '<br /><input type="submit" value="submit" /></form>';

I want my upload.php page to open in a popup where the user can upload the image. 我希望我的upload.php页面在弹出窗口中打开,用户可以在其中上传图像。 I'm pretty sure I can manage doing that. 我很确定我可以做到这一点。 Where I get stuck is after the file is uploaded, I want the popup to close and file name to show in the form input. 卡住的地方是文件上传后,我想关闭弹出窗口并使文件名显示在表单输入中。

Modify to reflect your names, but window.opener is the link to the other window. 进行修改以反映您的姓名,但是window.opener是指向另一个窗口的链接。 At that point, access any elements the same way. 此时,以相同的方式访问任何元素。

window.opener.forms['myform'].elements['fileinput'].value = nameOfFile;

window.close();

As the comment above says, use AJAX to do the file post. 如上面的评论所述,使用AJAX进行文件发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM