I have a while loop that is showing photographs, and under are each one is a link throwing a popup and opening a form. The form is made to send a comment to the photograph. Everything works here but I have a problem with photoid variable. I have been trying for many days but the id of a photo never gets out correctly out of the loop. Its always id of the last photo or of the first.... Could you please help me to do this? People have been helping me with some jquery, ajax scripts, with some hidden input in form but this never worked for me, maybe i am too stupid :( Could you help me please take the id of the photo connected with "comments" link outside of the loop? I am so tired of this....... Please! I tagged it also as java script because i dont know what will be needed to finish this
<?php
include 'connect.php';
//-----------------HERE BEGINS THE LOOP THROWING OUT PHOTOS--------------------
$allphotos = mysql_query("SELECT * FROM photos ORDER BY id DESC");
while ($numphotos = mysql_fetch_assoc($allphotos)){
$photoinfo = mysql_query('SELECT * FROM photos WHERE link="'.$numphotos['link'].'" ');
$fetchinfo = mysql_fetch_assoc($photoinfo);
echo '<img src="'.$numphotos['link'].'" alt="photo" class="photolink" align="middle">';
$photoid = $fetchinfo['id'];
echo " <div class='comments'><a href='#' class='my-button' onclick='komenty(".$photoid.")'>komentarze</a></div> ";
//---------------------HERE STARTS THE POPUP FORM-------------
echo "
<div id='element_to_pop_up'>
<img src='' alt='photo' class='photolink' align='middle'>
<form action='main.php' id='comments' method='post'>
<textarea rows='8' cols='80' name='comments'></textarea> <br />
<input type='hidden' name='photoid' value='".$photoid."'>
<input type='submit' name='send' value='Wyślij'>
</form>
<a class='b-close'></a>
</div>";
}
//----------------AFTER THE FORM IS SUBMITED THE CODE BELOW SAVES THE COMMENT TO DATABASE BUT THE PHOTOID IS ALWAYS NOT CORRECT---------
if (isset($_POST['send'])){
try{
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare("INSERT INTO comments (comment, photoid, writer, date) VALUES (:comment, :photoid, :writer, :date) ");
$stmt->execute(array('comment'=> $_POST['comments'], 'photoid'=> $_POST['photoid'], 'writer'=> $_GLOBALS['nick'], 'date'=> date("Y-m-d")));
}
catch(PDOException $e){
echo 'error main.php 1:' . $e->getMessage();
}
}
?>
Aucun commentaire:
Enregistrer un commentaire