Display data from database depending on the how many I have entered - php

In my database and more specific in the table named portfolio a user with a speficic username can store more than one data. The columns of the database are id,username,portfolio and portfolio_description
and I want to echo in my web page all the data for this username for example username=nbourlai. Until now I can echo only the first row for each username what is the loop that i can use in order to echo as many time is the id number.
Below is the current result and i want to create Portfolio and Portfolio description to be displayed with the appropriate values as many time as the highest id number is for the specific user.
Here is my code...
$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
$row = mysql_fetch_row($portfolio);
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";
Can you help me please?

Are you looking for while loop like this?
$portfolio = queryMysql("SELECT portfolio,portfolio_description FROM portfolio WHERE username='nbourlai'");
echo "<h2>Portfolio</h2>";
while($row = mysql_fetch_row($portfolio)){
echo "Portfolio: ";
echo stripslashes($row[0]) . "<br/>Portfolio Description: ";
echo stripslashes($row[1]) . "<br clear=left /><br />";
}
or, like this?
$portfolio = queryMysql("SELECT username,portfolio,portfolio_description FROM portfolio ORDER BY username");
$username = '';
while($row = mysql_fetch_row($portfolio)){
if($username != $row[0]){
echo "<h1>".$row[0]."</h1>";
echo "<h2>Portfolio</h2>";
echo "Portfolio: ";
echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
echo stripslashes($row[2]) . "<br clear=left /><br />";
}else{
echo "Portfolio: ";
echo stripslashes($row[1]) . "<br/>Portfolio Description: ";
echo stripslashes($row[2]) . "<br clear=left /><br />";
}
$username = $row[0];
}

Related

Mysql Field Data not displaying when a link is clicked?

I'm trying to get data from a database if a link is clicked.
I used the example codes suggested from this example -Getting mysql field data when a link is clicked?
But it doesn't work when I click on a link nothing comes up.
main.php
<?php
include('conn.php');
$sql2 = "SELECT Title FROM addpromo";
$result2 = mysql_query($sql2);
echo "<div id=\"links\">\n";
echo "<ul>\n";
while ($row2 = mysql_fetch_assoc($result2)) {
echo "<li> <a href=\"fullproject.php?title=\""
. urlencode($row2['Title']) . "\">"
. htmlentities($row2['Title']) . "</a>\n</li>";
}
echo "</ul>";
echo "</div>";
?>
This is displaying correct.but when I click at a link nothing is showing up in fullproject.php, Just a blank page.
fullproject.php
<?php
// Connect to server.
include('conn.php');
$projectname = isset($_GET['Title']);
$sql1 = "SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title'] . "<br />";
echo "<br /> ";
}
?>
Can someone help me to fix this, or any other way to make this(to get data from a database if a link is clicked) possible?
Change to this
main.php
<?php
include('conn.php');
$sql2="SELECT Title FROM addpromo";
$result2=mysql_query($sql2);
echo '<div id="links">';
echo '<ul>';
while($row2 = mysql_fetch_assoc($result2)){
echo '<li>'.htmlentities($row2['Title']).'</li>';
}
echo '</ul>';
echo '</div>';
?>
fullproject.php
<?php
if(isset($_GET['title'])){
include('conn.php');
$projectname= $_GET['title'];
$sql1="SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title']. "<br />";
echo "<br /> ";
}
}
?>
This is storing a boolean value $projectname= isset($_GET['Title']);, whether or not the title is set. Instead use $projectname = $_GET['Title'];
isset returns a boolean value (true/false) and you want the actual value of the variable:
$projectname= $_GET['title'];
Furthermore, you have to pass only the title as the URL parameter, without enclosing it within quotes. So there is an error in this line:
echo "<li> <a href=\"fullproject.php?title=" . urlencode($row2['Title']) . "\">"
Note the lack of \" after title=

PHP Form data not stored when submitted?

