I have a page to view and delete data from database. The delete is a link that clicked and pass the variable in url to another page for delete query. For example i have 3 rows in total in my database, i have no problem in deleting the last two rows but i was unable to delete the first row because the delete is deleting the record based on the "description" column, not the "ID" column. Below is the submission page and delete query page
viewreportdb.php
<!doctype html>
<head>
<meta charset="utf-8">
<title>Report Database </title>
<link href="style/fg_membersite.css" rel='stylesheet' type='text/css'>
</head>
<?php
include('config.php');
include('adminconfig.php');
$sql = "SELECT * FROM report ORDER BY ID";
$result = mysql_query($sql);
?>
<body>
<form method="POST" action="reportsave.php" onSubmit="return validate(this);">
<table id='fcolor2' class="viewdb3">
<tr>
<td style='border:2px solid black; background-color:#FF9933;' align=center> ID </td>
<td style='border:2px solid black; background-color:#FF9933;' align=center> Name </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Department </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Description </td>
<td style='border:2px solid black; background-color:#FF9933; width:auto;' align=center> Date </td>
</tr><?!-- missing close tr -->
<?php
while($row = mysql_fetch_array($result))
{
?>
<tr>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['ID'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['name'] ?></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['department'] ?></td>
<td><textarea rows="5" cols="50" name="description[]"><?php echo $row['description']; ?></textarea></td>
<td style='border:2px solid black; background-color:#33FF00; width:auto' align=center><?php echo $row['date'] ?></td>
</tr><!-- missing close tr -->
<tr>
<td><input class='edit' type = 'text' name="ID[]" value='<?php echo $row['ID'] ?>' maxlength="50"/></td>
<td><input class='edit' type = 'text' name="name[]" value='<?php echo $row['name'] ?>' maxlength="50"></td>
<td><input class='edit' type = 'text' name="department[]" value='<?php echo $row['department'] ?>' maxlength="50"></td>
<td> </td>
<td><input class='edit' type = 'text' name="date[]" value='<?php echo $row['date'] ?>' maxlength="50"></td>
<?php echo "<td><a href='http://localhost/kinhock/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
</tr><!-- missing close tr -->
<?php
} ?>
<tr class='btn2'><td><input type='submit' name='submit' value='Submit' /></td></tr>
<tr class='btn2'><td> Back to Description </td></tr><!-- missing open & close tr-->
<tr class='btn2'><td> Logout</td></tr>
</table>
</form>
<?php
$ID=$row['ID'];
$name=$row['name'];
$department=$row['department'];
$date=$row['date'];
$description=$row['description'];
?>
</body>
</html>
deletereport.php
<?php
include('adminconfig.php');
include('config.php');
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
$result=mysql_query($sql);
if(!$result){
die('invalid query:'.mysql_error());
}else
?>
<p style="font-family:arial;color:#0066CC;font-size:30px;">One row deleted...</p>
<?php
header('Refresh:3; url=viewreportdb.php');
die;
?>
Do not delete using description field because urlencoding and many more factors can break this code, even duplicate descriptions are quite possible, isn't it? Also your code is
liable to easiest SQL Injection methods!
Change your code here:
<?php echo "<td><a href='http://localhost/kinhock/
deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
And your SQL query:
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`='$id'";
STEP 1
Send the ID,not the description, for deletion.
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
STEP 2
Delete using the ID, not the description.
$id = intval($_GET['id']);
$sql="DELETE FROM `report` WHERE `id`=$id";
You should probably use the ID instead of the description within your where clause and in this line:
(change)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?description=".$row['description']."'>Delete</a></td>";?>
(to)
<?php echo "<td><a href='http://localhost/kinhock
/deletereport.php?id=".$row['ID']."'>Delete</a></td>";?>
and finally change:
$description = $_GET['description'];
$sql="DELETE FROM `report` WHERE `description`='$description'";
to:
$id = $_GET['id'];
$sql="DELETE FROM `report` WHERE `ID`='$id'";
Using the ID will be faster and more accurate assuming it is a primary or unique ID.
You may also want to use:
$id = mysql_real_escape_string($_GET['id']);
to prevent SQL injection.
I believe your report table's ids are based on the users IDs right? Why don't you make it such that each report had an ID(report ID), User_ID(eg. admin001) and a description. In this way if admin001 makes report A it will have ID 1, if admin001 makes report B it will have ID 2. si when you want to delete Report A you use
$sql = "delete from {tablename} where ID={report ID}";
Related
I don’t know, how I do this: I want to add $id from database to $_SESSION["dbID"] and after click, it shows me more information from database. But table generates in while function and $_SESSION["dbID"] every time set to the highest number of row from table. Please, Can you anyone change my code as I have $_SESSION["dbID"] on every <tr> of table different? Thank you
while($row = $result->fetch_assoc())
{
$id=$row['ID'];
$name=$row['Name'];
$subject=$row['Subject'];
$date=$row['Date'];
echo
'<tr class="trX" id="'.$id.'" href="google.com&id='.$row['ID'].'">
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $id . '</a></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $name . '</td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $subject . '</td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $date . '</td>
</tr>';
}
$_SESSION["dbID"] = $id;
echo ' </table> ';
Explanation
You can do away with all of the a tags and use JavaScript to handle the redirect...
$url = "/path/to/file.php?id=" . $id;
Set the URL to the page that you want to link to. The line above shows the link to the file "file.php" on the server with the query string "id=$id".
onclick="window.location.href='...'"
The line above is a JS equivalent of href. If you want to navigate to a server outside of your domain remember to add the full url e.g. https://www.website.com
Code example
while ($row = $result->fetch_assoc()) {
$id = $row['ID'];
$name = $row['Name'];
$subject = $row['Subject'];
$date = $row['Date'];
$url = "/url/path.php?id=" . $id;
echo <<<EOT
<tr class="trX" onclick="window.location.href='{$url}'">
<td class="tdX">{$id}</td>
<td class="tdX">{$name}</td>
<td class="tdX">{$subject}</td>
<td class="tdX">{$date}</td>
</tr>
EOT;
}
echo ' </table> ';
if (mysqli_num_rows($sql) > 0) {
$row = mysqli_fetch_assoc($sql);
}
while($row = $result->fetch_assoc()){ ?>
<tr class="trX" id="'.<?php echo $row['ID']; ?>.'" href="google.com&id='.$row['ID'].'">
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['ID']; ?></a></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Name']; ?></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Subject']; ?></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Date']; ?> </td>
</tr>
<?php } ?>
Dont define the rows just make a call for them in the databse.
I'm having trouble setting up some css code for a table I'm generating using php from a database. I want every even row to have a different background-color, so I tried using nth-child(even) for this, but it doesn't seem to work for some reason. Here's my code:
style.css:
#usertable {
border-collapse: collapse;
font-family: "Trebuchet MS", Arial;
}
#usertable td, #usertable th {
border: 1px solid black;
background-color: rgb(228,227,227);
padding: 8px;
}
#usertable tr:nth-child(odd){background-color: rgb(115,115,115);}
admin.php:
<table id="usertable">
<tr>
<th> Id: </th>
<th> Username: </th>
<th> Rights: </th>
</tr>
<?php
$userquery = "SELECT id, username, strength FROM users";
$result = $conn->query($userquery) or die($conn->error);
while ($row = $result->fetch_assoc()) { ?>
<tr>
<td> <?php echo $row['id']; ?> </td>
<td> <?php echo $row['username']; ?> </td>
<td>
<form method="post" action="">
<input type="number" min="0" max="255" name="newrights" value=" <?php echo $row['strength']; ?> ">
<input type="text" name="idnumber" hidden="true" value=" <?php echo $row['id']; ?> ">
<input type="text" name="usertochange" hidden="true" value=" <?php echo $row['username']; ?> ">
<input type="submit" value="Update">
</form>
</td>
</tr>
<?php
}
?>
</table>
There is some markup error with your code
<table id="usertable">
<tr> <!--Add this-->
<th> Id: </th>
<th> Username: </th>
<th> Rights: </th>
</tr> <!--Add this-->
<?php
$userquery = "SELECT id, username, strength FROM users";
$result = $conn->query($userquery) or die($conn->error);
while ($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['username'] . "</td><td>";
echo "<form method=\"post\" action=\"\">
<input type=\"number\" min=\"0\" max=\"255\" name=\"newrights\" value=\"" . $row['strength'] . "\">
<input type=\"text\" name=\"idnumber\" hidden=\"true\" value=\"" . $row['id'] . "\">
<input type=\"text\" name=\"usertochange\" hidden=\"true\" value=\"" . $row['username'] . "\">
<input type=\"submit\" value=\"Update\">
</form></td></tr>";
}
?>
Also looks like you have used many slaces so i suggest you to check table markup on browser
Updated: Add
#usertable td:nth-child(odd) { background-color: #efefef;}
Do not use tr as you have given background color to td and th initially
Why don't you sue some CSS class in each alternate table row?
Ex:
HTML
<tr class="bg-red">
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class="bg-blue">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
CSS
<style type="text/css">
.bg-blue{
background-color: blue;
}
.bg-red{
background-color: red;
}
</style>
I think this will simplify your code & make it easy to read.
Example for Dynamic Data
<?php
$users[] = array('fname'=>'Jill', 'lname'=>'Smith', 'age'=>50);
$users[] = array('fname'=>'Eve', 'lname'=>'Jackson', 'age'=>94);
$rowColClass = 'bg-blue';
foreach ($users as $key => $value)
{
echo "<tr class='$rowColClass'>";
echo "<td>$value[fname]</td>";
echo "<td>$value[lname]</td>";
echo "<td>$value[age]</td>";
echo "</tr>";
if($rowColClass == 'bg-blue')
$rowColClass = 'bg-red';
else
$rowColClass = 'bg-blue';
}
?>
I want to display the data loaded from database after it is inserted using the function add_data() but it's not displaying the data on the table.
if(isset($_POST['btn_add_details'])) {
add_data();
include("db_PSIS.php");
$sql2="SELECT * FROM sample_barcode WHERE IDRec='".$row['IDRec']."'";
$result2=mysql_query($sql2);
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
while($row2=mysql_fetch_array($result2)) {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
?>
Here's the function for adding data on the database:
function add_data() {
include("db_PSIS.php");
$sql="INSERT INTO sample_barcode (LotTraveller, ShipmentLotNumber) VALUES ('".$_POST['traveller']."', '".$_POST['datecode']."')";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
mysql_close($link);
}
Here's the code for database connection:
<?php
$username="****";
$password="****";
$database="****";
$hostname="****";
$link=#mysql_connect($hostname,$username,$password)
or die ("...cannot connect database, using $username for $hostname");
mysql_query("set names 'utf8'"); //指定数据库字符集
mysql_select_db($database,$link);
?>
Here's the form input:
<table width="500" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<tr>
<th align="right">Parent Traveller: </th>
<td><input type="text" name="traveller" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else { echo $row2['LotTraveller']; } ?>"/></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else { echo $row2['ShipmentLotNumber']; } ?>" /></td>
</tr>
<tr>
<th align="right">Traveller 1: </th>
<td><input type="text" name="traveller1" size="20" /></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode1" size="20" /></td>
</tr>
<tr>
<th align="right">Traveller 2: </th>
<td><input type="text" name="traveller2" size="20" /></td>
</tr>
<tr>
<th align="right">Date Code: </th>
<td><input type="text" name="datecode2" size="20" /></td>
</tr>
<tr>
<th align="right"> </th>
<td><input type="hidden" name="id" size="20" value="<?php if(empty($_POST['traveller'])) {echo ''; } else {echo $row2['IDRec'];} ?>"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="btn_add_details" id="btn_add_details" onClick="return check_submit();" value="Add the Detail(s)" onfocus="setStyle(this.id)"
style="color: #000000;
padding: 2px 5px;
border: 2px solid;
border-color: #7bf #07c #07c #4AA02C;
background-color: #09f;
font-family: Georgia, ..., serif;
font-size: 18px;
display: block;
height: 30px;
width: 200px;" /> </td>
</tr>
</table>
Since you stated that IDRec is your PK and is set to auto-increment, AND you are pulling records based on a single number... you will never get more than one result in your resultset.
Change this code block...
$sql2="SELECT * FROM sample_barcode WHERE IDRec='".$row['IDRec']."'";
$result2=mysql_query($sql2);
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
while($row2=mysql_fetch_array($result2)) {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
}
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
To this...
$sql2="SELECT * FROM sample_barcode WHERE IDRec=".$row['IDRec']."";
$result2=mysql_query($sql2);
$row2=mysql_fetch_assoc($result2); // added this line
if(!$result2) {
echo "<h1>Could not process query this time. Please try again later!</h1>";
}
else {
echo "<form name='form2' method='POST'>";
echo "<table class='output' border=2 align=center>";
echo "<tr class='thcolor'>";
echo "<th>Parent</th>";
echo "<th>LOT Traveller No.</th>";
echo "<th>Datecode</th>";
echo "</tr>";
echo "<tr>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['LotTraveller']."</td>";
echo "<td>".$row2['Datecode']."</td>";
echo "</tr>";
echo "</table>";
echo "</form>";
echo "<br><h1>Data successfully loaded!</h1>";
}
mysql_close($link);
}
In the first line I removed the single quotes around your IDRec - this is an integer so single quotes are not needed.
Removed your while loop - you will only ever get one result so a loop is not needed.
Also, this doesn't appear to actually be a form so you can also remove your <form> </form> tags.
Finally, start learning pdo_mysql. my_sql has been deprecated. Anyone still using this code will wake up one day to find out that their website has magically stopped functioning.
EDIT
In your add_data function, change it to this...
function add_data() {
include("db_PSIS.php");
$sql="INSERT INTO sample_barcode (LotTraveller, ShipmentLotNumber) VALUES ('".$_POST['traveller']."', '".$_POST['datecode']."')";
$result=mysql_query($sql);
$sql="SELECT * FROM sample_barcode ORDER BY IDRec DESC"; // added this line
$result=mysql_query($sql); // added this line
$row=mysql_fetch_assoc($result); // changed this line from array to assoc
mysql_close($link);
}
This will get the result of the data you just entered. I added a query to get the data you need to display.
i have this script bellow to open table2 when clicking on the buttom 'more details'
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#Table2').show();
alert($(this).attr('id'));
});
});
</script>
and this my code
<table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
$sql2=..
$sql3 = blabla ;
while($row3 =mysql_fetch_array($sql3)){
$sql4 = mysql_query (" SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and id_user = ".$userid." ");
$row4 = mysql_fetch_array($sql4) ;
$string = "<blink > here </blink>" ;
$wnumber = $row3[$ww] ;
echo "<tr id= '".$wnumber."'><td style= 'text-align : center ;'>Week ".$row3[$ww]."
</td>" ;
echo "<td >".(int) $row3["percent"] ."% </td>";
echo "<td > "?><?php if($row4['place'] ==
$row3[$ww] and $row2['id'] == $userid ){ echo $string ; } else { echo "";} ;?><?php
"</td>";
echo "<td ><button class='showr'>More Details</button> </td></tr>";
//More Details when clicking on this buttom it open the table2
}
</tr>
</table>
this is second table
<?php
echo "<div id= 'Table2' style= 'display:none;'>";
echo "<table width='100%' border='1' cellspacing='0' cellpadding='0'>";
echo "<th> Week ".$wnumber."</th>";
echo "<th>try2</th>";
echo "<tr ><td>day</td>";
echo "<td>fff</td></tr>";
echo "</table></div>";
?>
*what i have now 5 rows with 5 buttoms .
*what it happen now is when clicking on every bottom it echo same '$wnumber' lets say 6.
however it defers from row to row ,
- script works good with alert of the id of which row is clicked.
- only the last buttom who works with the last id of row.
*what i want is every bottom works with its row id which echo the right '$wnumber'
* what i have tried is (make variable in the div)
echo "<div id= '".$wnumber."' style= 'display:none;'>";
instead of
echo "<div id= 'Table2' style= 'display:none;'>";
but didnt work.
hope its clear and there is solution of it.
EDIT : this source code
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#Table2').show();
alert($(this).attr('id'));
});
});
</script>
<br />
<table id= "Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<th >Weeks</th>
<th ><p></p></th>
<th > Your place</th>
<th > More Details</th>
<tr>
<tr id= '1'><td style= 'text-align : center ;'>Week 1</td><td style= 'text-align :
center ;'>33% </td><td style= 'text-align : center ;'> <td style= 'text-align :
center ;'><button class='showr'>More Details</button></td></tr><tr id= '6'><td
style= 'text-align : center ;'>Week 6</td><td style= 'text-align : center ;'>33%
</td><td style= 'text-align : center ;'> <td style= 'text-align : center
;'><button
class='showr'>More Details</button></td></tr><tr id= '13'><td style= 'text-align:
center ;'>Week 13</td><td style= 'text-align : center ;'>33% </td><td style=
'text-align : center ;'> <blink style= 'color:#990000 ;font-weight: bolder;' > 69
here </blink><td style= 'text-align : center ;'><button class='showr'>More
Details</button></td></tr></tr>
</table>
<br />
<div id= 'Table2' style= 'display:none;'><table width='100%' border='1'
cellspacing='0' cellpadding='0'><th> Week 13</th><th>try2</th><tr ><td>day</td>
<td>fff</td></tr></table></div>
<br /><br /> <br />
I tried your html code after correcting the missing td, tr.
And then clicking on each row / button displays the div.
Ensure you form proper html code in your php echo
Try something like this:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$('#Table1 tr').click(function(event){
$('#details').find('#week' + $(this).attr('id')).show();
// alert($(this).attr('id'));
});
});
</script>
<?php
$result = mysql_query($sql);
// save the results to an array so you can loop them twice
while($row = mysql_fetch_array($result)) :
$rows[] = $row;
endwhile;
?>
<table id="table1">
<tr>
<th>heading</th>
<th>heading</th>
<th>heading</th>
<th>heading</th>
</tr>
<?php foreach ($rows as $row) : // loop #1 to output the main table ?>
<tr id="<?php echo $row['ww'] ?>">
<td>value</td>
<td>value</td>
<td>value</td>
<td><button type="button">More details</button></td>
</tr>
<?php endforeach; ?>
</table> <!-- end table 1 -->
<div id="details">
<?php foreach ($rows as $row) : // loop #2 to output a table for each set of details ?>
<table id="week<?php echo $row['ww'] ?>" style="display: none">
<tr>
<th>Week <?php echo $row['ww'] ?></th>
<th>try2</th>
</tr>
<tr>
<td>value</td>
<td>value</td>
</tr>
</table>
<?php endforeach; ?>
</div> <!-- end details -->
I copy / pasted your code into a jsFiddle and it seems to work as expected. Clicking on the row sends an alert with the correct ID.
http://jsfiddle.net/JeeNN/
Is there something I'm missing in your intent here?
Note : Best practice would be to skip the inline CSS and add external styling for your tables. Also, valid HTML is a must.
Update:
I went ahead and formatted the php code you posted so it's a bit easier to read. There's some variables that aren't defined and a couple other issues, but I'm sure you have it correct in your php file.
I think that you're going to want to run the loop a second time to create the second table. In this second loop, echo out the second table once per loop - you'll end up with a bunch of tables (one per row plus the first table). Then simply swap the visibility of the tables as user's click each id.
Is that what you're looking for? If not, perhaps try to rephrase your question.
Here's that formatted code:
<table id="Table1" width='100%' border='1' cellspacing='0' cellpadding='0'>
<?php
$sql2 = [..];
$sql3 = [..];
$ww = ?;
while($row3 = mysql_fetch_array($sql3)){
$sql4 = mysql_query ("SELECT $ww as place FROM data WHERE $ww =".$row3[$ww]." and id_user = ".$userid." ");
$row4 = mysql_fetch_array($sql4) ;
$string = "<blink> here </blink>" ;
$wnumber = $row3[$ww];
echo "<tr id='".$wnumber."'>";
echo "<td style='text-align:center;'>Week ".$row3[$ww]."</td>";
echo "<td>".(int) $row3["percent"] ."% </td>";
echo "<td>":
if($row4['place'] == $row3[$ww] and $row2['id'] == $userid ){
echo $string;
} else {
echo " ";
}
echo "</td>";
//More Details when clicking on this buttom it open the table2
echo "<td ><button class='showr'>More Details</button></td>";
echo "</tr>";
} ?>
</table>
<div id= 'Table2' style= 'display:none;'>
<table width='100%' border='1' cellspacing='0' cellpadding='0'>
<tr>
<th> Week <?php echo $wnumber; ?></th>
<th>try2</th>
</tr>
<tr>
<td>day</td>
<td>fff</td>
</tr>
</table>
</div>
I'm trying to use php with mysql. basically i've an index page where user fills a form and another page where all rows are displayed. i've checkboxes for each row for deleting the selected row/rows. i'm trying to create a new page (namely details) where it shows only the selected row.
I'm trying to use $_GET but i could not do it. maybe the syntax is wrong. any help is welcome.
here are the relative code parts:
display.php:
<?
require_once('auth.php');?>
<html>
<head>
<title>Goruntule</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<?
require "config.php"; // All database details will be included here
$page_name="display.php";
$start=$_GET['start']; // To take care global variable if OFF
if(!($start > 0)) { // This variable is set to zero for the first page
$start = 0;
}
$eu = ($start -0);
$limit = 10; // No of records to be shown per page.
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
// WE have to find out the number of records in our table. We will use this to break the pages
$query2=" SELECT * FROM table1 ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////
/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
echo "<TABLE width=80% align=center cellpadding=5 cellspacing=0> <tr>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>#</font></td>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>ID</font></td>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>Time</font></td>";
echo "</tr>";
////////////// Now let us start executing the query with variables $eu and $limit set at the top of the page///////////
$query=" SELECT * FROM table1 ORDER BY id DESC limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();
//////////////// Now we will display the returned records in side the rows of the table/////////
while($rows = mysql_fetch_array($result))
{
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' value='" . $rows[id] . "'></td>";
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='1'>$rows[id]</font></td>";
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='1'>$rows[DateTime]</font></td>";
echo "<td>Details</td>";
//here is the problematic line i guess
echo "</tr>";
}
echo "</table>";
////////////////////////////// End of displaying the table with records ////////////////////////
///// Variables set for advance paging///////////
$p_limit=100; // This should be more than $limit and set to a value for whick links to be breaked
$p_f=$_GET['p_f']; // To take care global variable if OFF
if(!($p_f > 0)) { // This variable is set to zero for the first page
$p_f = 0;
}
$p_fwd=$p_f+$p_limit;
$p_back=$p_f-$p_limit;
//////////// End of variables for advance paging ///////////////
/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td align='left' width='20%'>";
if($p_f<>0){print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='2'>PREV $p_limit</font></a>"; }
echo "</td><td align='left' width='10%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0 and ($back >=$p_f)) {
print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='2'>PREV</font></a>";
}
//////////////// Let us display the page links at center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit){
if($i <> $eu){
$i2=$i+$p_f;
echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='2'>$i</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$i</font>";} /// Current page is not displayed as link and given font color red
}
echo "</td><td align='right' width='10%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume and $this1 <($p_f+$p_limit)) {
print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='2'>NEXT</font></a>";}
echo "</td><td align='right' width='20%'>";
if($p_fwd < $nume){
print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='2'>NEXT $p_limit</font></a>";
}
echo "</td></tr></table>";
?>
<tr>
<td colspan="14" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
<form>
<INPUT TYPE="BUTTON" VALUE="Previous" ONCLICK="window.location.href='http://......../util'">
</FORM></td>
</tr>
<?php
$checkbox=$_POST['checkbox'];
if($_REQUEST['delete']=='Delete'){
foreach($checkbox as $id => $value)
{$sql="DELETE FROM table1 WHERE id='$value'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display.php\">";
}
}
?>
details
<?
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
detail.php
<?php
require_once('auth.php');
$host="localhost";
$username="";
$password="";
$db_name="";
$tbl_name="table1";
mysql_connect("$host", "$username", "$password")or die("Cannot connect ". mysql_error());
mysql_select_db("$db_name")or die("Cannot select DB ". mysql_error());
$num=$_GET['var1'];
$query = "SELECT * FROM table1 where id='$num'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row($result) or die(mysql_error());
?>
<table border="0" align="center" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="13" align="center" bgcolor="#FFFFFF"><strong>Bölge</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Time</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF" align="center"><? echo $row['13']; ?></td>
<td bgcolor="#FFFFFF" align="center"><? echo $row['0']; ?></td>
</tr>
<tr>
<td colspan="14" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
<form>
<input type=button value="Close" onClick="javascript:window.close();">
</form>
</tr>
<?php
$checkbox=$_POST['checkbox'];
if($_REQUEST['delete']=='Delete'){
foreach($checkbox as $key=>$value)
{$sql="DELETE FROM $tbl_name WHERE id='$value'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
<html><head><link href="loginmodule.css" rel="stylesheet" type="text/css" /></head></html>
As I've said, the only problem i'm guessing is with the syntax, or something small as i can echo the row when i gave the var1 a specific id.
I'm sorry if i'm reposting but i couldn't find an answer. Thanks!
Edit: I'm thinking of deleting checkbox parts and adding gif links in the while loop where users can delete, edit or detailed view of the corresponding row. seems easier i guess.
Make sure that the details link is within the while block.
<?php
while($rows = mysql_fetch_array($result)) {
echo 'Details';
}
?>
Looks like you should not be using double quotes.
<? echo "$rows[id]" ?>
Should be
<?php echo $rows[id]; ?>
I also suggest you use 'id' as the name of the get rather than 'var1'. 'var1' does not mean anything whereas 'id' makes more sense.
Details
It's because you are using $rows outside your while-loop. You have to put the
Details
inside the while loop.
//EDIT:
Alright, I've stripped your code to the very neccessary according to your problem. So don't just copy/paste the code, it probably won't work. But read it carefully, and I hope you get the idea and see what may be wrong with your code ;)
display.php
<?php
require_once('auth.php');
require "config.php";
$page_name="display.php";
$start = (isset($_GET['start']) && $_GET['start'] < 1) ? 0 : $_GET['start'];
$eu = ($start-0);
$limit = 10;
$query="SELECT * FROM table1 ORDER BY id DESC limit $eu, $limit";
$result=mysql_query($query);
echo mysql_error();
$i = 0; //counter for the bg-color
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<table width="80%" align="center" cellpadding="5" cellspacing="0">
<?php while($rows = mysql_fetch_array($result)) :
$bgcolor = $i%2 == 0 ? '#ffffff' : '#f1f1f1';
?>
<tr>
<td>
<input name="checkbox[]" type="checkbox" value="<?php echo $rows['id']; ?>">
</td>
<td style="align: left; font-family: Verdana; font-size: 10px; background-color: <?php echo $bgcolor; ?>;" id="title">
<?php echo $rows['id']; ?>
</td>
<td style="align: left; font-family: Verdana; font-size: 10px; background-color: <?php echo $bgcolor; ?>;" id="date">
<?php echo $rows['DateTime']; ?>
</td>";
<td>
Details
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
detail.php
<?php
require_once('auth.php');
$num= isset($_GET['var1']) ? $_GET['var1'] : '';
$query = "SELECT * FROM table1 where id='$num'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<form name="form1" method="post" action="">
<table border="0" align="center" cellspacing="1" cellpadding="0">
<input name="checkbox[]" type="checkbox" value="<? echo $rows['id']; ?>">
<? echo $row['13']; ?>
<? echo $row['0']; ?>
<input name="delete" type="submit" id="delete" value="Delete">
<button onClick="javascript:window.close();">Close</button>
</table>
</form>
And not only for security's sake, you should get more familiar with PHP and some design patterns before publishing your website.