I'm doing a code where the main page has titles of some exercises: 'Exercises.php' (stored in a Mysql database) and depending on what title the user clicks (with links), I want the title and the question itself in another page: ‘question.php’. The questions will also be taken from the database. Im trying to use a GET parameter in the exercise link with the id of the exercise. Then in ‘question.php’, get the exercise with that id from the database.
This is some of the code that I've done so far but I'm stuck. Could you help me?
Thank you.
Exercises.php – In here I have all of the titles of the exercises displayed.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises";
$result = $conn->query($sql);
?>
<?php
while($row = $result->fetch_assoc())
{
?>
<tr>
<td><?php echo $row["exercise_id"]; ?></td>
<td><a name="search" href="http://localhost/PHP%20Pages/2.php" target="_blank"><?php echo $row["title"]; ?></a></td>
<td><?php echo $row["difficulty"]; ?></td>
</tr>
<?php
}
?>
question.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises"; /*Select from table name: exercises*/
$result = $conn->query($sql); /*Check connection*/
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["exercise_id"] . ". " . $row["title"] . $row["text"] . "<br>";
}
}
?>
If you want to get the excericise_id from excercise.php you can change your href like this
Add Topic</td>
And you can get that exercise id in your question.php page like this
$id=$_GET['id'];
I would like you to suggest you to keep your db connection in separate file and try to include and use. that is not the good way to write database connection in every page.
Related
I have been trying to echo out database data through a while loop now for awhile but it doesn't work, I can't find where the issue is. I have tried echoing out data manually and that works just fine.
<?php $results = mysqli_query($con,"SELECT * FROM guestbook ORDER BY id DESC"); ?>
<?php while ($row = mysqli_fetch_assoc($results)) : ?>
<li>
<?php echo $row['message']; ?>
</li>
<?php endwhile ?>
First of all make sure you are connected to the database. I don't know if you omitted that on purpose or not. Also, check if the connection is established.
<?php
//Insert your server info here
$servername = "servername";
$username = "root";
$password = "root";
$dbname = "test_database";
// Create and check your connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM guestbook ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo '<li>' . row["message"] . '</li>';
}
} else {
// Your query produced 0 results
echo "Empty result!";
}
// Remember to close the connection
mysqli_close($conn);
?>
I want to select a value from the database with variables I get from $_GET, but it doesn't show any results. Could any one help me find what is wrong with my code?
here is my first page how i get the value to GET:
<?php
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?= $row['subject'] ?></td>
<td><?= $row['location'] ?></td>
<td><?= $row['geo'] ?></td>
<td><?= $row['date'] ?></td>
<td><?= $row['piority'] ?></td>
</tr>
<?php
}
}
?>
and here is my second page that i want to get data from database with subject variable from first page:
<?php
$varPage = $_GET['subject'];
$servername = "localhost";
$username = "bayansh_user";
$password = "u)nHf,Accmo)";
$dbname = "bayansh_bmc";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"SELECT `date` FROM `editor` WHERE subject = '.$varPage.'");
while($row = mysqli_fetch_array($result))
?>
and now i want to write the date here is my code:
<p style="font-family:B Zar; direction:rtl; font-size:165%;"> <?= $row['date'] ?> </p>
but it writes nothing.
Is there anything wrong with my code?
There seems to be an oversight on your part with regards to your SQL. You have an extra-dot, which likely was a Typo. Try addressing that part and your code would most likely work as you expected. Some Guys here have suggested working with Prepared Statement (which is quite necessary) in which case this Snippet would use PDO as opposed to MySQLi... although the principles are not so extremely different.
<?php
$varPage = $_GET['subject'];
$servername = "localhost";
$username = "bayansh_user";
$password = "u)nHf,Accmo)";
$dbname = "bayansh_bmc";
$dbh = null;
// USING PDO INSTEAD::
$dsn = 'mysql:host=' . $servername . ';dbname=' . $dbname;
try {
$dbh = new PDO($dsn, $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
// USING PREPARED STATEMENT... NOTICE "=:SBJ"
$sql = "SELECT `date` FROM `editor` WHERE subject=:SBJ";
// USING PREPARED STATEMENT CONTINUED: NOTICE "$dbh->prepare()"
$stmt = $dbh->prepare($sql);
// PASSING VALUES: NOTICE "['SBJ'=>$varPage]"
$stmt->execute( array('SBJ'=>$varPage) );
$resultSet = $stmt->fetchAll(PDO::FETCH_OBJ);
// LOOP THROUGH THE RESULT-SET AND DO SOME THINGS WITH THE DATA...
foreach($resultSet as $intKey=>$objData){
var_dump($objData->date); //<== DUMP SOME DATA TO THE STREAM
}
You should use the following code
$result = mysqli_query($conn,"SELECT `date` FROM `editor` WHERE subject = '".$varPage."'");
I have a database which has a table called 'propImages' and there are two columns.- 'pid' and 'location'.
And i have data in the database where multiple images can contained by single pid.
image contains database data
now i want to retrieve images from database according to given pid. there can be more than one image.
All i know it there should be an iteration to retrieve images.
I want to display images in HTML .
can you please show me the way to do it in php?
Thanks in advance guys
This may help you
<?php
include 'inc/database.php';
$conn = new mysqli($servername, $username, $password, $database);
$propid = $_GET['propid'];
$sql = "SELECT * FROM propImages WHERE propid='" . $propid . "';";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<img src=" . $row['image'] . ">";
}
}
else {
echo "No results";
}
?>
in the inc/database.php :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "database";
?>
To see how it works try visiting : file.php?propid=22
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "databasename";
// Create connection
$con = mysqli_connect($servername, $username, $password, $dbname);
//create sql
$sql = "SELECT * FROM `propImages` where pid='$YOUR_PID'";
$result = mysqli_query($con, $sql);
$row = mysqli_num_rows($result);
//retrive data print here
if($row > 0){
while($col = mysqli_fetch_assoc($result))
{
echo $col['location'];
}
} else {
echo 'no result found.';
}
?>
wish it helps
I have a notification page. Here's my notification code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM notification ORDER BY date desc";
$result = $conn->query($sql);
?>
<table>
<thead>
<tr style="background-color: #eee">
<th width="20%">Activity</th>
<th width="40%">Description</th>
<th width="20%">Date</th>
<th width="20%">Action</th>
</tr>
</thead>
<tbody>
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td width="20%">'.$row['activity'].'</td>';
echo '<td width="40%">'.$row['description'].'</td>';
echo '<td width="20%">'.$row['date'].'</td>';
?>
<td><a href="/test/admin/requests.php?view_id=<?php echo $row['id']; ?>" >VIEW</a></td>
<?php echo '</tr>';
}
// echo '<td width="20%">'.$row['date'].'</td>';
// echo '</tr>';
}
else {
echo "You have no notifications yet";
}
$conn->close();
$conn=mysql_connect("localhost", "root");
mysql_select_db("testdb", $conn);
if (! $conn){
DIE('Could not connect: ' . mysql_error());
}
$query="UPDATE notification set status ='read'";
$retval = mysql_query( $query, $conn );
?>
</tbody>
</table>
Output:
The code behind that VIEW link is:
<td><a href="/test/admin/requests.php?view_id=<?php echo $row['id']; ?>" >VIEW</a></td>
Now, for instance I click the VIEW link in of the "NEW MEMBERSHIP REQUEST" (refer to the photo please), the URL will lead me to that link and it should the information that I need to view or display for that certain member. And that's where my problem is because whenever I click the click, the information don't show up on the page, but the URL gives the correct ID number of that member..
This code is the page where the information of that certain member should appear when ever I click the link. But it display nothing but the correct id number. Help me please.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['id']))
{
$id=$_GET['id'];
$sql = ("SELECT * FROM requests where id = ".$id);
$result = $conn->query($sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
?>
<?php echo $id ?>
<br/>Name: <?php echo $row['name'] ?>
<br/>Age: <?php echo $row['age'] ?>
<br/>Date of Birth: <?php echo $row['dob'] ?>
<br/>Occupation: <?php echo $row['occupation'] ?>
<?php
}
?>
The name, age, date of birth and occupation are examples of the data that I'm saying which doesn't show up. But there was no error. Please please I wish someone could help me out.
ADDITIONAL:
My notification table has 6 fields:
id, user, activity, desc, status, date
Requests table has 5 fields:
id, name, occupation, dob, age
When the user submits his membership form, the details he had input like name, occupation, dob, and age will be inserted requests table and at the same time it will notify the admin though the notification table/page. Now when I click that link, the ID that shows is the ID from the notification and not from the requests where his datas were stored. So I guess that's where my mistake is. And I just realized that. Hope you can still help me figure out how to get that id from the requests table. I hope I explained my problem well. I understand if you guys didn't understand it. Thank you for those who helped and for those who will help me. :)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_GET['id']))
{
$id=$_GET['id'];
$sql = "SELECT * FROM requests where id ='$id' ";
$result = $conn->query($sql);
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
?>
<?php echo $id ?>
<br/>Name: <?php echo $row['name'] ?>
<br/>Age: <?php echo $row['age'] ?>
<br/>Date of Birth: <?php echo $row['dob'] ?>
<br/>Occupation: <?php echo $row['occupation'] ?>
<?php
}
?>
You are mixing OOP with procedural method. Use either one of them. If you are using OOP then don't use this:
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
It should be
$row=$result->fetch_array(MYSQLI_ASSOC);
i have this problem that my table doesnt show and all info in my database doesnt show also can you help me? the output only shows "Student No. First Name Middle Name Last Name Subject Code Units Final
..." . my info in database doesnt show and the table I created didnt show also . Any answers how to fix this code? thankss
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "tsukishiro";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM student_grades WHERE Student_ID='c2011-02529' ";
$result = $conn->query($sql);
echo "<table>";
echo "<tr><th>Student No.</th><th>First Name</th><th>Middle Name</th><th>Last Name</th><th>Subject Code</th><th>Units</th><th>Final Grade</th><th>Remarks</th><th>Professor</th></tr>";
while($row = mysqli_fetch_array($result)) {
$Student_ID = $row['Student_ID'];
$First_Name = $row['First_Name'];
$Middle_Name = $row['Middle_Name'];
$Last_Name = $row['Last_Name'];
$Subject_Code = $row['Subject_Code'];
$Units = $row['Units'];
$Final_Grade = $row['Final_Grade'];
$Remarks = $row['Remarks'];
$Professor = $row['Professor'];
echo "<tr><td>".$Student_ID."</td><td>".$First_Name."</td><td>".$Middle_Name."</td><td>".$Last_Name."</td><td>".$Subject_Code."</td><td>".$Units."</td><td>".$Final_Grade."</td><td>".$Remarks."</td><td>".$Professor."</td></tr>";
}
echo "</table>";
$conn->close();
?>
this is<table border='1' cellpadding='2' cellspacing ='2'><tr><td>Student No.</td><td>First Name</td><td>Middle Name</td><td>Last Name</td><td>Subject Code</td><td>Units</td><td>Final</td></tr></table>... </div>
thats the only code in that page only