I am working on an synonym/alias manager database where the user has the ability to store an association they feel is right. Say the user types in "rabbit" and no synonyms or aliases are found. The user then decides to associate "rabbit" with "bunny" and stores that into a database. Anytime any other user types in "bunny" the results for "rabbit" will appear. However, I am trying to implement a polling system asking the user if they feel the association is correct. If they think "bunny" fits "rabbit" then they vote yes, otherwise no. This is where I am stuck. As soon as I load the poll and press submit everything disappears and nothing gets sent to the database. Code is below:
$query = "SELECT * from searchtestdb where engname in ( SELECT synonyms.synonym FROM words LEFT JOIN synonyms ON synonyms.word_id = words.word_id WHERE word LIKE '%$searchBox%') "; // Query for animals in db
$query = mysql_query($query);
if(mysql_num_rows($query) == 0)
{
echo "<h2>Aliases: </h2>";
echo "Sorry, but we can not find an alias to match your query.";
echo "<br> ";
}
else
{
echo "<h2> Results using Alias: </h2>";
while($result = mysql_fetch_array($query))
{
$query2 = "SELECT * from searchtestdb where engname LIKE '%".$result['engname']."%';";
$result2 = mysql_query($query2);
while($row = mysql_fetch_array($result2))
{
print "<h4>Latin Name: </h4> ";
echo $row["latname"];
echo "<br> ";
print"<h4>English Name:</h4> ";
echo $row["engname"];
echo "<br>";
print "<h4> Species: </h4> ";
echo $row["spectype"];
print "<h4>Characteristics: </h4> ";
echo $row["charc1"];
echo "<br>";
echo $row["charc2"];
echo "<br>";
echo $row["charc3"];
echo "<br>";
}
}
$self = $_SERVER['PHP_SELF'];
print "<form method='post' action='$self' >\n";
print "<h4>Alias Association Correct? : </h4>";
print "<p>" .
"<input type='radio' name='vote' id='vote' value='1' /> \n" .
"Yes" .
"<input type='radio' name='vote' id='vote' value='2' /> \n" .
"No" .
"</p> \n" .
"<p>" .
"<input type='submit'name='submitVote' value='Submit' />" .
"\n </p> \n" .
"</form> \n" .
$vote=htmlentities($_POST['vote']);
echo $vote;
mysql_connect(----------------------) or die(mysql_error());
mysql_select_db("-----------") or die(mysql_error());
if($vote == 1)
{
mysql_query("INSERT INTO items(yes, uNo, word_id) VALUES ('0', '0', bunny');");
mysql_query("UPDATE items SET yes=yes+1 WHERE word_id='bunny';");
echo 'Thanks for voting Yes!';
}
if($vote == 2)
{ mysql_query("INSERT INTO items(word_id) VALUES ('".$result['engname']."'') ");
mysql_query("UPDATE items SET uNo=Uno+1 WHERE word_id='".$result['engname']."'");
echo "changos";
}
}
In a nutshell I made a small mistake:
$self = $_SERVER['PHP_SELF'];
should be
$self = $_SERVER['POST'];
Thanks Anyway.

mysql UPDATE not updating the mysql table row

