Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a variable with a changing value in MySQL query?
    primarykey
    data
    text
    <p>I'm building a tagging system for a site I'm working on. I have a variable for the url of the image that changes as the user selects different pictures. I'm using a MySQL command to display 3 tags for the selected picture, but it isn't working if I give it that changing variable. However, if I manually insert a url of an image, the captions show. However, these will show no matter what picture you're on so that doesn't really help. If I store a url under a variable, that works too, but not with the one that changes all the time. </p> <p><a href="http://ewsprojects.com/~lyons" rel="nofollow">Here</a>'s the site.</p> <p>I know this isn't very easy to understand, so please let me know if you want me to try re-explaining something or if you want me to include some of the code. Thanks!</p> <p>EDIT: Ok, I'll retry here. First, here's the code:</p> <pre><code>&lt;?php $username = $_GET['username']; session_start(); $_SESSION['username'] = $username; //Database Information $dbhost = ""; $dbname = ""; $dbuser = ""; $dbpass = ""; //Connect to database mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error()); mysql_select_db($dbname) or die(mysql_error()); //Do the query $query = mysql_query("SELECT * FROM images ORDER BY idnum DESC LIMIT 5"); // Generate an array of all images. $images = array(); while($image = mysql_fetch_array($query)) { // Adds each image to the images array. $images[] = $image; } $image['filename'] = $current_image; $query_captions = mysql_query("SELECT * from captions WHERE image = '$current_image' ORDER BY idnum DESC LIMIT 3"); $captions = array(); while($caption = mysql_fetch_array($query_captions)) { $captions[] = $caption; } ?&gt; &lt;?php // Beginning attempt at a caption script. ?&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Home - Site in Development&lt;/title&gt; &lt;link rel="stylesheet" type="text/css" href="styles.css"/&gt; &lt;script type="text/javascript"&gt; // Switches the url to view large image. function switchImageUrl(url, width, height) { var x = document.getElementById('caption_selection'); var y = document.getElementById('caption_input'); var z = document.getElementById('input_value'); x.style.display = 'none'; y.style.display = 'none'; z.value = ''; document.getElementById('center_frame').style.backgroundImage = 'url' + '(' + url + ')'; document.getElementById('center_frame').style.width = width; document.getElementById('center_frame').style.height = height; } &lt;/script&gt; &lt;!--Toggles view of caption selection tools and resets after.--&gt; &lt;script type="text/javascript"&gt; function selectionToggle() { var x = document.getElementById('caption_selection'); var y = document.getElementById('caption_input'); var z = document.getElementById('input_value'); if(x.style.display == 'block') x.style.display = 'none'; else x.style.display = 'block'; x.style.width = '75px'; x.style.height = '75px'; x.style.top = '0px'; x.style.left = '0px'; if(y.style.display == 'block') y.style.display = 'none'; else y.style.display = 'block'; z.value = ''; } &lt;/script&gt; &lt;script type="text/javascript"&gt; function collectAttributes() { var cap = document.getElementById('caption_selection'); var cw = document.getElementById('cap_width'); var ch = document.getElementById('cap_height'); var cl = document.getElementById('cap_left'); var ct = document.getElementById('cap_top'); cw.value = cap.style.width; ch.value = cap.style.height; cl.value = cap.style.left; ct.value = cap.style.top; } &lt;/script&gt; &lt;link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"&gt;&lt;/script&gt; &lt;script&gt; $(document).ready(function() { $("#caption_selection").draggable({ containment : "#center_frame" }); $("#caption_selection").resizable({ containment : "#center_frame" }); }); &lt;/script&gt; &lt;/head&gt; &lt;body onLoad="CalculateAllImageWidthes()"&gt; &lt;div id='account_links'&gt; &lt;?php if ($_SESSION['username']) { echo "Welcome $username!"; } else { ?&gt; &lt;a href='login.php'&gt;Login&lt;/a&gt; | &lt;a href='register.php'&gt;Register&lt;/a&gt; &lt;?php } ?&gt; &lt;/div&gt; &lt;h1&gt;Picture Captions&lt;/h1&gt; &lt;br/&gt; &lt;br/&gt; &lt;div id="left_bar"&gt; Submit a picture &lt;a href="upload.php"&gt;here&lt;/a&gt;. &lt;hr/&gt; &lt;h2&gt;Top Images&lt;/h2&gt; &lt;br/&gt; &lt;div id="front_pg_images"&gt; &lt;?php foreach($images as $image) { ?&gt; &lt;a onClick="switchImageUrl('&lt;?php echo $image['filename']; ?&gt;', '&lt;?php echo $image['width']; ?&gt;', '&lt;?php echo $image['height']; ?&gt;')"&gt;&lt;img src="&lt;?php echo $image['filename'];?&gt;" width="72px" height="58px" id="front_pg_thumbnail"/&gt;&lt;/a&gt; &lt;?php echo $image['name']." - by ".$image['submitter']; ?&gt; &lt;br/&gt; &lt;br/&gt; &lt;?php } ?&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="center_frame" style="width: &lt;?php echo $image['width']; echo "px" ?&gt;; height: &lt;?php echo $image['height']; echo "px" ?&gt;; background-image: url('&lt;?php echo $image['filename'];?&gt;')" &gt; &lt;?php foreach($captions as $caption) { ?&gt; &lt;div id="set_caption" style="width:&lt;?php echo $caption['width'].'px';?&gt;;height:&lt;?php echo $caption['height'].'px';?&gt;; left:&lt;?php echo $caption['posleft'].'px';?&gt;; top:&lt;?php echo $caption['postop'].'px';?&gt;"&gt;&lt;?php echo $caption['text'];?&gt;&lt;/div&gt; &lt;?php } ?&gt; &lt;div id="caption_selection" style="display:none"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="toggle_select_container"&gt; &lt;input type="button" id="toggle_select" onClick="selectionToggle()" value="Caption"/&gt; &lt;div id="caption_input" style="display:none"&gt; &lt;form action="postcaption.php" method="post" onSubmit="collectAttributes()"&gt; &lt;br/&gt; &lt;textarea name="caption_text" cols="27" rows="4" id="input_value"&gt;&lt;/textarea&gt;&lt;br/&gt; &lt;input type="hidden" name="filename" value="&lt;?php echo $image['filename'];?&gt;"/&gt; &lt;input type="hidden" name="width" value="" id="cap_width"/&gt; &lt;input type="hidden" name="height" value="" id="cap_height"/&gt; &lt;input type="hidden" name="left" value="" id="cap_left"/&gt; &lt;input type="hidden" name="top" value="" id="cap_top"/&gt; &lt;input type="submit" value="Post Caption"/&gt; &lt;/form&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>It uses a JavaScript function to change the image when the user clicks on the thumbnail. Take note of the <code>foreach</code>. For everywhere else on the page, the <code>$image['filename']</code> has worked perfectly for similar uses, but it just isn't working when I try to use it with this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload