I asked a question late last night and trying to work out a way to fix my issue
here
I included the link to the question encase it was relevant to this question
I am trying to display data from a file named example.php
in example.php i have the following code
<?php
//include database connection
include 'db_connect.php';
$query = "select id, name, surname
from contacts
where id='".$mysqli->real_escape_string($_REQUEST['id'])."'
limit 0,1";
$result = $mysqli->query( $query );
$row = $result->fetch_assoc();
$id = $row['id'];
$name = $row['name'];
$surname = $row['surname'];
?>
<?php echo$name; ?><br>
<?php echo$surname; ?>
so if i visited my page by going to example.php?id=100 it would display the client with the id off 100
but what i am trying to do is display this file depending on another table.
i have a table named repairs and in there is a column named client_id with is the same as the id of the contacts table.
I have tried to display the file by doing this.
<?php include'example.php?id=echo$client_id;'?>
But this did not work please advise me what the right code is and why this is wrong.
try:
<?php
$_REQUEST['id'] = $client_id;
include'example.php';
?>
Related
so I have a list of people with their city name and contact numbers in mysql database which is displayed on my website. I want to know which person was contacted by a visitor.
Here is a snippet of my code:
<?php
$city = $_POST['city'];
$sql = "SELECT * FROM users WHERE city = '$city'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
?>
<tr>
<td class="pl-4"><?php echo "<h3>" .$row['fname']. " " .$row['lname']. "</h3>"; ?>
<BUTTON onClick="contactclick();" class="track btn btn-outline-info p-2"><span class="icon-phone"></span> Contact</BUTTON>
</td>
<script>
function contactclick() {
<?php
$sql7 = "SELECT * FROM users WHERE id = '$id' LIMIT 1";
$result7 = mysqli_query($conn, $sql7);
$row7 = mysqli_fetch_assoc($result7);
$firstname = $row7['fname'];
$lastname = $row7['lname'];
$city7 = $row7['city'];
$txt = 'Call received by:'.$firstname.' '.$lastname.' of '.$city7.'';
$file = fopen('drivers-contacted.txt','a');
fwrite($file,$txt);
?>
}
</script>
</tr>
As you can see from the code I have tried making a text file and adding info into that file everytime 'contact' button is clicked. But it adds the name of all people in the list instead of 1 who was actually contacted. How can I solve this? Also, is there a better way to get the information I want, like which user was contacted from my database list?
PS : I'm new to coding
In general: you are trying to communicate in the direction of client-side to server-side when you want to do a server-side action on a button click. Embedding PHP code inside the client script is not the correct way to do this. Please look up AJAX. A library called jQuery is fairly easy to use and has a simple interface for AJAX.
In addition: unless there is some code missing from this snippet, what this appears to do is create n contactclick() functions, each with its own ID. You need to take the function out of the loop, and have it accept id as a parameter. Then you can do an AJAX call from this function and your back-end (PHP) code will write to a file.
New to php and decided to build a little template to display some fake content from a database. The goal is:
loop through the DB, print the items,
wrap them each in a link to the same page (in this case, details.php)
use a GET (details.php?id) to put the id column of the DB.
I've got the printing/looping happening, and the linking but I'm running into a strange problem. When I try to echo out the ID, I get the following error: "Notice Undefined index: id in /opt/lampp/htdocs/arrayTest/index.php on line...". When I use another any other column from the DB, it echos properly. What am I missing?
Query:
$sql = 'SELECT firstName FROM userNames';
$query = $dbh->query($sql);
PHP Page:
<h1>Names of People</h1>
<?php
if($query->rowCount() > 0) {
echo 'Found some shit';
foreach($query as $row) { ?>
<p><?php echo $row['firstName']; ?></p>
<?php } } ?>
DB Screen shot attached
Use this:
$sql = 'SELECT * FROM userNames';
You don't have the id because you are only selecting the firstName
You need to use id in your query modify your query as
$sql = 'SELECT id,firstName FROM userNames';
I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)
MYSQL table structure:
id , name , status , color
how can i use PHP to get some data from my database(mysql) ,the php script will look for table rows that have "good" in the "status" column. Then echo those rows which have good in status(P/S out into a JS file. It also get the color from each row (with status=good) and ptu it into javascript(jQUery)
I did a little research on how to use php to generate javascript.
Here's a php script, it's not working btw:
<?php
header("content-type: application/x-javascript");
include 'connect.php';
$sql = "SELECT * FROM users WHERE status ='good'";
$query = mysql_query($sql)or die(mysql_error());
$rows = array();
while($row = mysql_fetch_array( $query )){
$rows[] = $row;
echo "$('a[href*=\"row[username]\"]').css('color', 'row[color]');\n";
}
?>
The out put is (which wont work):
$('a[href*="row[username]"]').css('color', 'row[color]');
The connect.php is working(connects to database) , just the script is not working.
(BTW: The file name of this is usercolor.js.php , is it correct?)
I hope someone could guide me.
Thanks and have a wonderful day.
echo "$('a[href*=\"row[username]\"]').css('color', 'row[color]');\n";
won't work. Try
$username = $row['username'];
$color = $row['color'];
echo "$('a[href*=\"$username\"]').css('color', '$color');\n";
or
echo "$('a[href*=\"{$row['username']}\"]').css('color', '{$row['color']}');\n";
see the manual for an explanation. Also, according to your mysql table structure, the column is called name and not username.
I've scoured the web for a tutorial about this simple task, but to no avail. And so I turn to you helpful comrades. Here's what I need to do:
I have a MySQL database with an Events table. I need to create a PHP web page with a list of the Event titles, and each title must be a link to the full details of the Event. But I want to avoid having to create a static page for each event, primarily because I don't want the data entry volunteer to have to create these new pages. (Yes, I realize that static pages are more SEO friendly, but I need to forego that in this case for the sake of efficiency.)
I've seen PHP url syntax with something like this:
pagename.php?id=20
but I don't know how to make it work.
Any and all help greatly appreciated.
Thanks!
Kip
This is basic php. You would simply query the DB for the event details before the page headers are written and write the html accordingly.
The first thing I would ask you is if you know how to connect to your database. From there, you query based on the $_GET['id'] value and use the results to populate your html.
Not to be rude, but the question itself suggests you're new to PHP, right? So in order to provide a solution that works we might want to know just how far you got.
Also, you can rewrite your dynamic urls to appear like static ones using apache's mod_rewrite. It's probably a novice level thing if you're interested in "pretty" url's.
MODIFIED ANSWER:
In your loop you would use the id from the query result (assuming your primary key is id)...
while($field = mysql_fetch_array($result)) {
echo "<p class='date'>";
echo $field['month']." ".$field['day'].", ".$field['year'];
echo "</p>";
echo "<h3>";
echo ''.$field['event_name'].'';
echo "</h3>";
}
Then on somepage.php you would use the get var id to pull the relevant info...
$result = mysql_query("SELECT * FROM `calendar` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."');
don't forget to look into mysql_real_escape_string() for cleaning entries.
It's wise to take extra care when you are using $_GETvariables, because them can be easily altered by a malicious user.
Following with the example, you could do:
$foo = (int)$_GET['id'];
So we are forcing here the cast of the variable to a integer so we are sure about the nature of the data, this is commonly used to avoid SQL injections.
lets say you have the php file test.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("db", $conn);
$id = $_GET['id'];
$sql = "select * from table where id = $id";
$result = mysql_query($sql, $conn);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[0];
$content = $row[1];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
a dynamic page would be something like that..
Here is the pertinent code for extracting a list of events in November from a table named calendar, with each event having a link to a page called event.php and with the event's id field appended to the end of the url:
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"<a href='event.php?id=".$row['id']."'>".$row['event_name']."</a>"
;}
And here is the pertinent code on the event.php page. Note the row numbers in brackets depends on the placement of such in your table, remembering that the first row (field) would have the number 0 inside the brackets:
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
This works for me, thanks to the help from those above.
$foo=$_GET['id'];
in your example $foo would = 20