简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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