This is my first time with UPDATE (mysql) this code isn't send the updates to my mysql database/table/row. I spent hours on php.net , but from what I can see at my current level or php knowledge its right and since I'm wrong, them I' dumb =}.
I turned loggin on mysql - nothing useful
This is from ZEND server: So it appears to be passing, but it is not updating in the mysql database.
Function Name: mysql_query
Function Arguments:
'UPDATE ads SET adcode = \'7000sbjhbjhbhjb\' WHERE ads_ID = 8'
any pointers/help is much appreciated.
<?php
require 'config.php';
// Report all Errors
//error_reporting(-1);
//ini_set('display_errors','On');
//connect to DB
mysql_connect("$host", "$db_user", "$db_password");
mysql_select_db("$db_name");
$query = "SELECT * FROM ads";
$result = mysql_query($query);
$num = mysql_numrows($result);
echo "<h1><center>AD's Currently Available</center></h1><br /><br />";
$i=0;
while ($i < $num) {
$ID = mysql_result($result,$i,"ID");
$adname = mysql_result($result,$i,"adname");
$currentadcode = mysql_result($result,$i,"adcode");
// Delete AD item by ID number
$action = (isset($_REQUEST['action']));
if ($_GET['action'] == "deletead") { // remove AD
mysql_query("DELETE FROM ads where ID = '$_GET[IDnum]'");
$i=$i++;
header("Location: " . $_SERVER['PHP_SELF']);
}
$letknown = "<b>AD removed</b><br />";
**// Edit AD code
if (isset($_POST['editad' . $ID])) {
$newadcode = mysql_real_escape_string($_POST['adcode' . $ID]);
$doedit = "UPDATE ads SET adcode = '" . $newadcode . "' WHERE ads_ID = " . $ID;
$updatead = mysql_query($doedit);
header("Location: " . $_SERVER['PHP_SELF']);**
}
$letknown = "<b>Ad Edited</b><br />";
echo "<b>$ID : $adname</b><br />Delete Ad<br /><br />\n";
echo "Preview :<br /><div class=\"adcode\">$currentadcode</div><br /> \n";
echo "<br />\n";
echo "<form action=\"displayads.php\" name=\"addAD$ID\" method=\"post\">\n";
echo "AD code (can be any type of script) text link, javascript or banner :<br />\n";
echo "Code :<br /><textarea name=\"adcode$ID\" wrap=\"physical\" cols=\"60\" rows=\"5\" onKeyDown=\"textCounter(document.addAD$ID.adcode$ID,document.addAD$ID.remLen$ID,5000)\" onKeyUp=\"textCounter(document.addAD$ID.adcode$ID,document.addAD$ID.remLen$ID,5000)\">$currentadcode</textarea>";
echo "<br /><input readonly type=\"text\" name=\"remLen$ID\" size=\"3\" maxlength=\"3\" value=\"5000\">Characters Left \n";
echo "**<input type=\"submit\" name=\"editad$ID\" value=\"Edit AD Code\">**</form>\n";
echo "<br /><hr />";
$i++;
}
?>
You are missing closing double quotes :
$doedit = "UPDATE ads SET adcode = '" . $newadcode . "' WHERE ads_ID = '". $ID."'";
Or simply write your query like this :
$doedit = "UPDATE ads SET adcode = '$newadcode' WHERE ads_ID = '$ID'";
Use LIMIT 1 if you want to update only a single row

Display current logged in user's profile

I've been working on code for student registration. I finished coding for my log in.
What I'm having a problem with is showing the user's profile: student fname, lname, program, gender and year level in textboxes. I'm using MySQL as my back end.
My code is like this:
<?php
// connects to the database
$mysqli = new mysqli("localhost", "root", "");
$query = 'SELECT fname, lname, program, gender, year FROM students WHERE fname = '.$_SESSION['myusername'];
$mysqli->query($query);
echo "<div align=\"center\">";
echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
echo "<b>First name:</b> ". $_POST['fname'];
echo "<br /><b>Last name:</b> ".$_POST['lname'];
echo "<br /><b>Program:</b> ".$_POST['program'];
echo "<br /><b>Year:</b> ".$_POST['year'];
echo "<br /><b>Gender:</b> ".$_POST['gender'];
echo "</div>";
?>
This is my code for mainstudent.php and checklogin.php.
Here you go:
<?php
session_start();
// connects to the database
$mysqli = new mysqli("localhost", "root", "");
$query = "SELECT fname, lname, program, gender, year FROM students WHERE fname = '".$_SESSION['myusername']."'";
if($result = $mysqli->query($query))
{
while($row = $result->fetch_assoc())
{
echo "<div align=\"center\">";
echo "<br />Your <b><i>Profile</i></b> is as follows:<br />";
echo "<b>First name:</b> ". $row['fname'];
echo "<br /><b>Last name:</b> ".$row['lname'];
echo "<br /><b>Program:</b> ".$row['program'];
echo "<br /><b>Year:</b> ".$row['year'];
echo "<br /><b>Gender:</b> ".$row['gender'];
echo "</div>"
}
$result->free();
}
else
{
echo "No results found";
}
?>
please add your password in session as well

extract array values from a function

