I have code for list values from database but when I add them to echo "<div>" it stops working
My code:
$sql = "SELECT name, size, type, username FROM Files";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<div class='filediv'>";
echo "<a href='uploads/$row['name']' download>$row['name']</a>";
echo "<p>Size: $row['size']KB</p>";
echo "<p>Type: $row['type']</p>";
echo "<p>Creator: $row['username']</p>";
echo "</div>";
echo "<hr>";
}
}
you have a syntax error
echo "<div class='filediv'>";
echo "<a href='uploads/".$row['name']."' download>".$row['name']."</a>";
echo "<p>Size:". $row['size']."KB</p>";
echo "<p>Type: ".$row['type']."</p>";
echo "<p>Creator: ".$row['username']."</p>";
echo "</div>";
echo "<hr>";
You can use {} for print php variables in html.
echo "<div class='filediv'>";
echo "<a href='uploads/{$row['name']}' download>{$row['name']}</a>";
echo "<p>Size: {$row['size']}KB</p>";
echo "<p>Type: {$row['type']}</p>";
echo "<p>Creator: {$row['username']}</p>";
echo "</div>";
echo "<hr>";
Related
I am trying to create a post system, where, if the user writes only text and doesn't upload an image, a blank image error should not appear (only plain text should appear after submitting).
So I have to check whether an image is uploaded or not.
The below code does the work of posting only Text.
while ($row = mysqli_fetch_array($result)) {
if (($_FILES['image1']['tmp_name'])==1){
echo "<br>";
echo "<div class='postuser'>";
echo "<div id='img_div1' class='caption'>";
echo "<p>".$row['image_text1']."</p>";
echo "<img src='images/".$row['image1']."' width='288px'>";
echo "</div>";
echo "</div>";
}
else
{
echo "<br>";
echo "<div class='postuser'>";
echo "<div id='img_div1' class='caption'>";
echo "<p>".$row['image_text1']."</p>";
echo "</div>";
echo "</div>";
}
}
PS: I have used file_exists() function and is_uploaded_file() function with both $_FILES['image1']['tmp_name'] and $_FILES['image1']['name'], tried many other ways, but to no help.
Hope nobody closes the question this time, please.
This code solved my query
while ($row = mysqli_fetch_array($result)) {
echo "<br>";
echo "<div class='postuser'>";
echo "<div id='img_div1' class='caption'>";
echo "<p>".$row['image_text1']."</p>";
if(!empty($row['image1']))
{
echo "<img src='images/".$row['image1']."' width='288px' height='250px' class='postedimg' >";
}
else
{
echo "";
}
echo "</div>";
echo "<p>".$row['sr']."</p>";
echo "</div>";
}
?>
My code is working as for my needs. But the only thing bugging me is
the "else" is not working. When i search for a correct record the
record will appear and it was running fine. But if i Incorrectly
search a record nothing will happen. i am expecting "Records not Found" will echo but nothing happen.
}else{
echo "Records not found";
}
This is the whole code.
<?php
$conn = mysqli_connect("localhost", "root", "", "my1stdb") or die("could not connect");
$set = $_POST['search'];
if ($set) {
$show = "SELECT * FROM users where email='$set'";
$result = mysqli_query($conn, $show);
while ($rows = mysqli_fetch_array($result)) {
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
} else {
echo "Records not found";
}
?>
</table>
You need to use mysqli_num_rows() along with mysqli_fetch_assoc():-
<?php
$conn=mysqli_connect("localhost","root","","my1stdb") or die("could not connect");
$set = $_POST['search'];
if($set) {
$show="SELECT * FROM users where email='$set'";
$result=mysqli_query($conn,$show) or die(mysqli_error($conn));
if(mysqli_num_rows($result)>0){ // check data present or not
while($rows=mysqli_fetch_assoc($result)){ // for lighter array due to associative indexes only
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
}else{
echo "Records not found";
}
}else{
echo "Please insert search term";
}
?>
</table>
Note:- Your code is wide-open for SQL INJECTION. to prevent from it use prepared statements
Reference:-
mysqli prepared statements
PDO prepared statements
You could count the number of results returned.
if($set) {
$show="SELECT * FROM users where email='$set'";
$result=mysqli_query($conn,$show);
$recordCount = 0;
while($rows=mysqli_fetch_array($result)){
$recordCount++;
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
if($recordCount==0){
echo "Records not found";
}
}
I try to fetch all image from database but only one image is shown.
this is my code:
<?php
$query = "SELECT id, name, image, price FROM products ORDER BY name";
$stmt = $con-> prepare ( $query );
$stmt->execute();
$num = $stmt->rowCount();
if($num>0){
//some html code
this is my while loop:
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
echo "<div class='inner'>";
//creating new table row per record
echo "<ul>";
echo "<li>";
echo "<a class='thumb' href='images/{$image}'>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<span class='hoodnamecarousel'>{$name}</span>";
echo"</a>";
echo "</li>";
echo "</ul>";
echo "</div>";
echo "</div>";
This is second part of displaying image after click on any image:
echo "<div id='thumbs2' style='display:none;'>";
echo "<div class='inner'>";
echo "<ul>";
echo "<li>";
echo "<a class='thumb' href='images/{$image}'></a>";
echo "<div class='product-id' style='display:none;'>{$id}</div>";
echo "<span class='hoodnamecarousel'>{$name}</span>";
echo "<br />";
echo "<span class='price'>{$price}</span>";
echo "<br />";
echo "<a href='add_to_cart.php?id={$id}&name={$name}' class='button btn btn-primary'>";
echo "افزودن به سبد <span class='glyphicon glyphicon-shopping-cart'></span>";
echo "</a>";
echo "</li>";
echo"</ul>";
echo "</div>";
echo "<div id='closeBtn'>بستن</div>";
}
echo "</div>";
echo "</div>";
}
else {
echo "محصولات در حال بروزرسانی می باشند.";
}
?>
this code only display 1 image and doesn't show other images from database
please help me!
I'm trying to to create a users profile, it is suposed to get the id from the url, then get all the info from the user with that id.
<?php
session_start();
$userID = $_GET["id"];
$getUserNames = "SELECT * FROM users where id=$userID";
$result = $conn->query("getUserNames");
$row = mysqli_fetch_assoc($result);
if (isset($_GET["id"])) {
echo "<h1>";
echo $row["username"];
echo "</h1>";
echo "<p><b>Name: </b>";
echo $row["username"];
echo "</p>";
echo "<p><b>Password: </b>***** (<a href='#'>Change Password</a>)</p>";
} else {
if (isset($_SESSION["username"])) {
echo "<h1>";
echo $_SESSION["username"];
echo "</h1>";
echo "<p><b>Name: </b>";
echo $_SESSION["username"];
echo "</p>";
echo "<p><b>Password: </b>***** (<a href='#'>Change Password</a>)</p>";
} else {
echo "<p>You need to be logged in too see your profile!";
}
}?>
I get nothing from the echo and there is no error.
Please help!
You have an error at $conn->query("getUserNames"); which should be: $conn->query($getUserNames);
The complete new code with the fix looks like this:
<?php
session_start();
$userID = $_GET["id"];
$getUserNames = "SELECT * FROM users where id=$userID";
$result = $conn->query($getUserNames);
$row = mysqli_fetch_assoc($result);
if (isset($_GET["id"])) {
echo "<h1>";
echo $row["username"];
echo "</h1>";
echo "<p><b>Name: </b>";
echo $row["username"];
echo "</p>";
echo "<p><b>Password: </b>***** (<a href='#'>Change Password</a>)</p>";
} else {
if (isset($_SESSION["username"])) {
echo "<h1>";
echo $_SESSION["username"];
echo "</h1>";
echo "<p><b>Name: </b>";
echo $_SESSION["username"];
echo "</p>";
echo "<p><b>Password: </b>***** (<a href='#'>Change Password</a>)</p>";
} else {
echo "<p>You need to be logged in too see your profile!";
}
}?>
I'm trying to style the output of each echo.
Ideally I'd like to use <span class=""> </span> for each echo, but I'm not too sure how to achieve this.
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo $row['Title'];
echo $row['Message'];
echo "<img src='".$row['Image']."'/>";
}
mysql_close($con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo "<span class=\"myclass\">$row['Date']</span>";
echo "<span class=\"myclass\">$row['Title']</span>";
echo "<span class=\"myclass\">$row['Message']</span>";
echo "<img src='".$row['Image']."'/>";
}
mysql_close($con);
or, much nicer, in a table:
$result = mysql_query("SELECT * FROM Blog");
echo "<table>"
while($row = mysql_fetch_array($result)) {
echo "<tr>"
echo "<td>$row['Date']</td>";
echo "<td>$row['Title']</td>";
echo "<td>$row['Message']</td>";
echo "<td><img src='".$row['Image']."'/></td>";
echo "</tr>"
}
echo "</table>"
mysql_close($con);
You then can style each row and column with a class.
Try this:
$prepend = "<span class=''>";
$append = "</span>";
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $prepend.$row['Date'].$append;
echo $prepend.$row['Title'].$append;
echo $prepend. $row['Message'].$append;
echo $prepend."<img src='".$row['Image']."'/>".$append;
}
mysql_close($con);
I'd create a function that does this:
function decorated_echo($text) {
echo '<span class="myclass">' . $text . '</span>';
}
This way, you don't have to repeat this everytime you want this behaviour.
You are guessing right, just add required html in the echo:
echo '<span class="yourclass"><img src="'.$row['Image'].'" /></span>';
or you can just put inline style if no css file is loaded:
echo '<span style="color:red;"><img src="'.$row['Image'].'" /></span>';