Getting mysql field data when a link is clicked? - mysql

I'm trying to get data from a database if a link is clicked.
Basically i have a main.php that contains this:
$sql2="SELECT projectsid, projectname, description,
SUBSTRING(description,0,80) FROM projects";
$result2=mysql_query($sql2);
while($row2 = mysql_fetch_assoc($result2)) {
echo "<div id=\"links\">";
echo "<ul>";
echo "<li> <a href=\"fullproject.php\">" . $row2['projectname'];
$_SESSION['projectname']= $row2['projectname'];
echo "<em>" . $row2['description'] . "</em></a></li>";
echo "</ul>";
echo "</div>";
}
This displays a list of names and a brief description.
Proj1
description
Proj2
description
What i'd like to do is to display the full contents of a project if that one is clicked.
fullproject.php
<?php
session_start();
$projectname= $_SESSION['projectname'];
// Connect to server.
require ("connect.php");
$sql1="SELECT projectsid, projectname, programme, difficult, requirements,
resources, description, contact, gsize, size
FROM projects WHERE projectname = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['projectname']. "<br />";
echo "Programme : " . $row1['programme'] . "<br />";
echo "Difficult : " . $row1['difficult'] . "<br />";
echo "Requirements : " . $row1['requirements'] . "<br />";
echo "Resources : " . $row1['resources'] . "<br />";
echo "Description : " . $row1['description'] . "<br />";
echo "Contact : " . $row1['contact'] . "<br />";
echo "Group size : " . $row1['gsize'] . " " . $row1['size'] . "<br />";
echo "<br /> ";
}
Whenever I click any of the project list items, it only displays the last itemin the list.
I believe this is happening because when the while loop ends the session variable is set to the last one.
Can someone tell me how to fix this?
Thanks

Try using GET variable instead of session
in main.php use
echo "<li> <a href='fullproject.php?projectname=$row2[projectname]'>";
in fullproject.php declare a variable and initialize it to the get variable
$projectname= $_GET['projectname'];

There is a lot wrong with this code.
1) You are trying to send the primary key of the table via 2 different methods in main.php: in the session and via the path of the URL. This is just bad practice - the reason you have access to different conduits for passing data is that they all have different semantics, but even then, sending the same thing twice then there's always going to be a risk that the receiving end may see different values and must terefore have a way of reconciling the discrepancy.
2) You are using the session to pass a transactional data item. This is a bad idea - the data item relates to a specific transition - not to the session. $_SESSION should only be used for storing data relating to the sesion - not to navigation or transactions.
3) The other conduit you provided for passing the data item is by appending it to the path within the URL. While in some circumstances this approach has benefits it also has complications specific to the webserver the code is implemented on - you've not provided any explanation of why you are using this apporach instead of a conventional $_GET or $_POST var.
The reason your code is failing is because you can only store a single value in $_SESSION['projectname']. Even if you stored an array of values in the session, there is no mechanism for the webserver to know which of these values was selected when the code was delivered to the browser.
Your HTML is badly structured and poorly formatted too.
The code you've shown does not match the description you've given (it does not display the project name).
Your code is also wide open to SQL injection attacks.
Change it so that in main.php:
echo "<div id=\"links\">\n";
echo "<ul>\n";
while($row2 = mysql_fetch_assoc($result2)){
echo "<li> <a href=\"fullproject.php?project=\""
. urlencode($row2['projectname']) . "\">"
. htmlentities($row2['projectname']) . "</a>\n";
echo "<br /><em>" . $row2['description'] . "</em></li>";
}
echo "</ul>";
echo "</div>";
And in fullproject.php
<?php
session_start();
require ("connect.php");
$projectname= mysql_real_escape_string($_GET['projectname']);

echo"<a href='fullproject.php?project_id='".$row2['project_id'].
"> $row2['projectName']</a>";
in fullproject.php
$project_id=$_GET['project_id'];
then you can use this id to display contents of that project id from database

Related

UNIX Script - Coloring HTML content based on current date

I want to write a script which will colour lines of the input green if they represent the current date (i.e. on 2017-07-13, the second line should be green), red otherwise.
Input format (CSV):
Feed1,2017-07-01
Feed2,2017-07-13
Feed3,2017-07-03
Here is what I have so far:
while IFS="," read dtts feed; do
if [ "$dtts" == "$DATEVALID" ]; then
echo "<tr>"
echo "<td><font color=green>$dtts</font></td>"
echo "<td bgcolor=green>$feed</td>"
echo "</tr>"
else
echo "<tr>"
echo "<td><font color=red>$dtts</font></td>"
echo "<td bgcolor=red>$feed</td>"
echo "</tr>"
fi
done < $INPUTFILE.csv
But it is showing incorrect output (all lines have the same colour).
I see a couple of possible problems with your code. Some have been fixed when editing your original question, please consider formatting your code properly when asking a question on SO.
You are reading fields from the CSV in the incorrect order.
read dtts feed # This means the first field is $dtts, the second is $feed
So the while loop should probably look like
while IFS="," read feed dtts; do
And finally, you did not provide the code responsible for $DATEVALID or $INPUTFILE. An important question is whether $DATEVALID is in the same format as the one used in your input file. To obtain such date within a shell script, you could try:
DATEVALID=`date "+%Y-%m-%d"`
This should give you today's date in the YYYY-MM-DD format you need. To summarise:
DATEVALID=`date "+%Y-%m-%d"`
INPUTFILE="input.csv"
while IFS="," read feed dtts; do
echo "<tr>"
if [ "$dtts" == "$DATEVALID" ]; then
echo "<td><font color=green>$dtts</font></td>"
echo "<td bgcolor=green>$feed</td>"
else
echo "<td><font color=red>$dtts</font></td>"
echo "<td bgcolor=red>$feed</td>"
fi
echo "</tr>"
done < $INPUTFILE
First problem -- your data does not match the variables you are specifying in your read command.
First question -- what is the value of $DATEVALID ?
echoing your variables will go a long way toward troubleshooting these types of problems.

