get data from sql and show on specific location - php

I m using the following query to get data from sql :
$query = mysql_query($sql);
while($row = mysql_fetch_array($q))
{
$url = ''.$row['a'].' '.$row['b'].' '.$row['c'].' '.$row['d'].'<br>';
}
now i want to do replace {URL} (that is writen in html file ) with data from sql
$mtheme = str_replace("{URL}",$url,$mtheme);
echo $mtheme;
the problem i that it shows only last row

$url .= ''.$row['a'].' '.$row['b'].' '.$row['c'].' '.$row['d'].'<br>';

Related

How to display record from one to many mysql tables in php?

I am having a problem in fetching records from database using php.But When I simply execute the following query into phpmyadmin sqls o it works but when I try to excute using php then its dipslaying error like this:
"undefined index experience.start"
$experience = "SELECT
experience.start,
experience.finish,
experience.isActive,
experience.title,
experience.nameOfOrgnization,
experienceprofessionnelle.organismeLogoURL,
description.details
FROM experience,description WHERE experience.ID_EP = description.ID_Description";
$result = $connection->query($experience);
while($row=$result->fetch_assoc()){
echo $row["experience.start"];
}
Try This
$experience = "SELECT
experience.start,
experience.finish,
experience.isActive,
experience.title,
experience.nameOfOrgnization,
experienceprofessionnelle.organismeLogoURL,
description.details
FROM experience,description WHERE experience.ID_EP = description.ID_Description";
$result = $connection->query($experience);
while($row=$result->mysqli_fetch_array()){
echo $row["experience.start"];
}
OR
$experience = "SELECT
experience.start,
experience.finish,
experience.isActive,
experience.title,
experience.nameOfOrgnization,
experienceprofessionnelle.organismeLogoURL,
description.details
FROM experience,description WHERE experience.ID_EP = description.ID_Description";
$result = $connection->query($experience);
while($row=$result->fetch_assoc()){
echo $row["start"];
}
you should use $row["start"] instant of $row["experiene.start"] as MYSQL return column name not a table.column name

How can i get paginated results for both departure and return flights from this code

I am trying to use this code to return flights from a table .. the json data i get is okay but i want to display the data using divs so that a client can proceed to book a flight but am stack
<?php
include 'connection.php';
session_start();
$sql = "
SELECT origin
, destination
, departure
, arrival
, craftoperator
, date
, returndte
, operatorlogo
, Price
FROM test
WHERE origin = '".$_REQUEST['origin']."'
AND destination = '".$_REQUEST['destination']."'
AND date = '".$_REQUEST['departure']."'
OR returndte = '".$_REQUEST['return']."' ";
$result = $conn->query($sql);
while($row = mysqli_fetch_assoc($result))
{
$output[]=$row;
}
print(json_encode($output));
?>
The image shows the desired output right now i can only see json output
Firstly, your code is vunerable to SQL injection.
Now, to make website to use <div> blocks you need to return that json_encode($data). Then to use those information in another file where to decode data json_decode($data) and to use foreach/for to parse each row.
Example:
foreach(json_decode($data, true) as $flight) {
echo '
<div>....</div>
';
}

PHP Array to string to mysql - empty record

I am on point where I have to usk on forum.
So, I have an array that is my return from join table sql query.
i am displaying it correctly without the problem.
but some of those values I want to put in different table of mysql database.
$array = joint_table();
$array_value = array['key'];
I can echo array_value and it's displaying correctly, also checked variable type and it returns STRING.
however when I am inserting it into the table, it's empty cell.
I am inserting other stuff like date() and such and that is inserted correctly.
So my sql query works fine, besides I am using same query in other places without problem.
Only values I have from that array are not inserting, but still can echo them.
<?php
$page_title = 'Complete Task';
require_once('includes/load.php');
// Checkin What level user has permission to view this page
page_require_level(2);
$task = join_task_table((int)$_GET['id']);
?>
<?php
if(isset($_POST['complete_task'])){
$area = $task['area'] ;
$jig = $task['jig'];
$desc = $task['description'];
$freq = $task['freq'];
$date = make_date();
$user = current_user();
$user_done = remove_junk(ucfirst($user['name']));
$comment = remove_junk($db->escape($_POST['comment']));
if(empty($errors)){
$sql = "INSERT INTO tpm_history (area_name,jig_name,description,frequency,date_done,done_by_user,comment)";
$sql .= " VALUES ('{$area}','{$jig}','{$desc}','{$freq}','{$date}','{$user_done}','{$comment}')";
$result = $db->query($sql);
if($result && $db->affected_rows() === 1){
$session->msg('s',"Job Completed");
redirect('home.php', false);
} else {
$session->msg('d',' Sorry failed to complete the task!');
redirect('task_complete.php?id='.$task['id'], false);
}
} else{
$session->msg("d", $errors);
redirect('task_complete.php?id='.$task['id'],false);
}
}
?>
I am lost. Help.

Creating a php search from Mysql database

I am building a bus reservation system using php & mysql.
In here I am trying to input the search field "route" which is fields of the mysql table.
It seems to have problem in searching and printing the results to the page. Please help me out.
<?php
$connect=mysqli_connect("localhost","root","","tsms");
$output ='';
if(isset($_POST['from'])){
$searchq = $_POST['from'];
$query = mysqli_query("SELECT * FROM bus WHERE route='$serchq' ");
$count = mysqli_num_rows($query);
if($count==0){
echo "<script>
alert('No bus services are found');
</script>";
} else {
while($row = mysqli_fetch_array($query)){
$imageData = $row['image'];
$arrival = $row['arrival_time'];
$departure = $row['departure_time'];
$type = $row['bus_type'];
$class = $row['class'];
$name = $row['bus_name'];
$facilities = $row['facilities'];
$reservation = $row['reservation_fee'];
$output = '<div>'.$arrival.''.$departure.''.$type.''.$class.''.$name.''.$facilities.''.$reservation.'</div>';
}
}
}
echo $output;
?>
Not sure where is the problem, but the sql comparison with "=" searches for a perfect match. Try to use the "like" as
SELECT * FROM bus WHERE route like '%$serchq%'
also, do escape the serchq, because you can get hacked this way.
In your search form, do you have a (dropdown) input or a simple text input?
In your sql query, you are searching for an exact match.
Should this be your issue, consider changing "route='$serchq'" to "route LIKE $serchq" for a more broad match. Also the quotes are not necessary around $serch so eliminating them might help.

False result SQL request in a code in PHP

The SQL's request give three results in PHPMyAdmin and in my code give only one result.
The SQL request must extract three lessons (id,title,year) of school and the PHP script must show all lessons with a link in another page.
Could you help me please ?
Thanks in advance
<?php
$requete_cours="SELECT ID_COURS, COURS.SIGLE, ANNEE FROM COURS, MODULE WHERE COURS.ID_MODULE = MODULE.ID_MODULE AND ID_PERSONNE = $userid";
//echo $requete_cours;
$res = mysqli_query($cxn, $requete_cours);
echo (mysqli_error ($cxn));
$tabLine = array();
while($ligne = mysqli_fetch_array($res)){
echo '<a href="professeur_absences.php?classe=\''.$ligne['ANNEE'].'\'cours=\''.$ligne['SIGLE'].'\'>';
echo $ligne['ANNEE'].' - '.$ligne['SIGLE'].'</a> <br/>';
//$tabLien[$ligne['ID_COURS']] = $ligne['ANNEE'];
}
//var_dump($tabLien);
$_SESSION['tabLien'] = $tabLien;
?
>

Categories