Pasing data/values - php

I have this search php code in which i made the formnumber into a hyperlink. All I wanna do is once i click a formnumber it will retrieve all ta data of tha click formnumber into the database and echos it.
$dbname = "vianney300";
$SRCHDATA = $_POST["srch"];
if($connection===FALSE)
echo "<p> Connection Failed. ". mysql_error()."</p>";
else{
if(mysql_select_db( $dbname)===FALSE)
echo "<p>Could not select database.</p>";
}
$query = "SELECT * FROM profile WHERE LastName='$SRCHDATA'";
$result = #mysql_query($query);
if ($result===FALSE){
echo "<p>Unable to execute query.</p>";
}
else {
while (($row = mysql_fetch_assoc($result))!==FALSE){
echo "Form no: ";
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
echo "Last name: ";
echo "{$row['LastName']}</br>";
echo "First name: ";
echo "{$row['FirstName']}</br></br>";

If you want to send a form number to individual page then you have to do like this inside your while function :
echo "<a href='individual.php?form_no={$row['FormNo']}'>{$row['FormNo']}</a></br>";
And on individual.php you will retrieve form number using $_GET['form_no']
individual.php
<?php
echo $_GET['form_no'];
// Using this form number you can fetch the data from database
?>

You have made a small mistake on this line
echo "<a href='individual.php'>{$row['FormNo']}</a></br>";
it should be
echo "<a href='individual.php?formnumber={$row['FormNo']}'>{$row['FormNo']}</a></br>";
ok.then what should i put to echo in the individual.php
Thats another question really, but
code individual.php
<?php
if ( isset($_GET['formnumber']) ) {
echo 'Recieved parameter "formnumber" in the GET array = ' . $_GET['formnumber'];
} else {
echo 'No parameter passed';
}
?>

Related

How to combine to php's (sql)

I need to combine these two codes,more likely the sql. I'm sitting on it about hours and can't get it.
<?php
$mysqli2 = new mysqli("Edudb", "mct", "QwSkepticx97!");
$mysqli2->select_db("maturita");
$ask2 = $mysqli2->query("SELECT * FROM gallery");
if(mysqli_num_rows($ask2)>0)
{
while ($row2=mysqli_fetch_object($ask2))
{
echo "<a href='show-gall.php?gallery=$row2->gall_id'>$row2->name<br></a</span>";
}
}
?>
and this one
<?php
$conn = mysqli_connect("Edudb", "mct", "QwSkepticx97!", "maturita");
$sql="SELECT g.name, i.img_thumb FROM gallery g JOIN images i ON g.gall_id = i.gall_id GROUP BY g.gall_id ";
$query=mysqli_query($conn, $sql);
if(mysqli_num_rows($query)>0)
{
while ($row=mysqli_fetch_object($query))
{
echo "<div class='view'>";
echo "<a href='show-gall.php?gallery=$row->gall_id'><img src='$row->img_thumb' alt=''></a>";
echo "<br/>";
echo "<a href='show-gall.php?gallery=$row->gall_id'>$row->name</a>";
echo "<br/>";
echo "</div>";
}
}
?>
I'll be thankful for any suggestions
Try this one
include('here put the name of one file of two ');
Then put this statement in the other one.
Now the other file that has include() statement shows everything in the second one.

Displaying multiple MySQL tables in one HTML table with PHP, experiencing loop trouble

This is the code I am trying to use to display information from multiple MySQL tables in one table. I'm also trying to create an else clause if the table information doesn't exist. This is my failed attempt at it.
I keep editing this as I peck away at it but at this point I still can't get it to display the final else when the row doesn't exist.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info WHERE Item_ItemID = $row[ItemID] ";
$info_results = mysql_query($info);
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
}
?>
you could re-write the SQL statement in your $info "SELECT * FROM Info" to "SELECT * FROM Info wHERE Item_ItemID=$row['ItemID'] ... and then delete the
if ($info_row['Item_ItemID'] = $row['ItemID'])
Plus you new a while ind your code after $info_results = mysql_query($info);
* EDITED *
then you do like this, instead of
while ($info_row = mysql_fetch_array($info_results))
{
if ($info_row['Item_ItemID'] = $row['ItemID']) // Display Information from Info table If column exists with same ItemID.
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
} else //else dispaly Add information to info link.
{
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
try this
if(mysql_num_rows($info)>0){
while($info_row = mysql_fetch_array($info){
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
}else {
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
i try to edit your code but im not able to try it.
<?
include"db.inc.php";//database connection
$item = "SELECT * FROM Item";
$result = mysql_query($item);
while ($row=mysql_fetch_array($result)) // Display information from Item Table.
{
echo ("<tr><td>Edit</td>");
echo ("<td>$row[ItemID]</td>");
echo ("<td>$row[Category]</td>");
echo ("<td>$row[Cost]</td>");
echo ("<td>$row[Condition]</td>");
echo ("<td>$row[PurchaseLot_PurchaseLotID]</td>");
echo ("<td>$row[Location]</td>");
echo ("<td>$row[Date]</td>");
echo ("<td>$row[Desc]</td>");
echo ("<td>$row[Notes]</td>");
echo ("<td>Pics</td>");
echo ("<td>Info</td></tr>");
$info = "SELECT * FROM Info where Item_ItemID = ".$row['ItemID'];
$info_results = mysql_query($info);
if ($info_row = mysql_fetch_array($info_results))
{
echo ("<tr><td colspan=\"12\">");
echo ("Manufacturer:$info_row[Manufacturer]<br>");
echo ("Model:$info_row[Model]<br>");
echo ("Model Number:$info_row[ModelNumber]<br>");
echo ("Serial Number:$info_row[SerialNumber]<br>");
echo ("Service Number:$info_row[ServiceNumber]<br>");
echo ("</td></tr>");
}
else { //else dispaly Add information to info link.
echo ("<tr><td colspan=\"12\">Add Extra Information</td></tr>");
}
}
?>
i hope this is the answer.

Added if else argument and lost variable from database

<?php
$con=mysqli_connect("localhost","root","","clarks");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$place = $_GET['place'];
$place2 = $_GET['place2'];
$return = $_GET['return'];
$people = $_GET['people'];
$pickup = $_GET['pickup'];
$dropoff = $_GET['dropoff'];
$result = mysqli_query($con,"SELECT * FROM pricelist WHERE place1='$place' AND place2='$place2' AND people='$people'");
while($row = mysqli_fetch_array($result))
{
if (!empty($row['Price']))
{
echo "Not Applicable";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>£";
echo $row['Price'] * $return + $pickup + $dropoff;
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
}
mysqli_close($con);
?>
This is returning the "Not Applicable" from the if argument every time.
I've tried placing the lines in different order and using both the price and result variable, it still only returns not applicable but without the if else argument, the rest of it works as intended and brings up a price so I know the price variable shouldn't be empty and neither should the result variable.
With it returning not applicable all the time, I assume it's not retrieving the information from the database properly any more but I can't figure out why when it works perfectly fine without the if else.
Any help would be great, Thanks.
Your logic is wrong:
if (!empty($row['Price']))
^ here
{
echo "Not Applicable";
}
should be:
if (empty($row['Price']))
{
echo "Not Applicable";
}
You also have a serious sql injection problem, you should use prepared statements or at the very least use mysqli's escaping function.
if (empty($row['Price']))
{
echo "Not Applicable";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet'
.....
}
}
I think the in the if condition the negation causes the problem
Remove the exclamation mark!
It might be because you are using mysqli_fetch_array instead of mysqli_fetch_assoc. http://www.php.net/manual/en/mysqli-result.fetch-assoc.php
You should also start using real_ escape_ string on your parameters, or do it in the right way with parameterized prepared statement. http://www.php.net/manual/en/mysqli.prepare.php
<?php
$con=mysqli_connect("localhost","root","","clarks");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$place = $_GET['place'];
$place2 = $_GET['place2'];
$return = $_GET['return'];
$people = $_GET['people'];
$pickup = $_GET['pickup'];
$dropoff = $_GET['dropoff'];
$result = mysqli_query($con,"SELECT * FROM pricelist WHERE place1='$place' AND place2='$place2' AND people='$people'");
$row = (mysqli_fetch_array($result));
$a = $row[3];
if (empty($a))
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>";
echo "N/A";
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
else
{
echo "<html><body style='background-color: #31ff01;'><link rel='stylesheet' href='css/bootstrap.min.css'>";
echo "<div id='prices'>£";
echo $a * $return + $pickup + $dropoff;
echo "</div>";
echo "<div id='back'>";
echo "<a href='index.html'>Go Back</a>";
echo "</div>";
echo "<br>";
}
mysqli_close($con);
?>
Well I found this workaround, instead of using while I just got rid of it and assigned the price from the database into its own variable meaning there's no conflict with the if else statement. Works like a charm... now to look at prepared statements :S

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 selecting from database

I'm having trouble selecting data from inside one of the tables in the database.
I'm trying to display a user's profile when their name is clicked on.
<?php
$view = mysql_fetch_array(mysql_query("select * from users where id=$view"));
if (empty ($view[id])) {
print "No such user.";
exit;
}
print "<center><b><u>$view[user]</b></u> ($view[id])</center><br>";
print "Rank: $view[rank]<br>";
?>
The link they will click is:
viewpage.php?page_id=4&view=....
I can't get this to work.
<?php
$id=$_GET["view"];
$query = mysql_query("SELECT * FROM users WHERE id=$id");
if($query)
{
echo '<table>';
echo '<tr><th>Name</th><th>ID</th><th>Something Else</th></tr>';
while($view = mysql_fetch_assoc($query))
{
echo '<tr>';
echo '<td>'.$view['Name'].'</td>';
echo '<td>'.$view['id'].'</td>';
echo '<td>'.$view['some other variable in your data'].'</td>';
echo '</tr>'; //This one goes after you've echoed all the data for one user.
}
echo '</table>';
} else {
echo 'no user found';
}
?>

Categories