Hi I'm trying to make a basic flickr like website where you can leave a comment on the rightside of the image like this http://www.flickr.com/photos/36681814@N05/13494397313/in/explore-2014-03-29 I have right now a user logs in and upload a image the images name, who uploaded and some more stuff is saved in the database table images. And when they click on the uploaded image it "popsup" in mbox jquery plugin. On the rightside there is a bar where all the information is displayed. But now i want to make a comment field on that bar and when they send it it's displayed on the same bar. I got so far that the comment and username is saved in the database but i cant seem to figure out how to get the img_id from the images table in to the comments table img_id row. So i can display the comments that are made for that image. this is the code for the gallery.php where the images is looped through and shown $query = "SELECT * FROM images"; $result = mysqli_query($dbc, $query); while ( $row = mysqli_fetch_array($result)) {?> <a href="bilder/<?php echo $row['namn'];?>" data-init="mbox" data-type="image" class="mbox"><img src="thumb_bilder/<?php echo $row['namn'] ;?>" width="300" height="200"/><span class="mbox-descr"><span class="information"> <?php echo $row['username'];?><br /> <?php echo $row['datum'];?><br /><br /> <?php echo $row['kommentar'];?><br /> <p style="border-bottom:1px solid grey; margin-top:80px; margin-bottom:30px;"></p> <?php echo $row['kamera'];?><br /><?php echo $row['objektiv'];?><br /> <?php echo $row['slutartid'];?><br /><img src="img/blandare.png" class="smabilder"/> <?php echo $row['blandare'];?><br /><img src="img/skarpdjup.png" class="smabilder"/> <span class="skarpdjup"><?php echo $row['skarpdjup'];?></span><br /> <img src="img/iso.png"/ class="smabilder"><?php echo $row['iso'];?> <form method="post" action="includes/insert.php"><textarea name="kommentar"></textarea><input type="submit" name="submit" value="skicka"></form></span></span></a> <?php } ?> PHP: i tried this but it didn't work i dint get the img_id <?php include 'connect.php'; include 'membersphp.php'; $query = "SELECT img_id FROM images"; $result = mysqli_query($dbc, $query); while ( $row = mysqli_fetch_array($result)) { } $id = $row['img_id']; $comment = $_POST['comment']; $comment = mysqli_real_escape_string($dbc, $comment); $uppladdare = $_SESSION['username']; $uppladdare= mysqli_real_escape_string($dbc, $uppladdare); $sql="INSERT INTO comments(img_id,comment, username) VALUES ('$id', '$comment','$uppladdare')"; if (!mysqli_query($dbc,$sql)) { die('Error: ' . mysqli_error($dbc)); } mysqli_close($dbc); ?> PHP: How do i get the images table img_id into the comments database img_id row?
You need to do the assignment of the ID inside the while-loop - on the example above, you've put it outside the loop, which at best will give you one ID, and it won't be separate for each image.
uhm - since the ID will be declared inside the while-loop (one for each image), you can get that in several ways, depending on how you push the other information - but you will have to echo the ID out somewhere, either as part of a form, as a value, or as a GET-parameter or something similar. But since you're trying to submit form data, this should all be included in a form, somewhere.
I can't figure out what you're trying to do (looks like the typical mess of jquery matched to HTML 5 "for nothing" garbage, like those stupid malfing data- attributes), but I can say your PHP methdology is just bad... You're using mysqli, USE IT -- which is to say get rid of those stupid malfing string additions and 2004 style "real-escape-string" crap and use the entire reason you're supposed to be using mysqli instead of mysql_ -- PREPARED QUERIES. Likewise I'd suggest you stop opening and closing PHP for no good reason on every blasted line, and even if you WERE to use that methodology that's what <?= is for. Your markup is also greatly flawed -- since you don't ever seem to even close your anchor or even most of your span, resulting in block level elements inside inline-level ones -- and no matter what the HTML 5 ****tards say about that, it does NOT work reliably in any browser yet! (and that rule existed for a reason). FORM has no business inside an ANCHOR much less SPAN. Overall though, I suspect we're not seeing enough of your code or what you are doing to give you a real answer -- I'm assuming most of the code has been cut out or something with that 'while for nothing', query that isn't even run properly, and lack of actual user response logic. Whatever it is you're trying to do, I'm pretty sure that's not how you do it. I think what you might be missing is assigning the ID to each of your form element's NAME attributes, probably best done via array assignment -- you have nothign to establish a relationship between your form comments and their related ID's. Or is your intent to assign that text to ALL ID's in the database (which is what you wrote)?