Below is a function in PHP I created which returns some product information in an array. On the web page where I am calling this function I want to be able to specify exact array elements for example just the product description ($result2['itm_desc']). Please can someone point me in the right direction as to do this. I would assume you call a function like fetch array but i'm not quite sure how to execute this.
public function getAllReelImages(){
$sql = "SELECT id FROM $this->table3 WHERE itm_cat = 2 ORDER BY id ASC";
echo "<br /><br />";
$stmt = mysqli_query($this->connection, $sql);
/*fetch values*/
while($result = mysqli_fetch_array($stmt)){
echo "<br /><br />";
$sql2 = "SELECT itm_details.id,itm_details.itm_make,itm_details.itm_model,itm_details.itm_desc,itm_pic_detail.itm_pic_name, itm_value.itm_sale_price
FROM
itm_details, itm_pic_detail, itm_value
WHERE
itm_details.id = {$result['id']} AND itm_pic_detail.id = {$result['id']}
AND itm_value.id = {$result['id']} ORDER BY id ASC";
$stmt2 = mysqli_query($this->connection, $sql2);
while($result2 = $stmt2->fetch_array()){
echo ($result2['id']); echo"<br />";
echo ($result2['itm_make']); echo"<br />";
echo ($result2['itm_model']); echo"<br />";
echo ($result2['itm_desc']); echo"<br />";
echo ($result2['itm_sale_price']); echo"<br />";
echo "<img src='$this->dir"."{$result2['itm_pic_name']}'><br />";
echo"<br />";
echo $value;
}
}
}
First of all.. Your code is pretty bad. I made it little bit more clear. For example: echo ($result2['id']); echo"<br />"; TO-> echo $result2['id'] . '<br />'; (for future references.)
They way your code is at the moment. You wont add anything to an array, since you echo them right away. I made my version, that would first create the array and then later you can use that array..in any way you want :)
And doesn't the ORDER BY use by default as ASC?
I also changed the first query more dynamic.
NOTE: I'm not very good with mysql table joining, somebody must validate the second query. But in my eyes, its absolutely incorrect. However, I'm posting this in the reference for creating an array with a function and then displaying it.
NOTE2: I hope your question was about PHP o.0
# This should be in some class ?!
function getAllReelImages ($category) {
$sql = "SELECT * FROM `" . $this->table3 . "` WHERE `itm_cat` = '" . $category . "' ORDER BY `id` ASC";
$stmt = mysqli_query($this->connection, $sql);
while($result = mysqli_fetch_array($stmt)) {
$sql2 = "SELECT `itm_details.id`, `itm_details.itm_make`, `itm_details.itm_model`, `itm_details.itm_desc`, `itm_pic_detail.itm_pic_name`, `itm_value.itm_sale_price` FROM `itm_details`, `itm_pic_detail`, `itm_value` WHERE `itm_details.id` = '" . $result['id'] . "' AND `itm_pic_detail.id` = '" . $result['id'] . "' AND `itm_value.id` = '" . $result['id'] . "' ORDER BY `id` ASC";
$stmt2 = mysqli_query($this->connection, $sql2);
while($result2 = $stmt2->fetch_array()){
$returns[$result2['id']]['id'] = $result2['id'];
$returns[$result2['id']]['itm_make'] = $result2['itm_make'];
$returns[$result2['id']]['itm_model'] = $result2['itm_model'];
$returns[$result2['id']]['itm_desc'] = $result2['itm_desc'];
$returns[$result2['id']]['itm_sale_price'] = $result2['itm_sale_price'];
$returns[$result2['id']]['itm_pic_name'] = $this->dir . $result2['itm_pic_name'];
}
}
return $returns;
}
# Display the results:
echo '<br /><br />';
foreach (getAllReelImages(2) as $img_id => $img_value) {
echo $img_id . '<br>';
echo $img_value['itm_make'] . '<br>';
echo $img_value['itm_model'] . '<br>';
echo $img_value['itm_desc'] . '<br>';
echo $img_value['itm_sale_price'] . '<br>';
echo $img_value['itm_pic_name'] . '<br>';
echo '<hr>';
}
echo '<br />';

Categories