i am trying to output some information based on the edit in the code below :
echo "<td>".$rows['td']."<a href='edittd.php?edit=$rows[Nom_Matiere]'> edit <a></td>";
but the problem is that i want to output this information using 2 different information $rows[Nom_Matiere] and $rows[Number] how can i do that i tried those options :
1*
echo "<td>".$rows['td']."<a href='edittd.php?edit=$rows[Nom_Matiere] and $rows[Number]'> edit <a></td>";
2*
echo "<td>".$rows['td']."<a href='edittd.php?edit=$rows[Nom_Matiere] && $rows[Number]'> edit <a></td>";
but they didn't work ,please is there suggestions how i can do that ,and the edittd.php:
echo mysql_error();
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Mail_Enseignant']))
{echo mysql_error();
$username = $_SESSION['Mail_Enseignant'];
echo mysql_error();
//query the database
if(isset($_GET['edit'])){
$Nom_Matiere = $_GET['edit'];
$Number = $_GET['edit'];
$res = mysql_query("select Nom_Etudiant,Numero from etudiant,groupe,matiere where matiere.`Nom_Matiere`= '".$Nom_Matiere."' and groupe.`Number`= '".$Number."' and matiere.`Id_Specialite`=groupe.`Id_Specialite`")or die($myQuery."<br/> <br/>".mysql_error());
while($rows = mysql_fetch_array($res)):
echo "<tr class='light'>";
echo "<td>".$rows['Nom_Etudiant']."</td>";
echo "<td>".$rows['Numero']."</td>";
endwhile;}
?>
You need a parameter for the second value there.
echo "<td>{$rows['td']}<a href='edittd.php?edit={$rows['Nom_Matiere']}&number={$rows['Number']}'> edit <a></td>";
To make two rows use:
echo "<td>".$rows['td']."</td><td><a href='edittd.php?edit=$rows[Nom_Matiere]& $rows[Number]'> edit <a></td>";
Changed: Added 2 tags instead of your one.
Also your code is full of SQL injection problems, use mysql_real_escape_string or better yet, use somethng better like PDO.
Related
I have a table with registered users. My code is suppose to delete a row when clicking delete in the table.
This is in the database.php
.....
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr><td align="left">' . $row['Id'] . '</td><td align="left">Delete</td></tr>';
}
......
So, I'm getting the id when clicking delete. So far, this part works but when I tried to run the delete query it doesn't work.
delete.php
<?php
session_start();
include 'connection.php';
if (isset($_POST['Id']) && is_numeric($_POST['Id'])){
$id = mysqli_real_escape_string($conn, $_POST['Id']);
$result = mysqli_query("DELETE FROM table_name WHERE Id= '$id' ")
or die(mysqli_error());
echo "<h3><br><br><a href=database.php> <b> Go Back</a></h3>";
echo "Data Deleted";
}else {
echo "Error";
echo "<h3><br><br><a href=database.php> <b> Go Back</a></h3>";
}
?>
I just get "Error" and it doesn't remove the row. How can I fix it?
Edit:
<?php
session_start();
include 'connection.php';
if (isset($_GET['Id']) && is_numeric($_GET['Id']))
{
$id = mysqli_real_escape_string($conn, $_GET['Id']);
$result = mysqli_query("DELETE FROM User_reg WHERE Id= '$id' ")
or die(mysqli_error());
echo "<h3><br><br><a href=AdminLog.php> <b> Go Back</a></h3>";
echo "Data Deleted";
}else {
echo "Error";
echo "<h3><br><br><a href=AdminLog.php> <b> Go Back</a></h3>";
}
?>
Still getting the same result with the delete query not working.
Also "Id" name is set in the same way as in the database.
$_POST['Id'] is not set, because you got to that script via a link.
Delete
links are GET requests, not POST requests. So, $_GET['id'] (note that it is $_GET['id'] rather than $_GET['Id'] because you used id in your link) should be set, but it's not really safe to use a link to delete things to begin with.
There are various ways to get around this issue. One way is to have the delete link in your table direct you to a intermediate confirmation page that posts to the actual delete script.
it would not work because you are sending a get parameters and checking for post and note the comment above for prepared statement and also try not to use get to delete data because a programmer can easily change the id and delete another user info use post instead because it cant be tweaked that is why social use let me callm it ajax to delete, because a deleted cannot be retrieved unless you create an alternative so use POST METHOD instead change this
if (isset($_POST['id']) && is_numeric($_POST['id'])){
$id = mysqli_real_escape_string($conn, $_POST['id']);
to this
if (isset($_GET['id']) && is_numeric($_GET['id'])){
$id = mysqli_real_escape_string($conn, $_GET['id']);
This should work
I'm having a problem in this simple SQL/PHP query...
<?php
$course=$row['course'];
include('../db.php');
$cat=$row['cat'];
$result = mysql_query("SELECT * FROM question WHERE course='$course' AND cat='$cat'");
while($row = mysql_fetch_array($result))
{
echo $row['question'].'?<br>';
$qid=$row['qid'];
echo '<input type="hidden" name="qqqq[]" value="'.$qid.'" />';
echo '<select name="answer[]">';
echo '<option>Select Answer></option>';
$resultik = mysql_query("SELECT * FROM choices WHERE question='$qid' ORDER BY RAND() LIMIT 4");
while($rowik = mysql_fetch_array($resultik))
{
echo '<option>';
echo $rowik['opt'];
echo '</option>';
}
echo '</select><br><br>';
}
?>
Basically, this is a online examination. I want to display all the questions if the student will login. And the questions will be order/arrange according by their course. But eventually, there's no display at all. Not even a single letter will display.
Any help would be appreciated. Thank you so much.
In this there must some POST or GET values to get the course and cat which means
$course=$row['course'];
$cat=$row['cat'];
Since the $row is empty this is the case it will not display anything. Check with isset() like following
$course = isset($row['course']) ? $row['course'] : 'COURSE';
$cat = isset($row['cat']) ? $row['cat'] : 'CAT';
The included file include('../db.php'); please check the database connectivity has established or not?.
I have made a connection to mysql database and echoing values from a table.
while($data = mysql_fetch_array($res))
{
?>
<a href="nextpage.php"<?php echo $data['rowname'] ?></a>
<?php
}
?>
Problem is when I click on the particular link, on the nextpage.php, it should display only the result of the value of a href clicked. So on the nextpage.php, I have defined something like SELECT * from tablename where rowname = 'a href value'.
What's happening now is that it displays only the last rowname value regardless of whichever link I click on, very obvious!
I have tried forms, arrays, SESSIONS but to no avail. How do I fix this?
the href should be like this
<?php echo $data['rowname']; ?>
and then on next page you can use $_GET['val'] and pass it to SELECT query
Try example as below
page1.php
while($data = mysql_fetch_array($res))
{
echo "<a href='nextpage.php?id=".$data['rowname']." >". $data['rowname'] ."</a>";
}
?>
your href will look like nextpage.php?id=[your value]
nextpage.php
$qry = mysql_query("select * from [table_name] where id = ".$_GET['id']."",$con);
while($data = mysql_fetch_array($res))
{
echo $data[0];
}
on nextpage pass value using $_GET['id'] to sql query.
I have the following code that works by outputting as a link ( the link comes from a field in my database) I wish to do the same for the code below, however i cannot get it work, here is the example of what I have that works, and the code that i wish to make output as a link:
Working Code what I want it to look like
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM adrenaline WHERE title LIKE '%".$term."%'";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query)){
echo '<br> '. $row['title'] .'';
}
}
?>
And the code that i have at the moment, it works by be manually typing in the hyper link, however I wish to make it take the link from the database like the example above
//query the database
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
//ferch the results / convert results into an array
WHILE($rows = mysql_fetch_array($query)):
$title = $rows['title'];
echo "<a href='shard.php'>$title</a>";
endwhile;
?>
Many thanks!
I am not 100% certain if this is what you meant to ask... let me know in comments:
<?PHP
$query = mysql_query("SELECT * FROM hobby WHERE id = '1' ");
if(mysql_num_rows($query) >= 1) {
while($rows = mysql_fetch_array($query)) {
echo sprintf("%s", $rows["description"], $rows["title"]);
}
} else { echo "No hobbies found."; }
?>
I believe you might have faced some syntax issues while dealing with quotes parsing a variable in <a html tag. Consider using sprintf something like in my example.
I have also added a mysql_num_rows() just in case and you can see its a good fail-safe method incase there are no rews found on any select query.
IMPORTANT: STOP using mysql_ functions because its deprecated from new PHP versions. Use PDO or mysqli instead.
Hello i am new to php and i have tried to find a piece of code that i can use to complete the task i need, i currently have a page with a form set out to view the criteria of a course. also i have a dropdown menu which currently holds all the course codes for the modules i have stored in a database. my problem is when i select a course code i wish to populate the fields in my form to show all the information about the course selected. The code i am trying to get to work is as follows:
<?php
session_start();
?>
<? include ("dbcon.php") ?>
<?php
if(!isset($_GET['coursecode'])){
$Var ='%';
}
else
{
if($_GET['coursecode'] == "ALL"){
$Var = '%';
} else {
$Var = $_GET['coursecode'];
}
}
echo "<form action=\"newq4.php\" method=\"GET\">
<table border=0 cellpadding=5 align=left><tr><td><b>Coursecode</b><br>";
$res=mysql_query("SELECT * FROM module GROUP BY mId");
if(mysql_num_rows($res)==0){
echo "there is no data in table..";
} else
{
echo "<select name=\"coursecode\" id=\"coursecode\"><option value=\"ALL\"> ALL </option>";
for($i=0;$i<mysql_num_rows($res);$i++)
{
$row=mysql_fetch_assoc($res);
echo"<option value=$row[coursecode]";
if($Var==$row[coursecode])
echo " selected";
echo ">$row[coursecode]</option>";
}
echo "</select>";
}
echo "</td><td align=\"left\"><input type=\"submit\" value=\"SELECT\" />
</td></tr></table></form><br>";
$query = "SELECT * FROM module WHERE coursecode LIKE '$Var' ";
$result = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_num_rows($result) == 0){
echo("No modules match your currently selected coursecode. Please try another coursecode!");
} ELSE {
Coursecode: echo $row['coursecode'];
Module: echo $row['mName'];
echo $row['mCredits'];
echo $row['TotalContactHours'];
echo $row['mdescription'];
echo $row['Syllabus'];
}
?>
however i can only seem to get the last entry from my database any help to fix this problem or a better way of coding this so it works would be grateful
Thanks
The main error is in your final query, you're not actually fetching anything from the query, so you're just displaying the LAST row you fetched in the first query.
Some tips:
1) Don't use a for() loop to fetch results from a query result. While loops are far more concise:
$result = mysql_query(...) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
...
}
2) Add another one of these while loops to your final query, since it's just being executed, but not fetched.
For me i would use some javascript(NOTE: i prefer jQuery)
An easy technique would be to do this(going on the assumption that when creating the drop downs, your record also contains the description):
Apart from creating your dropdown options like this <option value="...">data</option>, you could add some additional attributes like so:
echo '<option value="'.$row['coursecode'].'" data-desc="'.$row['description'].'">.....</option>
Now you have all your drop down options, next is the javascript part
Let's assume you have included jQuery onto your page; and let's also assume that the description of any selected course is to be displayed in a <div> called description like so:
<div id="course-description"> </div>
<!--style it how you wish -->
With your javascript you could then do this:
$(function(){
$("#id-of-course-drop-down").change(function(){
var desc = $(this).children("option").filter("selected").attr("data-des");
//now you have your description text
$("#course-description").html(desc);
//display the description of the course
}
});
Hope this helps you, even a little
Have fun!
NOTE: At least this is more optimal than having to use AJAX to fecch the description on selection of the option :)