Im making a website like craigslist where people can make a posting of something they want to sell with a post title, post description, and email.
The problem:
I can pull all data from the db except for the image. When images are uploaded they are stored in C:\xampp\htdocs\uploads and the imagename is saved in the database with the rest of the info. Below is my php. Im trying to pull the name of the image from the db and use that to grab it like this
echo "<img src='uploads/".$image."' width='200'> ";
Im new to php so any tips are appreciated. Is this an ok method to store/retrieve user uploaded images?
Thanks
<?php
$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "mydb"; /* Database name */
$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
die("Connection failed: " . mysqli_connect_error());
}
$sql= "SELECT * FROM products";
$result = $con->query($sql);
$image = "SELECT image FROM products";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src='uploads/".$image."' width='200'> ";
echo "Title: " . $row["title"]. " Price: $" . $row["price"]. " <br> " . $row["description"]. " <br> " . $row["contact"] . " <br><br>";
}
} else {
echo "0 results";
}
$con->close();
?>
You need to use row same for image like:
$sql= "SELECT * FROM products";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<img src='uploads/".$row["image"]."' width='200'> ";
echo "Title: " . $row["title"]. " Price: $" . $row["price"]. " <br> " . $row["description"]. " <br> " . $row["contact"] . " <br><br>";
}
} else {
echo "0 results";
}
$con->close();
Related
I have a page that shows some of the information in the database. I'd like to add a link in each row (like making the first name a link) that I can click on that brings me to a page that shows the rest of that row's info (like a profile page). I'm thinking of making a link that passes the id to the profile page so that the profile page can gather the info.
I'm sure this is simple, but I'm just not getting it. How do I make the link show up in each row that sends only that row's id number? Because I'd rather not have to go into each row and make a special link.
Here is the code I have:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, FirstName, LastName, Phone, Email FROM Contacts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "
- Name: " . $row["FirstName"]. "
" . $row["LastName"]. "
" . $row["Phone"]. "
" . $row["Email"]. " <br>";
}
} else {
echo "0 results";
}
$conn->close();
?>```
Based on your example you could do this:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, FirstName, LastName, Phone, Email FROM Contacts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
echo "id: " . $row["id"] . " - Name: " . $row["FirstName"] . " " . $row["LastName"] . " " . $row["Phone"] . " " . $row["Email"] . " <br/>";
}
} else {
echo "0 results";
}
$conn->close();
?>
That profile.php page would check if the id is set with isset and query the data based on id simular to this: PHP & MYSQL: Select from where id=$id
Not tested and make sure to sanitize any user-generated variables.
I'm a bit new to MySQL and I am trying to create a search system. I am using some simple PHP code from w3schools.
I connect to my database:
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "img";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then echo the results.
$sql = "SELECT ID, Name, Description, Path FROM system";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["Name"]. " " . $row["Description"]. " " . $row["Path"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
My database contains one entry, but when I run this code, "0 Results" is echoed to the screen. Is there something I am doing wrong here?
yeaaaaaa.. so so far so bad... my codes aren't working .. but im happy that there is no errors. BUT it doesnt print anything.. just blank...
$studentno = isset($_POST['studentno']);
$con=mysqli_connect("localhost","root","","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$con->set_charset("utf8");
$result = mysqli_query($con,"SELECT * FROM data WHERE studentno = '$studentno'");
while($row = mysqli_fetch_array($result)) {
echo "<h3> Name: ". $row['last'] . " , " . $row['first']. " ".$row['middle']. " Birthdate: ". $row['birthdate'] ." Adviser: ". $row['adviser'] ."</h3>"; //these are the fields that you have stored in your database table data
echo "<h3> StudentNo.: ". $row['studentno'] . " ". " Age: " . $row['age'] ."</h3>";
echo "<h3> Track/Strand: ". $row['track'] . " ". "</h3>";
}
mysqli_close($con);
it is offline btw... just for a project..
Currently I am using this script
<?php
$servername = "**CENCERED**";
$username = "**CENCERED**";
$password = "**CENCERED**";
$dbname = "**CENCERED**";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT userID, users_name, usertype, status FROM tbluseraccounts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["userID"]. " <br> Name: " . $row["users_name"]. " <br> Account Type: " . $row["usertype"]. " <br> Status: " . $row["status"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
How can I add to the code so when $row["status"] say "Online" my text will be green or when it says "Offline" It will turn Red.
In your while loop you can do something like
while($row = $result->fetch_assoc()) {
if($row["status"] == "Online"){
$tcolor = "green";
}
elseif($row["status"] == "Offline"){
$tcolor = "red";
}
echo "ID: " . $row["userID"]. " <br> Name: " . $row["users_name"]. " <br> Account Type: " . $row["usertype"]. " <br> Status: <font color='".$tcolor."'>" . $row["status"]. "</font><br>";
}
Hope this can give you some idea.
Okay so my code works pretty well so far, it all goes through, my only problem is that when I try and print the unordered list and it's contents I get nothing. When I view my source code I have <ul> </ul>. There's a space, so surely something is happening.
This is my code, I have commented it slightly but what's happening is obvious:
$uname = mysqli_real_escape_string($link, $_SESSION['Username']); //Get username ready
$sql = mysqli_query($link, "SELECT * FROM users WHERE Username = '" . $uname . "'"); //SQL Query result
if(!$sql)
{
echo "Error retrieving User ID. Please try again. MySQL Error: " . mysqli_error($link);
}
elseif($row = mysqli_fetch_assoc($sql))
{
$uid = $row['UserID']; //Obtain UserID
}
else
{
echo "Error: " . mysqli_error($link) . "<br />" . $uname . " / " . $sql . " / " . $uid;
}
mysqli_free_result($sql);
$sql = mysqli_query($link, "SELECT * FROM auditions"); //Get everything from the auditions table
if(!$sql)
{
echo "Error retrieving auditions. Please try again later. Error: " . mysqli_error($link);
}
elseif($row = mysqli_fetch_assoc($sql))
{
if(mysqli_num_rows($sql)==0)
{
echo "Sorry, there are currently no open auditions. Please try back at a later date.";
}
else
{
echo "<ul>";
while($row = mysqli_fetch_assoc($sql))
{
echo "<li><a href='auditions.php?id=" . $row['AudID'] . "'>" . $row['AudName'] . "</a></li>";
}
echo "</ul>";
}
}
else
{
echo "Error: " . mysqli_error($link);
}
Where am I going wrong? The only thing it doesn't do is actually pick up any results and I've put some data into the table so there are entries! Otherwise it would say there aren't any. I've reversed this so it shows the message if there aren't 0 entries and that works. What am I doing wrong guys?
Thanks in advance.
You are fetching the result twice. Instead, only fetch the result in the while loop:
<?php
$sql = mysqli_query($link, "SELECT * FROM auditions"); //Get everything from the auditions table
if(!$sql)
{
echo "Error retrieving auditions. Please try again later. Error: " . mysqli_error($link);
}
else{
if(mysqli_num_rows($sql)==0)
{
echo "Sorry, there are currently no open auditions. Please try back at a later date.";
}
else
{
echo "<ul>";
while($row = mysqli_fetch_assoc($sql))
{
echo "<li><a href='auditions.php?id=" . $row['AudID'] . "'>" . $row['AudName'] . "</a></li>";
}
echo "</ul>";
}
}
?>
See this link for more information regarding mysql_fetch_assoc