How can i edit in php with sql - php

My Code -
<?php
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees ORDER BY id DESC") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
 Echo "<img src=http://www.mywebsite.com/upload/".$info['video'] ."> <br>";  //This is the name of my Video URL in MySQL
Echo "<b>Video</b> ".$info['Video'] . "<br> "; //This is the name of my Video Name in MySQL
Echo "<b>Author</b> ".$info['Author'] . "<br> "; //This is the name of my USER in MySQL
Echo "<b>Category</b> ".$info['category'] . "<br> "; //This is the name of Video Category in MySQL
Echo "<b>Description</b> ".$info['Description'] . "<hr>"; //This is the name of Description in MySQL
}
?>
As raw output the details are coming in List like this -
Image1
Video name1
Author name1
Category1
Description1
<hr>
Image2
Video name2
Author name2
Category2
Description2
Now want to Make put <div> Tags and Tags in between the Image, Author, Video Name
How can i put ? Cuz when i edit "<br>" and insert "<div class="thumb"></div><br>"
It shows error ! - Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/servin/public_html/upload/view.php on line 14
How can i edit mysql php code so that i can add the html codes + the All details must also shows like this

you cant use double quotes by this <div class="thumb"> you should use it like that <div class='thumb'> with single quote .because you already using it with echo , try this :
<?php
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees ORDER BY id DESC") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
echo "<div class='thumb'><img src='http://www.mywebsite.com/upload/".$info['video'] ." ' /> </div><br>"; //This is the name of my Video URL in MySQL
echo "<div class='thumb'><b>Video</b> ".$info['Video'] . "<br /> "; //This is the name of my Video Name in MySQL
echo "<div class='thumb'><b>Author</b> ".$info['Author'] . "<br /> "; //This is the name of my USER in MySQL
echo "<div class='thumb'><b>Category</b> ".$info['category'] . "<br /> "; //This is the name of Video Category in MySQL
echo "<div class='thumb'><b>Description</b> ".$info['Description'] . "<hr>"; //This is the name of Description in MySQL
}
?>
EDIT: if you want use html here better way
<?php
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees ORDER BY id DESC") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
?>
<div class='thumb'><img src='http://www.mywebsite.com/upload/<?php echo $info['video']; ?> ' /> </div><br />
<div class='thumb'><b>Video</b><?php echo $info['Video'] ;?><br />
<div class='thumb'><b>Author</b><?php echo $info['Author'] ;?><br />
<div class='thumb'><b>Category</b><?php echo $info['category'] ; ?><br />
<div class='thumb'><b>Description</b><?php echo $info['Description'] ; ?><hr />
<?php } ?>

Related

How to display multiples images from MySql

I would like to know how to display multiples images from MySQL in Html.
I have two files:
photogallery.php where I display the image and gallery.php where I have the php code.
This works but I can only display 1 image and I can't see all images!
Here is the code for photogallery.php where I display the photo:
<div align='left'>
<img src='gallery.php' height='95' width='95'/>
</div>
and here is the code for gallery.php:
session_start();
$host = "localhost";
$username = "root";
$password = "";
$db_name = "photos";
$tbl_name="gallery";
mysql_connect("$host","$username","$password")or die ("error22");
mysql_select_db("$db_name") or die("error2");
$ussername=$_SESSION['username'];
$query= mysql_query("SELECT * FROM $tbl_name where username='$ussername'");
while($row= mysql_fetch_assoc($query)){
$imageData=$row["image"];
//header("content-type:image/jpeg");
echo $imageData;
}
Thank you!
If you have image data stored on the database with a function like get_file_contents :
while($row= mysql_fetch_assoc($query)){
$imageData=$row["image"];
echo "<div align='left'>";
echo " <img src='data:image/jpeg;base64,"
. base64_encode($imageData) . "' height='95' width='95'/>";
echo "</div>";
}
if we assume that $row["image"] is path to image, then you should change your code like below:
...
while($row= mysql_fetch_assoc($query)){
$imageData=$row["image"];
?>
<div align='left'>
<img src='<?php echo $imageData;?>' height='95' width='95'/>
</div>
<?php
}
...
I don't execute my code, I want show you only idea

undefined index when trying to display number from database

