PHP MYSQL - $_GET and Query Issue - php

I have a page which contains a table. The 'change_id' field contains hyperlinks which direct the user to a different page with an overview of that change_id's details.
'<td>' . $row['change_id'] . '</td>';
Now, just to test that the change_id is being received on the home2.php page, I used the following code:
<?php
include 'config.php';
$change_id=$_GET['change_id'];
print_r($_GET);
?>
This test successfully displayed the correct change id's:
Array ( [changeid] => 1006 )
Now, when I go to query the SQL Database using the change_id it doesn't work as desired.
<?php
include 'config.php';
$change_id=$_GET['change_id'];
$query1 = "SELECT * FROM `change_request_tbl` WHERE `change_id` = $change_id";
$result = mysqli_query($conn, $query1);
echo "<fieldset><legend><strong>New Requests:</legend></strong>
<table border=4 bordercolor=black class=table>
<tr>
<th>Change ID:</th>
<th>Customer Name:</th>
<th>Change Requestor:</th>
<th>Date CR raised:</th>
<th>CPM/Ticket:</th>
<th>Out of Hours:</th>
<th>Change Category:</th>
</tr>";
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['change_id'] . "</td>";
echo "<td>" . $row['customer_name'] . "</td>";
echo "<td>" . $row['change_requestor'] . "</td>";
echo "<td>" . $row['date_cr_raised'] . "</td>";
echo "<td>" . $row['cpm_ticket'] . "</td>";
echo "<td>" . $row['out_of_hours'] . "</td>";
echo "<td>" . $row['category_of_change'] . "</td>";
echo "</tr>";
}
echo "</table></fieldset><br><br><br>";
?>
The table are headers are shown without any data. Any ideas on how to fix? Thanks in advance

You are setting $change_id to $_GET['change_id']
However you are passing $_GET the parameter name of changeid
If you change
$change_id = $_GET['change_id'];
To
$change_id = $_GET['changeid'];
It should work as expected :)

Related

create google maps url in php

I Have a database with latitude and longitude values, I´m trying to write a PHP file that gets these values and then create links to google maps, so when I run the PHP file the values can be clicked pointing to google maps with the latitude and longitude values.
I never worked with PHP before and I cannot make it work, and I will be very thankful if anyone here can help me, please.
Here is the code I´ve written so far:
$result = mysqli_query($con,"SELECT * FROM user");
echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>address</th>
<th>phone</th>
<th>email</th>
<th>message</th>
<th>location</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['message'] . "</td>";
echo "<td>" . '<a href="http://maps.google.com/maps?z=12&t=m&q=loc:', urlencode $row['location'], '">' . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
the error code is telling you whats up. Look at line 35. You arent concatenating correctly. You need to change
echo "<td>" . '<a href="http://maps.google.com/maps?z=12&t=m&q=loc:', urlencode $row['location'], '">' . "</td>";
To
echo '<td><a href="http://maps.google.com/maps?z=12&t=m&q=loc:'. urlencode($row['location']) . '"></td>';
Have a read up on String Operators

For loop not working in PHP script

