Printing MySQL data to textbox - php

I apologize before I speak English very little I want to write all of the data in textboxes, but I could not do it
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "vipser";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("SELECT * FROM `a_info` WHERE `pax`='" . $_GET['pax'] . "' and `nereden`='" . $_GET['durum'] . "' and `nereye`='" . $_GET['gdurum'] . "'");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
while ($row = $result->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['pax'] . "</td>";
echo "<td>" . $row['a_cinsi'] . "</td>";
echo "<td>" . $row['nereden'] . "</td>";
echo "<td>" . $row['nereye'] . "</td>";
echo "<td>" . $row['saat'] . "</td>";
echo "<td>" . $row['km'] . "</td>";
echo "<td>" . $row['fiyat'] . "</td>";
echo '<td><img src="' . $row["a_resmi"] . '" width="75" height="75"/></td>';
echo "</tr>";
}
echo "</table>";
}
else {
echo "0 results";
}
$conn->close(); ?>

Store the data of the loop in a variable instead of echoing and then assign the variable into textbox's value. Done this here:
<?php
$str = "";
if ($result->num_rows > 0) {
while($row = $result->fetch_array()) {
$str .= . $row['pax'] . " " . $row['a_cinsi'] . " " . $row['nereden'] . " " . $row['nereye'] . " " . $row['saat'] . " " . $row['km'] . " " . $row['fiyat'] . " ";
}
}
?>
<input type="text" value="<?= $str ?>" />
You can put this input inside while if you want different textboxes for each row.

Related

return does not retrieve all data from while loop

So I am having some difficulty understanding why, If i use return on the end of my function it does not return all the values but only one.
But if I use echo, all values get displayed.
New to functions and trying to understand and make use of them more. Any explanation is much appreciated.
function find_all_users($Teacher_role) {
global $mysqli;
if ($Teacher_role == "teacher") {
$set_update_role = "teacher";
}
$set_visable = 1;
if (!($stmt = $mysqli->prepare("SELECT user_id, user_email, first_name, last_name, role FROM users WHERE role = ? AND visible = ? ORDER BY user_id ASC"))) {
echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
/* 2 Bind params */
if (!$stmt->bind_param("si", $Teacher_role, $set_visable)) {
echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
}
/* 1 Execute statements */
if (!$stmt->execute()) {
echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
}
$result = $stmt->get_result();
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
$user_id = $row["user_id"];
$user_email = $row["user_email"];
$user_Fname = $row["first_name"];
$user_Lname = $row["last_name"];
$user_role = $row["role"];
$output = "<tr>";
$output .= "<td>" . $user_id . "</td>";
$output .= "<td>" . $user_email . "</td>";
$output .= "<td>" . $user_Fname . "</td>";
$output .= "<td>" . $user_Lname . "</td>";
$output .= "<td>" . $user_role . "</td>";
$output .= "<td> <a href='update_" . $set_update_role . ".php?id=" . $user_id . "'>Edit</a>" . "</td>";
$output .= "</tr>";
return $output;
}
mysqli_close($mysqli);}
Do not return $output in the while loop. Your $output value is returned immediately upon first run of the loop. Let the while loop build $output for all values and only after everything is completed return $output.
$output = '';
while ($row = mysqli_fetch_assoc($result)) {
$user_id = $row["user_id"];
$user_email = $row["user_email"];
$user_Fname = $row["first_name"];
$user_Lname = $row["last_name"];
$user_role = $row["role"];
$output .= "<tr>";
$output .= "<td>" . $user_id . "</td>";
$output .= "<td>" . $user_email . "</td>";
$output .= "<td>" . $user_Fname . "</td>";
$output .= "<td>" . $user_Lname . "</td>";
$output .= "<td>" . $user_role . "</td>";
$output .= "<td> <a href='update_" . $set_update_role . ".php?id=" . $user_id . "'>Edit</a>" . "</td>";
$output .= "</tr>";
}
mysqli_close($mysqli);
return $output;

PHP mysql query returns a blank screen

I've commented out the bottom part, and the SQL query works fine. Its the displaying of the query where the error is coming from i believe.
$host = "127.0.0.1";
$user = "root";
$pass = "Toom13371!";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
// 2. Selecting DB.
$dbname = "filters";
mysql_select_db($dbname);
// 3. Build/Test SQL Query
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
//echo $sql; //Comment/Uncomment to test sql query.
// 4. Retrieve info from MySQL.
$query = mysql_query($sql);
// 5. Display Query.
echo "<table border='1'>
<tr>
<th>Low Frequency</th>
<th>High Frequency</th>
</tr>";
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Any help would be appreciated, I'm sure it's going to be some small stupid error i've over looked.
Thanks in advance :)
I'm guessing, based on your query, that you need to change this
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
and
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
to
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['start_passband'] . "</td>";
echo "<td>" . $row['stop_passband'] . "</td>";
echo "</tr>";
}
Change line
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
Also before query check
$_POST['Lowfreq'] and $_POST['Highfreq']
If there is no value in these variables query will must return empty.
In your query there should not brackets for string.
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
should be:
$sql = "select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'";

Searching from MySql database and display the results as a hyperlink