i am trying to display the isbn number from my database when they click an image from another page and for some reason i keep getting this error
Notice: Undefined index: isbn on line 8
<html><title>Employee List</title> <head></head>
<body>
<h1> List of Employees </h1>
<?php
require_once("include/db_connect.php");
$db_link = db_connect("project");
$isbn = $_REQUEST['isbn'];
$query = "SELECT * FROM bookstore WHERE isbn = '$isbn'";
$result = mysql_query($query);
if ($row = mysql_fetch_assoc($result)) {
$isbn = $row['isbn'];
echo "<tr><td><strong>Isbn:</strong>". $isbn. "</td>";
}
?>
</body>
</html>
main program for my php program
<?php require_once("include/db_connect.php"); ?>
<html>
<head><title>Displaying Image files using PHP</title></head>
<body>
<h1>Bookworms Booksite</h1>
<style> #import "css/style.css";</style>
<div id = "nav">
<ul>
<li><a class = "navbutton" href="index.html">Home</a></li>
<li><a class = "navbutton" href="membership.html">Books</a></li>
<li><a class = "navbutton" href="classes.html">Shopping Cart</a></li>
<li><a class = "navbutton" href="">Checkout</a></li>
<li><a class = "navbutton" href="shop.html" id ="current" >About</a></li>
</ul>
</div>
<p></p>
<p></p>
<div id = "sideNav">
<ul>
<li>Enter Bookstore</li><br>
<li>Bookstore Administartion</li><br>
<li>Search</li>
</ul>
</div>
<?php
$db_link = db_connect("project");
// Retrieve table properties
$fields = mysql_list_fields("project", "bookstore");
$num_columns = mysql_num_fields($fields);
// Make a simple database query to select all columns and rows
$query = "SELECT * FROM bookstore";
$result = mysql_query($query) or die("SQL query failed");
// Display results as an HTML table. Note how mysql_field name
// uses the $fields object to extract the column names
echo '<table border="1" cellpadding = "5" >';
// Display the column names
echo "<tr>";
echo "</tr>";
// Loop over the rows of the table.
// $row contains the information for each row
// Refer to the names of the fields in the table
// Must ahow the path where the image files are held
while ($row = mysql_fetch_assoc($result))
{
echo "<tr>";
$isbn = $row['isbn'];
$title = $row['title'];
echo "<td><a href='bookDetail.php'><img src = 'images/". $row['image']. "'></a></td>";
echo "<a href='bookDetail.php? id= ". $isbn . "'>". $title . "</a><br/>";
echo "<td>". $row['isbn']."</td>";
echo "<td>". $row['title']."</td>";
echo "<td>". $row['author']."</td>";
echo "<td>". $row['pub']."</td>";
echo "<td>". $row['year']."</td>";
echo "<td>". $row['price']."</td>";
echo "</tr>";
}
echo "</table>";
// Free the resources and close the connection
mysql_free_result($result);
mysql_close($db_link);
?>
</body>
</html>
Line 8 is this :
$isbn = $_REQUEST['isbn'];
Therefore, your $_REQUEST['isbn'] variable is undefined. Do a print_r ($_REQUEST); to check your request parameters.
To use $_REQUEST['isbn'], you need the page to be called by a form and have a field with name='isbn', otherwise it won't exist.
More information about forms : http://www.php.net/manual/en/tutorial.forms.php
Actually the line 8 is "$_REQUEST['isbn'];" which has nothing to do with database. Are you sure you're passing the parameter to the page?

script to include like and unlike option with every mysql fetched result

while($info = mysql_fetch_array( $result ))
{
//Outputs the data
Echo "<b>".$info['name'] . " </b> :-<br>";
include('db.php');
$postid = 1;
$data = mysql_fetch_object(mysql_query('SELECT `like` , `unlike` FROM posts WHERE id=" '.$postid.' " '));
Echo "<html><body><a href='javascript:;' onclick='doAction($postid,like);'>Like (<span id='<?php echo $postid;?>_likes'>$data->like</span>)</a></html></body>";
Echo "<html><body><a href='javascript:;' onclick='doAction($postid,unlike);'>Unlike (<span id='<?php echo $postid;?>_unlikes'>$data->unlike</span>)</a></html></body>";
}
In the above script I have included like and unlike option with every fetched result.
I get the following result: like (0)unlike (0)
Every thing works perfectly. The only problem is that it doesn't increment when clicked on.
Please help me.
It works perfect in the simple code shown below:
<html>
<head>
<script src="http://code.jquery.com/jquery-1.3.2.min.js"></script>
<script>
function doAction(postid,type){
$.post('doAjax.php' , {postid:postid, type:type}, function(data){
$('#'+postid+'_'+type+'s').text(data);
});
}
</script>
</head>
<body>
<?php
include('db.php');
$postid = 1;
$data = mysql_fetch_object( mysql_query( 'SELECT `like` , `unlike` FROM posts WHERE id= " ' . $postid . ' " ' ) );
?>
<p>hello</p>
Like (<span id="<?php echo $postid; ?>_likes"><?php echo $data->like; ?></span>)
Unlike (<span id="<?php echo $postid; ?>_unlikes"><?php echo $data->unlike; ?></span>)
</body>
</html>
I also want to use PDO instead of mysql_fetch for the script shown above .
Thanks in advance.
Try replacing the 2 lines "Echo <html...> with:
echo "<div><a href='javascript:;' onclick='doAction($postid,like);'>Like (<span id='{$postid}_likes'>{$data->like}</span>)</a></div>";
echo "<div><a href='javascript:;' onclick='doAction($postid,unlike);'>Unlike (<span id='{$postid}_unlikes'>{$data->unlike}</span>)</a></div>";
Other issues:
-avoid using mysql_query, use PDO as suggested
-the query inside a loop will lead to poor performance. you should have a single query that gets the info and the likes (using JOINs). And inside the loop display the likes for each info.