I have written a PHP script to pull data from a SQL database to display on a webpage. Everything seems fine but when I run the script it throws the error:
Parse error: syntax error, unexpected ')', expecting ';' in db_data.php on line 21
Line 21 is my FOR loop, I have checked the syntax of the loop and it seems to be correct so I can't understand why it is failing.
$result = mysqli_query($con,"SELECT * igi");
echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";
for($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['REF'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['LOCATION'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['ISSUE'] . "</td>";
echo "<td>" . $row['URGENCY'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
Change for to while
<?php
// Missing FROM here vv
$result = mysqli_query($con,"SELECT * FROM igi");
echo "<table border='1'>
<tr>
<th>Ref</th>
<th>Nameame</th>
<th>Location</th>
<th>Email</th>
<th>Issue</th>
<th>Urgency</th>
</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['REF'] . "</td>";
echo "<td>" . $row['NAME'] . "</td>";
echo "<td>" . $row['LOCATION'] . "</td>";
echo "<td>" . $row['EMAIL'] . "</td>";
echo "<td>" . $row['ISSUE'] . "</td>";
echo "<td>" . $row['URGENCY'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
As Anthony Thompson and RJParick says, you have to change for to while. The error you're encountering is due tu for that need 3 params like this :
for($i; $i<10; $i++) {//Note the ; used to separate params.
//do something
}
So your loop would begin like this :
while($row = mysqli_fetch_array($result)){
Another detail, your SQL isn't correct use SELECT * FROM igi instead.

How to make string/variables from query results into a link in php

best regards.
I know perhaps some's considering my question is quite stupido. But I've been trying looking for to this simple scripting. As my post title above, how to make the query results into a url. I have this scripts:
$result = mysqli_query($con,'SELECT......');
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Category</th>
<th>Link</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['author_name'] . "</td>";
echo "<td>" . $row['publisher_name'] . "</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['url_flipbook'] . "</td>";
echo "</tr>";
}
echo "</table>";
I want to make the result from $row ->'url_flipbook' into a url. The $row -> 'url_flipbook' will produce a html pages that users can click on it, it's located to a folder in my localhost.
The database field:
| url_flipbook |
-----------------
/myspace/click-it/info_1.html
I want that query results became a link for the output.
I've tried:
<a href=''echo $row['url_flipbook'];'?>''>FLIPBOOK</a>"</td>";
echo "<td>" . <a href='$row['url_flipbook']>FLIPBOOK</a>"</td>";
Nothing works...if you could help me to solve this problems. Thank you so much...
best regards,
Kris
Just concat the string to the href:
echo "<td>FLIPBOOK</td>";
or alternatively:
echo '<td>FLIPBOOK</td>';
did u try
echo "<td>" . "<a href='" . $row['url_flipbook'] . "' >FLIPBOOK</a></td>";
try this
echo "<td><a href='".$row['url_flipbook']."'>FLIPBOOK</a></td> ";

Displaying MySQL data in PHP Table Not working

Currently I'm creating just a simple website that I'm fooling around with. Users can add movies to watch, and then can view them later on. What my current problem is, is this.
Sorry for the large image. As you can see its displaying the first result correctly, but the second result gets all skrewy and displays at the top of the screen. My code for displaying the data is:
$result = mysql_query("SELECT * FROM `movies`");
echo "
<table id=\"allTable\" align=\"center\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" width=\"100%\">
<tr>
<th>ID</th>
<th>Movie</th>
<th>Genre</th></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
echo "</table>";
}
Any help would be greatly appreciated!
EDIT Fixed the problem right after I created this. Removed from while loop and put it under. Fixed.
echo "</table>"; should be moved outside of your while($row = mysql_fetch_array($result))
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
}
echo "</table>";

Only the first data display from my database but other shows but not on a table

Please kindly look into my code and help me fix the errors in my code. only my first data displays on the table on my view page but the other data displays on the view page not on the table.
my image name and the image itself successfully inserted in my database and my image directory which i name "upload" but the image wont display on my view page.
<?php
include ("config.php");
// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>address</th>
<th>nationality</th>
<th>accountnumber</th>
<th>accounttype</th>
<th>balance</th>
<th>passport</th>
<th>username</th>
<th>passport</th>
<th>update</th>
<th>delete</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['nationality'] . "</td>";
echo "<td>" . $row['account'] . "</td>";
echo "<td>" . $row['accounttype'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo "<td><h1><img src=\"upload/\" height=35 width=35 /> $row[id]</h1></td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";``
echo "<td>update</td>";
echo "<td>delete</td>";
echo "</table>";
// close while loop
}
?>
With a quick view, I see you are closing the table inside the while loop. You should change it for a
</tr>
and remember using the thead and tbody tags.
Close tbody and table after ending the loop.
This needs to be outside your while loop.
echo "</table>";
And
<img src=\"upload/\"
Is only pointing to the upload directory, you aren't specifying an actual image. Try something like:
echo "<td><h1><img src=\"upload/$row['image']\" height=35 width=35 /> $row['id']</h1></td>";
Moving the table closing tab out side the while loop will solve the first issue.
Change the code as follows to solve the image problem.
echo '<td><img src="upload/'.$row['your image name'].'" height=35 width=35 ></td>';
Your code should be as follows.
<?php
include ("config.php");
// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>firstname</th>
<th>lastname</th>
<th>address</th>
<th>nationality</th>
<th>accountnumber</th>
<th>accounttype</th>
<th>balance</th>
<th>passport</th>
<th>username</th>
<th>passport</th>
<th>update</th>
<th>delete</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['nationality'] . "</td>";
echo "<td>" . $row['account'] . "</td>";
echo "<td>" . $row['accounttype'] . "</td>";
echo "<td>" . $row['balance'] . "</td>";
echo '<td><img src="upload/'.$row['your image name'].'" height=35 width=35 ></td>';
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";``
echo "<td>update</td>";
echo "<td>delete</td>";
echo "</tr>";
// close while loop
}
echo "</table>";
?>
i am having a little difficulty in displaying my id info on my update page. i have two entries 6 and 7 if i click update on id 6 , it will show on my update page but if i click update on id 7, on the address bar, it will indicate that the id =7 but it will still show id 6 infos.
here is my display code that display data on my update page
<?ph
include ('config.php');
// Retrieve data from database
$sql="SELECT * FROM $tbl_name ";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
?>
secondly if i change data my update page, the change wont reflect on my view page.
<?php
include('config.php');
//This is the directory where images will be saved
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$address=$_POST['address'];
$nationality=$_POST['nationality'];
$accountnumber=$_POST['account'];
$accounttype=$_POST['accounttype'];
$balance=$_POST['balance'];
$username=$_POST['username'];
$password=$_POST['password'];
$id=$_POST['id'];
// update data in mysql database
$sql="UPDATE $tbl_name SET firstname='$firstname', lastname='$lastname', address='$address', nationality='$nationality',accountnumber='$account',accounttype='$accounttype',balance='$balance',username='$username',password='$password'
WHERE id='$id'";
$result=mysql_query($sql);
header ("Location: details.php");
?>

Categories