Make Query Data Become Link [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
As mentioned in the title, I would like to know how can I make the queried data become a link to another php file. For example, referring to the pic attached below, I would like to show all the cities of United States by just simply click on it and same goes with United Kingdom. I want to make like when I click on any of the state, the php file link to it will automatically know which state I've selected and generate its corresponding cities.
The following is my code:
<html>
<head><title>State</title></head>
<body>
<?php
$dbh = pg_connect("host=localhost dbname=state user=postgres");
if (!$dbh) {
die("Error in connection: " . pg_last_error());
}
$sql = "SELECT * FROM state ";
echo "<table>";
echo "<table border=\"1\" align=\"center\">";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>State</th>";
echo "</tr>";
$result = pg_query($dbh, $sql);
if (!$result) {
die("Error in SQL query: " . pg_last_error());
}
while ($column = pg_fetch_array($result)) {
echo "<tr>";
echo "<td>".$column[0]."</td>";
echo "<td>".$column[1]."</td>";
echo "</tr>";
}
echo "</table>";
pg_free_result($result);
pg_close($dbh);
?>
</body>
</html>

in your current php file:
// Replace your while statement in your code above with this...
$row_counter = 0;
while ($row = pg_fetch_array($result,$row_counter,PGSQL_ASSOC)) {
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td><a href='state.php?id=".$row['id']."'>".htmlentities($row['state'])."</a></td>";
echo "</tr>";
$row_counter++;
}
And in another php file, let's say state.php, you could run something like this:
$state_id = $_GET['state_id'];
// connect to database...
$sql = "SELECT * FROM some_table WHERE state_id = '".pg_escape_string($state_id)."'";
// run your query...
Note: htmlentities will prevent XSS problems and pg_escape_string will help prevent SQL injection (but research prepared statements for a better approach).

Related

php page not showing anything [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
im trying to create a drop down many and populate the options from fields in a database that i have.
So far i have got the following code:
<?php
session_start();
include_once("config.php");
$query = "SELECT Category FROM books";
$result = mysqli_query ($mysqli, $query);
echo "<select name=dropdown value=''>Dropdown</option>";
while($row = mysqli_fetch_array($result))
{
echo "<option value=$row[Category]>$row[Category]</option>";
}
echo "</select>";
?>
</div>
</body>
</html>
However when i try to view the page nothing seems to happen.
My page remains blank and all i see is the color that i gave to the body in the css file.
Was wondering if anyone knew wh this was happening and if they can sort it out?
Thanks!
Assuming you are getting results from your query. Can find out by print_r($result). Think you are getting death screen because of quotes and missing opening for first option in dropdown.
echo '<select name="dropdown" value=""><option value="">Dropdown</option>';
while($row = mysqli_fetch_array($result))
{
echo '<option value="' . $row['Category'] . '">' . $row['Category'] . '"</option>';
}
echo "</select>";

Echo a table of people - PHP, MySQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a database table called 'partners'. I want to echo this table into a html/php table which has 5 columns id, channel, email, paypal, and network. I also wan't a form that adds new people to the table through the website.
<?php
mysql_connect("localhost", "mysql_user", "mysql_password"); //your parameters here
mysql_select_db("mydb"); //you DB name here
$q = "SELECT id, channel, email, paypal, network FROM partners;";
$result = mysql_query($q);
echo "<table>";
echo "<tr><td>id</td><td>channel</td><td>email</td><td>paypal</td><td>network </td></tr>";
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr>";
echo "<td>".$row[0]."</td>";
echo "<td>".$row[1]."</td>";
echo "<td>".$row[2]."</td>";
echo "<td>".$row[3]."</td>";
echo "<td>".$row[4]."</td>";
echo "</tr>";
}
echo "</table>";
mysql_free_result($result);

Entries from database to dropdown list simple php code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a material record database.
material_records
poly canvass
metal
washer
knot
how to call this entries into a dropdown list.. please help.. i don't know how to call the entries into a dropdown. Happy new year.. :)
Create a query
$handle = mysql_connect("127.0.0.1", "username", "password");
if($handle) {
#mysql_select_db("database_name", $handle);
$query = "SELECT * FROM material_records";
$result = mysql_query($query, $handle);
}
Fetch and loop the results and print a select
if(isset($result)) {
if(mysql_num_rows($result) > 0) {
echo '<select name="mydropdrown">';
while($row = mysql_fetch_object($handle)) {
echo '<option value="blub">'.htmlspecialchars($row["poly_canvass"]).'</option>';
echo '<option value="blub">'.htmlspecialchars($row["metal"]).'</option>';
echo '<option value="blub">'.htmlspecialchars($row["washer"]).'</option>';
echo '<option value="blub">'.htmlspecialchars($row["knot"]).'</option>';
}
echo '</select'>;
}
}
$query = "SELECT description FROM material_records";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
echo "<option>" . $row['description'] . "</option>";
}
This is one of the simplest ways to get the values for your dropdown.

Function for dumping entire mysql table in php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am trying to make a simple function that creates an HTML table that first gets the headers in th-tags and then dumps all the rows into html table rows.
Here's what I got so far.
function dumpMysqlTable($tblName) {
$result=mysql_query("SELECT * FROM `$tblName`");
echo "<table>";
echo "<tr>";
$i=0;
while($i<mysql_num_fields($result)) {
$fields=mysql_fetch_field($result, $i);
echo "<th>".$fields->name."</th>";
$i++;
}
echo "</tr>";
echo "<tr>";
$j=0;
while($j<mysql_num_rows($result)) {
$k=0;
while($row=mysql_fetch_array($result)) {
echo "<td>".$row[$k]."</td>";
$k++;
}
$j++;
}
echo "</tr>";
echo "</table>";
}
However, it messes up badly, and picks rows from the table its not supposed to and I just can't get it to work. Please help :)
You are making several mistakes.
while($row=mysql_fetch_array($result)) {
will return a whole row (or line) of the table. So $row[$k] references a row (one line in the table), not a cell value.
Then you should know that mysql_fetch_array by default will return an array with both numerical keys and associative indices. So you should either use mysql_fetch_array( $result, MYSQL_NUM ) or simply mysql_fetch_row(). Then you can loop through the row:
while( $row = mysql_fetch_row( $result ) ) {
echo "<tr>";
foreach( $row as $cell )
{
echo "<td>".$cell."</td>";
}
echo "</tr>";
}
Notice you also forgot to open a new tr for each line.

Coloring row when ip match in php output with mysql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a form and when a user submits the form his ip is logged. I need a way when 2 or more ips match using tables at output to color only the rows with the same ip
<?php
// Connects to your Database
mysql_connect("localhost", "pp", "mygas13") or die(mysql_error());
mysql_select_db("pp") or die(mysql_error());
$data = mysql_query("SELECT * FROM userTable")
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['nick'] . "</td> ";
Print "<th>Ip:</th> <td>".$info['ip'] . " </td></tr>";
}
Print "</table>";
?>
Like this?
if($ip = $sameip) {
echo "<td style='background: #fffff;'>$ip</td>";
} else {
echo "<td>$ip</td>";
}
This is a bit of a guess due to no code being shown but I think that's what your after?

Categories