Php search script to search all values in mysql table

I have a table of books which has info such as bname, bauthor, bdetails...etc
I want to provide a php script to search any thing from the table ...i tried a few scripts but it didnt work ..
Also i want to provide the option to provide option of selecting category ...
if the category is not selected then also the script should run...
Can anyone help me in this please ...
..
I m trying this :
$data = mysql_query("SELECT * FROM info WHERE upper($field) LIKE'%$find%'");
$result = mysql_query($data);
if (!$result) die ("DAtabase acces faild bc : ". mysql_error());
echo " <ul class=listbk>";
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo " <li class=bkd><a href=details.php?bid=".$row['pid']. "> <img src=images/".$row['bname']. ".jpg />
<ul class=showcasetitle> <span> ". $row['bname'] . " </span> </ul>
<p align=center style=text-align:center; font-size:10px;> By <span> ". $row['bauthor'] . "</span></p>
</a> <p align=center style=text-align:center; color: #F00;><span class=WebRupee style=color: #F00;>₹</span> ". $row['price'] . " only !!!</span></p>
</li> " ;
}
echo " </ul> ";
<?php
$text = 'John';
$query = "SELECT * FROM `books` WHERE MATCH(`bname`,`bauthor`,`bdetails`) AGAINST ('".$text."')";
?>

change an image via php dropdown

What I am trying to do is change the image when an item is selected from the drop down. This is part of a form so I cant have the value change. However the option value is the row id, that row would also contain the target for the image. But because the target 'file' is called outside the loop it isn't firing.
I read I have to call it within the loop first but can't get it to work. Could you look at the code below and throw me a hint?
Thanks
<?php
include ("conned-db.php");
$result = mysql_query("SELECT * FROM gallery")
or die(mysql_error());
echo "<select id='gallery_id' name='gallery_id' style='width:200px;' >";
while($row = mysql_fetch_array( $result ))
{
echo '<option value=' . $row['id'] . '>';
echo $row['gallery_name'];
echo '</option/>';
}
echo "</select>";
echo "</td>";
echo "<td colspan='2' rowspan='2'>";
echo '<img src=' .$row['file']. '/></td>';
?>
Try this, I think this is what you are looking for
If you want to do something like this you must use Ajax. Here you go for the link that helps you to understand about Ajax.
http://www.w3schools.com/php/php_ajax_database.asp
Note:
If you wanted it to be only PHP without Javascript, you would have to sacrifice the 'must not refresh' constraint, as the only way to submit the form is by pressing the button, and submitting the content.
This should work too. If the file locations of the images are available at the time you load the page using ajax is not a must. You have to use ajax if you need to query the server again to retrieve the required file location. The following code assumes that you have the location of the images for each item of the dropdown list at the time you load the page.
<select id='gallery_id' name='gallery_id' style='width:200px;'
onchange='document.getElementById("image").src=this.options[this.selectedIndex].title' >
<?php
while($row = mysql_fetch_array( $result ))
{
?>
<option value='<?php echo $row["id"]; ?>' title='<?php echo $row["file"]; ?>'>
<?php echo $row["gallery_name"]; ?>
</option>
<?php
}
?>
<img id="image" />
Here is a short example which implements java scrip and php where i update src of an image based on the id from the select you might want to change with specific src based on that id
<?php
include ("conned-db.php");
$item = $_GET["imageid"];
if ($item == "")
{
$item = 1;
}
$result = mysql_query("SELECT * FROM gallery")
or die(mysql_error());
?>
<select id='gallery_id' name='gallery_id' onChange="window.location='file.php?imageid='+this.value" style='width:200px;' >
<?
while($row = mysql_fetch_array( $result ))
{
echo '<option value=' . $row['id'] . '>';
echo $row['gallery_name'];
echo '</option/>';
}
?>
</select>
</td>
<?
echo "<td colspan='2' rowspan='2'>";
?>
<img src=' <?=$item?> '/></td>

Categories