Here is my code, what I want to do is display the first names of my friends in another page as Hyperlinks and when a link is clicked I want to display the data.
<?php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>
<?php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result))
{?>php
<a href=<?php "echo "<p>" . $row['FirstName'] . "</p>";"?>> `your page name`</a><?php
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>
Your file from questions change:
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
to
while($row = mysqli_fetch_array($result))
{
echo ' . $row['FirstName'] . "";
}
show.php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['UserId'])){
if(is_numeric($_GET['UserId'])){
$UserId = (int)$_GET['UserId'];
$result = mysqli_query($con,"SELECT * FROM friends WHERE UserId= $UserId");
if(count($result)){
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}}}}
Important: I'm not sure what is UserId column name, so change it.
Also, please check for syntax errors.
Try connection it with an incremental ID out of the database. So every row in your friends table must have a unique ID. In that way you can connect it and get the row data from your friends table.
So your friends table must look like this:
friend_id (unique id)
firstname
lastname
email
address
And your code can be something like (this is not save from any attacks):
<?php
$con = mysqli_connect("localhost","root","abc123","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['friendid'])) {
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result)) {
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
echo "<p>Details</p>";
}
} else {
$result = mysqli_query($con,"SELECT * FROM friends WHERE friend_id = '".$_GET['friendid']."'");
$row = mysqli_fetch_assoc($result);
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>

insert hyperlink in php mysql query

I am trying to display a link in a php document. the data is stored in mysql. I have stored the url in the field course_url.
i can get the page to show the hyperlink asd a plain text but want it to show ashyperlink with "Click Here" anchor text. The coding i got so far is:
<?php
$con=mysqli_connect("localhost","root","","mentertraining");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT `coursedates`.`coursedate_id`,`coursedates`.`course_id`,`coursedates`.`date1`,`courses`.`course_title`,`courses`.`course_url`,`courses`.`no_of_days` FROM coursedates\n"
. "LEFT JOIN `mentertraining`.`courses` ON `coursedates`.`course_id` = `courses`.`course_id` LIMIT 0, 30 ";
$result = mysqli_query($con,$query);
echo "<table border='1'><tr><th>Course Title</th><th>Course Date</th><th>No of Days</th><th>Course URL</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
$date = new DateTime($row['date1']);
$row['date1'] = $date->format('d/m/Y');
echo "<tr>";
echo "<td>" . $row['course_title'] . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . $row['no_of_days'] . "</td>";
echo "<td>""<a href=" . $row['course_url'] . >"'Click Her'"</a>""</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Your echo is malformed on this line:
echo "<td>""<a href=" . $row['course_url'] . >"'Click Her'"</a>""</td>";
It should be:
echo "<td><a href='" . $row['course_url'] . "'>Click Here</a></td>";
You have used the wrong "' quotations
while($row = mysqli_fetch_assoc($result))
{
$date = new DateTime($row['date1']);
$row['date1'] = $date->format('d/m/Y');
echo "<tr>";
echo "<td>" . $row['course_title'] . "</td>";
echo "<td>" . $row['date1'] . "</td>";
echo "<td>" . $row['no_of_days'] . "</td>";
echo "<td><a href='" . $row['course_url'] ."'>Click Her</a></td>";
echo "</tr>";
}

printing [mysql_query] result in a table

I want to print mysql_query result in a table. I know how to do it but I am just confused. I tried this.
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$i = 1;
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($i<=2 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=4 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=6 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=8 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
echo "</tr></table>";
}
?>
As you can see it is written again and again with a slight change of 2,4,6,8 in the while loop. It works but the problem is I cant rewrite it again and again as when the website will go live it will have more than 1000 entries. Could You guys help me out by suggesting another way to do this?
""** I need it to be like these dots (dots represent records in the database) **"""
. . . .
. . . .
. . . .
THANKS in Advance.
Ramzy
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
echo "</tr></table>";
}
?>
while($row = mysql_fetch_array($sql)) {
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
I don't really see what's the problem here.
By the way you should never call you're array like this $row[id] but you should quote the key instead $row['id']; Because if a constant id exists it will screw up your code and also for performance reason.
Just use
$limit = 1000;//place any value you need here to limit the number of rows displayed
while ($i<=$limit && $row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
++$i;
}
Also, that limit is unnecessary if all you want is to flush every record to the output. You could just do
while ($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
And it will stop as soon as there are no more records.
To print all database rows into an HTML-table, use:
echo '<table>';
$i = 0; // $i is just for numbering the output, not really useful
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>' . $i . ' - ' . $row['id'] . ' : ' . $row['name'] . '</td></tr>';
$i++;
}
echo '</tr></table>';
here is a general function I use:
function query_result_to_html_table($res, $table_id = NULL, $table_class = NULL, $display_null = true)
{
$table = array();
while ($tmp = mysql_fetch_assoc($res))
array_push($table, $tmp);
if (!count($table))
return false;
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" "
. ($table_id ? "id=\"$table_id\" " : "")
. ($table_class ? "class=\"$table_class\" " : "") . ">";
echo "<tr>";
foreach (array_keys($table[0]) as $field_name) {
echo "<th>$field_name";
}
foreach ($table as $row) {
echo "<tr>";
foreach ($row as $col => $value) {
echo "<td>";
if ($value === NULL)
echo "NULL";
else
echo $value;
}
echo "\n";
}
echo "</table>\n";
return true;
}
I Got The Answer.. I wanted it to be like this. I made this and It Actually Works.
<?php
$i = 1;
mysql_connect("localhost" , "root" , "") or die('Could not Connect.');
mysql_select_db("db") or die('Could not select DB.');
$query = "select * from `check`";
$sql = mysql_query($query) or die(mysql_error());
echo "<table border='5' width='50%'><tr><th>Name</th><th>Gender</th></tr></table><table border='5' width='50%'><tr>";
if($i<3){
echo "<td align='center'>".$row['name']."</td>";
echo "<td align='center'>".$row['gender']."</td>";
++$i;
} else {
echo "<td align='center'>".$row['name']."</td><td align='center'>".$row['gender']."</td>";
echo "</tr>";
$i = 1;
echo "<tr>";
}
}
echo "</table>";
?>
</div>
Thank You Guys For Your Support

Categories