I have a form that allows a user to input a contraction (e.g. Larry's dogs) into a mysql table> The code is as follows:
<?php
<?php
if(isset($_POST['submit'])){
$title = mysqli_real_escape_string($conn,$_POST['title']);
$insertImageQ = "INSERT INTO images (title, name, keyword1, keyword2, keyword3, enter_date) VALUES('$title', '$name', '$keyword1','$keyword2','$keyword3',now())";
if(!mysqli_query($conn, $insertImageQ)){
die('error inserting new image');
}
$newImage = "one new image added";
}
}
?>
When I echo the field "title" on a webpage, it works fine unless it is echoed in an image title attribute. For example,
<?php
$slideShowQ = "SELECT * FROM slideshow";
$result= mysqli_query($connection, $slideShowQ) or die("error getting connected");
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<img class='lgImg'";
echo "id='";
echo $row['Id'];
echo "'";
echo "title='";
echo htmlentities($row['title']);
echo "'";
echo "alt='";
echo htmlentities($row['title']};
echo "'";
echo "src='_images/";
echo $row['name'];
echo "'/>";
}
?>
<?php
//release returned data
mysqli_free_result($result);
?>
In a javascript file, I call a function that on mouseover the image, a div id="topLeft" has it's html set to the title. For example,
$('.lgImg').mouseover(function(){
var title =$(this).attr('title');
$('#topLeft').html(title);
When there is a contraction in the title field, everything from the apostrophe is cut off.
Also, on another page, the title field is included in a fancybox application where the images are drawn from the mysql table. The title field appears as it should in a fancybox application unless there is a contraction in the title, in which case, the image will not even pop up as it should in a fancybox application.
Also, if I update the same image with a contraction with this code,
<?php
if(isset($_POST['submit'])){
$title= mysqli_real_escape_string($conn,$_POST['title']);
$updateQ = "UPDATE images SET title='$title'";
if(!mysqli_query($conn, $updateQ)){
die('error inserting');
}
}
?>
the field in the table goes blank and the fancybox does not work.
In sum, I can insert a contraction and echo that contraction as long as I don't echo it in an image title attribute, but I cannot update the field with a contraction.
Can anyone please help me to understand this contradiction?
htmlentities() will leave single quotes by default and as you are using them in your html attributes this will cause invalid html.
You can enforce encoding of single quotes like this:
htmlentities("'", ENT_QUOTES);
More info at the manual page
Related
I have created a members.php page that connects to a database table. The table has the following fields: id, username, profile image.
The following PHP code displays the profile image for each user in rows of 6 and allows each image to be clickable.
<?php
// The code below will display the current users who have created accounts with: **datemeafterdark.com**
$result=mysql_query("SELECT * FROM profile_aboutyou");
$row1 = mysql_fetch_assoc($result);
$id = $row1["id"];
$_SESSION['id'] = $id;
$profileimagepath = $row1["profileimagepath"];
$_SESSION['profileimagepath'] = $profileimagepath;
$count = 0;
while($dispImg=mysql_fetch_array($result))
{
if($count==6) //6 images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<center>
<img src="<?php echo $dispImg['profileimagepath'];?>" width="85px;" height="85px;">
</center>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
This is all great, however, when I click on the image that loads it re-directs me to: viewmemberprofile.php which is what it is supposed to do. But it always displays the same image with the same id value (i.e.) 150 no matter which image I click. What I would like to have happened is. If I click on an image with id 155 etc... it will display content for that image data field not consistently the same image data regardless of which image I click.
Your help and guidance would be greatly appreciated. Thank you in advance.
One thing that I forgot to mention is that I do use sessions so... when I am re-directed to the viewmemberprofile.php page I use the following code to aide in getting the data that I need from the table.
<?php
$id = $_SESSION['id'];
echo($id);
?>
<?php
echo('<br>');
?>
<?php
$profileimagepath = $_SESSION['profileimagepath'];
?>
<img src="<?php echo($profileimagepath);?>" width="50px;" height="50px;">
I have yet to impliment the suggested solution.
You need to pass the ID of the row to viewmemberprofile.php, e.g.:
<a href="viewmemberprofile.php?id=<?= $dispImg['profileimagepath'] ?>">
And viewmemberprofile.php needs to select that row from the DB:
SELECT * FROM profile_aboutyou WHERE id = $_GET['id']
The above SQL statement is pseudo-code; you need to write actual code to accomplish what it is describing, preferably using parameterized queries.
Im trying to add images to my database by using an image path, under the Image column, i have used varchar as the the type and i have inserted images/canin.jpg as the value.
When im trying to display this image using php it doesn't work, this is what i have :
$sql = "SELECT dog_id, dogbreed, image, sellercontact, location FROM dog";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Dog Breed</th><th>Image</th><th>Seller and Contact number</th><th>Location</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["dog_id"]."</td><td>".$row["dogbreed"]." <td>".$row["image"]."</td><td>".$row["sellercontact"]." </td><td>".$row["location"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
All the other information such as dog it, dog breed etc is being retrieved, but its not bringing back the image.
any ideas?
You need to use the HTML img tag to display the image.
Replace <td>".$row["image"]."</td> with <td><img src=\"".$row["image"]."\"/></td>
This should produce <td><img="images/canin.jpg"/></td> when executed
I asked this question before but no one could help me unfort. I have images and headings coming from database now the problem is that only one image is displaying(the last image) i need both to display.
here is my revised code
$query = "SELECT page_title, page_image FROM pages WHERE id='$page'";
$result = mysqli_query($connection, $query);
confirm_query($result);
while ($page_fetch = mysqli_fetch_assoc($result)) {
$page_title = $page_fetch['page_title'];
$images = $page_fetch['page_image'];
echo "<div class=\"content \">";
echo "<h3 class=\"words\"> $page_title </h3>";
echo "<img src='pics/" . $images . "' width=\"340\" height=\"252\" alt=\"\" />";
echo "</div>"; //end box
} // close while loop
here is my database for pages
page_id id page_image page_title
1 1 ocean.jpg have a look at the ocean
2 1 house.jpg The house
just some extra info the images must display dynamically , as they coming in from a form to db to this page
Your are returning your images from your query.
Check the view source page, You will find your error their.
Your returing image name will not match with the image which you got in the folder. (For the second one)
I am trying to insert into a database posted form values, retrieved from an html form that was generated via php. However it is not inserting the values, however I do see the values when I print the posted variables. Here is the code.
This is the form written in php that outputs html
<?php
$find = mysql_query("SELECT file_name FROM chemlab_files WHERE student_id = '$student_id'") or die ("Could not search!");
while($row = mysql_fetch_array($find)){
$file_name = $row['file_name'];
$_SESSION['file_name'] = $file_name;
echo "<label class='checkbox'><input type='checkbox' name='file_name[]' value='{$file_name}'>$file_name</label><br><input type='text' name='description[]' value='' placeholder='description'>";
}
?>
This is the form handler
//description
if(isset($_POST['description'])){
foreach($_POST['description'] as $description){
$file_name_description = mysql_real_escape_string($description);
//print_r($file_name_description);
}
}
$result = mysql_query("SELECT * FROM chemlab_files WHERE student_id='$student_id'") or die (mysql_error());
//if the student_id does exist, proceed
if($result==1){
//insert into chemlab_files update description
$insert_file_name_description = mysql_query("UPDATE chemlab_files SET description='$file_name_description' WHERE file_name='$file_name'");
}else{
echo 'something went wrong during sharing!';
exit();
I can print the variable $file_name_description but cannot insert into the database.
To be more clear the form has multiple fields, for ever file_name (checkbox with value ="$file_name") has a description field. When I do print_r for $file_name and $description I get the values for all the fields. However when I go to insert the description into the database nothing happens.
INSERT INTO table_name
VALUES (value1, value2, value3,...)
you need this.
If you are trying to create a new database record, shouldn't your sql be an INSERT statement like
INSTERT INTO chemlab_files SET (description, name) VALUES ('$file_name_description','$file_name')
I see two problems:
I do not understand why the text input in your HTML is an array (description[])
I don't see why you iterate it at the beginning, and you always set the $file_name_description to the last value. Are you trying to append all posted description inputs to the $file_name_description variable? If yes, then you should write .= instead of =
I have created a database namely newwork and table name property. In this database I have stored user details and one image. Now I want to display all the data from one field and stored image from the same field in a new page. Please help me for this program. The fields are property_id, property, location, image.
You really need two parts here (one to build the HTML and another to fetch/display the image):
Part 1: HTML builder
<?php
$res = mysqli_query($cnx, 'SELECT property_id, property, location from newwork';
if (res)
{
while ($row = mysqli_fetch_assoc($res))
{
echo '<span class="property">'.$row['property'].'</span>';
echo '<span class="location">'.$row['location'].'</span>';
echo '<span class="photo"><img src="image.php?id='.$row['property_id'].'" /></span>
}
}
Part 2: Image builder
<?php
$res = mysqli_query($cnx, 'SELECT image
FROM newwork
WHERE property_id='.intval($_REQUEST['id']));
if ($res)
{
$row = mysqli_fetch_assoc($res);
if (!empty($row))
{
header('Content-Type: image/jpg');
echo $row['image'];
exit;
}
}
header('Location: error_image.jpg',TRUE,302);
I suggest going though this step by step tutorial
http://www.tizag.com/mysqlTutorial/index.php