PDO: How can I etrieve an image address from MySQL and print it?

I'm stepping up from mysql to PDO and I'm getting close as all my fields print now except the images. These are held in a separate file with only the address in the database. I can retrieve these in mysql but I'm having trouble doing it in PDO.
Where I should see the image, the best I've been able to achieve is getting it to print "Arrayh" at the end of each line. I don't seem to be able to find how to do this anywhere so here I am again. This is the part of the file that prints.
$STM->bindParam(':Country', $Country, PDO::PARAM_STR);
$STM->execute();
$row= $STM->fetchAll();
$img_url = "http://www.xxxxx.net/images/";
if(count($row)){
foreach($row as $data){
echo $data['Country'] ." ".$data['Year']." ".$data['Description']." ".$data.$img_url['Images']."<br />";
}
}else {
echo 'no row found';
}
Say if you called your image field img_url
foreach($row as $data){
echo "<img src='"$data['img_url']."'><br />";
}
I've eventually got it to work so I will put what I've used here otherwise this question will be useless for people in the future who have the same problem. The echo data line is replaced by two lines :
echo $data['Key'] ." ".$data['Country'] ." ".$data['Year']." ".$data['Description']." ";
echo "<img src='$img_url".$data['Images']."' <br /><br />" ;
"<br />";
Now I just need to get it into a table to neaten it up a bit. Thanks to all who helped.

How to repeat different pic?

I wan to always display the different pic. So there's a problem with looping the different pic. How do i do that?
index.php:
<?php
$result = mysql_query("SELECT * FROM table2", $connection );
echo '<ul>';
while($row = mysql_fetch_array($result)) {
if ($row)
echo "<li>";
echo "<img src='code.php?id=3'>";
echo "&nbsp";
echo "</p>";
echo "</li>";
echo "<br/>" ;
}
echo '</ul>';
mysql_close($connection);
?>
You need to pull the id value from the database. If you have a column called, maybe id you would want to put:
while($row = mysql_fetch_array($result)){
if ($row){
echo "<li>";
echo "<img src='code.php?id=".$row['id']."'/>"; // see what I did there?
echo "&nbsp";
echo "</p>"; // take this out
echo "</li>";
echo "<br/>"; // take this out
}
}
P.S. - don't write new code using the deprecated mysql extension. Use PDO or mysqli at least. And don't put <br />s between your <li>s, or close unopened <p>s. And, generally speaking, don't store your images in your database - just store the path to the image and put the images themselves in a folder on the server where they belong.
And please format your code - it is hard to read with no indentation or separation of files (your if statement was not properly enclosing what should have been conditioned statements). And mysql_real_escape_string is not as cool as you think it is.
Hope this helps.

How to use MySql Query in CI Bonfire

I have this code on my PHP test file. Now I need to do it in my CI-Bonfire application. I used find_by() and find_all() but, I didn't get any result that I want.
<?php
$result2 = mysql_query('SELECT * FROM its1_dms_users LEFT JOIN its1_dms_roles ON its1_dms_users.role_id=its1_dms_roles.role_id WHERE its1_dms_users.deleted=0 AND its1_dms_users.banned=0 AND its1_dms_users.active=1');
print "<select name='Notice' multiple size='10'>";
while ($roles = mysql_fetch_assoc($result2)){
print "<optgroup label='" . $roles['role_name'] . "'>";
print "<option value='" . $roles['id'] . "'>" . $roles['display_name'] . "</option>";
print "</optgroup>";
}
print "</select>";
?>
I just need to know. How I run that Query in CI-Bonfire in My Modules Controller.
As the Ci Bonfire works on the codeignitor frame work so you can run the mysql Query using the codeignitor class.
Please follow the below URL.
http://ellislab.com/codeigniter/user-guide/database/queries.html

Error: Column count doesn't match value count at row 1 -MySQL

I am a beginner programmer and I have just started using MySQL - I am using a program called PHP MyAdmin to Create a table in a database. I want to enter in values and then have those values appear in a table. I have made a table called table 1 in a database called sweetshop. The two rows I put in the PHP MyAdmin program are called SweetID and SweetName.
I hope someone will be able to help. Sorry if I don't come across clear - I haven't been coding long!
The code I'm using for this particular section is:
<?php
$con = mysqli_connect("localhost","root","","sweetshop");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to Connect to MySQL: ".mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM table1");
echo "<table>
<tr>
<th>Sweet ID</th>
<th>Name</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo " <td>" . $row['SweetID'] . "</td>";
echo " <td>" . $row['SweetName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I assume you mean that the columns you have are named SweetID and SweetName
If so, change your select statement from
$result = mysqli_query($con,"SELECT * FROM table1");
to
$result = mysqli_query($con,"SELECT SweetID, SweetName FROM sweetshop.table1");
and you should be good (db name in your select isn't required but is a great habit to develop now).
Try running that exact select through phpMyadmin to see what your result set looks like as well, if you get results there then you should also get them from php
It would also be helpful for you to post the full structure of the table just in case