Delete specific row in database table and generatet html table - php

This is my delete page :
<?php
require('includes/config.php');
$id = $_GET['ID'];
$pdoConnect = new PDO($db);
$query='DELETE * FROM studentraspored WHERE ID = "' . $id . '" ';
$pdoResult = $db->prepare($query);
$pdoExec = $pdoResult->execute($query);
header('location:index.php');
?>
This is generated table in my “memberpage.php”:
if (count($rows)){
foreach ($rows as $row) {
$_SESSION['row'] = $rows;
$id = floatval($row['ID']);
echo "<tr>" .
'<form action="delete_raspored.php" method="post">'.
"<td>" . $row["ID"] . "</td>" .
"<td>" . $row["den"] . "</td>" .
"<td>" . $row["chas"] . "</td>" .
"<td>" . $row["predmet"] . "</td>" .
"<td>" . $row["profesor"] . "</td>" .
"<td>" . $row["prostorija"] . "</td>" .
"<td>" . $row["tip"] . "</td>" .
'<td><input type="submit" id="' . $id . '" value="Delete" ></td>'.
"</form>".
"</tr>"
This not working properly. I don't understand why maybe something i missed with floatval

Start by trying this:
<?php
require('includes/config.php');
$id = $_GET['ID'];
$query='DELETE FROM studentraspored WHERE ID = ?';
$pdoResult = $db->prepare($query);
$pdoResult->execute(array($id));
header('location:index.php');
exit();
Note the placeholder in place of the actual value, this will prevent SQL injections. The value is passed in in the execute, or you could bind it (http://php.net/manual/en/pdostatement.bindparam.php). http://php.net/manual/en/pdo.prepared-statements.php
The delete syntax was also off, delete deletes a whole row not specific columns, http://dev.mysql.com/doc/refman/5.7/en/delete.html.
In your form I also don't see an element named ID so that could be another issue and your form is submitting via POST, not GET.

Related

Retrieving employee salaries with mysql and php using nymber_format

I am returning employee information from a musql db using php. I am getting the salary to returned with a formatted comma using the number_format() method but when the data is returned, all employees have the same salary. How do I get php to return individual salaries from the employees table?
PHP/MySQL
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
$salary = $row['salary'];
$rows_sal = number_format($salary);
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
number_format doesn't work on an array. Do it like this:
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
//$salary = $row['salary'];
//$rows_sal = number_format($salary);
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . number_format($row['salary']) . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
That's because you're working with the salary outside the foreach loop. (And, by the way, you're leaving the first row out of the table).
Before I suggest you how to do it, let me explain what it's happening with your actual code: (follow the comments)
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
//here you're asking THE FIRST row:
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
$salary = $row['salary'];
//so $rows_sal is the first salary. All this have nothing to do with the iteration below.
$rows_sal = number_format($salary);
echo("<table>");
//here you're asking for the next results (leaving 1st row outside the table!)
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
//and here you're referencing the value set before the while.
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
Now this is what you should do: (follow the comments)
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
//don't ask for results yet!
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH)) //including 1st row
{
//now $row is EACH row, NOW you can format your salary:
$salary = $row['salary'];
$rows_sal = number_format($salary);
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
Hope it helps.

How can I save table echoed into session?

Here's my code:
now, how can I save these in session, so that whenever I go to new link, the table is still there. Thanks guys.
while($row = mysqli_fetch_assoc($locate)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['grade'] . "</td>";
echo "<td>" . $row['section'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
$namers = $row['name'];
//echo "<td><center> <button name = 'name1' type = 'submit' value = '$namers'>Go</button> </center></td>";
Either store $row in a session:
$_SESSION['rowdata'] = $row;
OR serialise the $row to store it and unserialise it when you want to use it:
//Store
$_SESSION['rowdata'] = serialise($row);
//get back
$row = unserialise($_SESSION['rowdata']);

Displaying php table with postdate first

Have a piece of code which displays video posts within an admin panel, for some reason its not displaying any new data. So im wondering if there is a way to display these post with the latest at the top and sorted by date so all new ones will show
if(!empty($post['images'])){
$images = unserialize($post['images']);
}else{
$images = array('no_image.jpg');
}
$content.= "<tr>"
. "<td>{$post['title']}</td>"
. "<td><img src='" . base_url(ltrim($images[0], "./")) ."' widht='25' height='25' /></td>"
. "<td>" . substr(strip_tags($post['content']), 0, 10) . "</td>"
. "<td>"
. "<a href='". site_url('admin/video/edit') . "/" . $post['id'] ."'>Edit</a> | "
. "<a href='". site_url('admin/video/delete') . "/" . $post['id'] ."'>Delete</a>"
. "</td>"
. "</tr>";
You missed $_POST PHP global variable.
Try this:
if(!empty($_POST['images'])){
$images = unserialize($_POST['images']);
}else{
$images = array('no_image.jpg');
}
$content.= "<tr>"
. "<td>{$post['title']}</td>"
. "<td><img src='" . base_url(ltrim($images[0], "./")) ."' widht='25' height='25'/></td>"
. "<td>" . substr(strip_tags($_POST['content']), 0, 10) . "</td>"
. "<td>"
. "<a href='". site_url('admin/video/edit') . "/" . $_POST['id'] ."'>Edit</a> | "
. "<a href='". site_url('admin/video/delete') . "/" . $_POST['id'] ."'>Delete</a>"
. "</td>"
. "</tr>";
this will help.

Trying to get results to show on table with PDO Statement

I am new to PDO trying to figure how to get this to work with PDO. I have it working with MYSQL I maybe confused how PDO works. I am getting a blank page any ideas on how I can go about getting all the results of received records. I see tutorials on PDO but when I do it it is for single records with an array.
<?php
require_once("../db_connect.php");
$stmt = $db->prepare ("SELECT * FROM requests WHERE status='Received'");
echo"Received Requests";
echo "<br><br>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo("<table bgcolor=F2F2F2 width=1080 border='2'>");
echo("<br><tr><th>Id</th><th>Update</th><th>LanID</th><th>Name</th><th>Location</th><th>Manager</th><th>request</th><th>Description</th><th>request_comments</th><th>Status</th><th>Comments</th><th>Completed User</th><th>Completed Date</th></tr>");
echo("<tr>");
echo "<td>". $row['id'] . "</td>"
."<td><a href='../update.php?id=" . $row['id'] . "'>Update</a></td>"
."<td>" . $row['lanId'] . "</td> "
. "<td>". $row['name'] . "</td>"
. "<td>". $row['department'] . "</td>"
. "<td>" . $row['manager'] . "</td>"
. "<td>" . $row['request'] ."</td>"
. "<td>" . $row['request_description'] ."</td>"
. "<td>" . $row['request_comments'] ."</td>"
. "<td>" . $row['status'] ."</td>"
. "<td>" . $row['comments'] ."</td>"
. "<td>" . $row['compUser'] ."</td>"
. "<td>" . $row['compDt'] ."</td>";
echo '</tr>';
}
echo("</table>");
?>
<html>
<head>
<meta http-equiv="refresh" content="5" >
<title>
</title>
</head>
<body background="../images/background.jpg">
</body>
</html>
db_connect.php
<?php
$db_host = "localhost";
$db_username = "root";
$db_pass = "";
$db_name = "systems_requests";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
?>
You forgot to execute your prepared statement.
Please use $stmt->execute () before using $stmt->fetch ()
Also please fetch like so:
$requests = $stmt->fetch (PDO::FETCH_ASSOC);
foreach ($requests as $request) {
echo $request["whateverKeyYouWant"];
}

PHP data entry (SQL)

I have a form that posts to a PHP page. This is a snippet of the page. The goal is to read out a SQL table and then you click on the text values and they are editable. You then edit them and submit and it updates the SQL table. I basically need a way to strip the letter from the beginning of the id. but the letter is important because it defines in what column the new information goes into. Also if there is a different language or method to do this, I am open to suggestions.
<script type="text/javascript">
function exchange(id){
var ie=document.all&&!window.opera? document.all : 0
var frmObj=ie? ie[id] : document.getElementById(id)
var toObj=ie? ie[id+'b'] : document.getElementById(id+'b')
toObj.style.width=frmObj.offsetWidth+7+'px'
frmObj.style.display='none';
toObj.style.display='inline';
toObj.value=frmObj.innerHTML
}
</script>
<?php
$result = mysqli_query($con,"SELECT * FROM List") or die(mysql_error());
var_dump($_POST);
echo "<table id=list>
<tr id='list_header'>
<th class='list'>ID</th>
<th class='list'>Description</th>
<th class='list'>Winner</th>
</tr><form name='edit' action='inde.php?id=prizes' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='list'><span id='n" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Number'] . "</span><input name='n" . $row['Number'] . "b' id='n" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Number'] . "' style='display:none;'></td>";
echo "<td class='list'><span id='d" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Description'] . "</span><input name='d" . $row['Number'] . "b' id='d" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Description'] . "' style='display:none;'></td>";
echo "<td class='list'><span id='w" . $row['Number'] . "' onclick='exchange(this.id)'>" . $row['Winner'] . "</span><input name='w" . $row['Number'] . "b' id='w" . $row['Number'] . "b' class='replace' type='text' value='" . $row['Winner'] . "' style='display:none;'></td>";
echo "<td></td>";
echo "</tr>";
}
echo "</table><input type='submit' value='Save Changes'></form>";
?>`
From what I understand you have a string id and you want to do the following:
if(strlen($id)>1){
$important=substr($id, 0, 1); //that gets the important stuff
$id = ($id, 1, strlen($id)); //that's id without the first char
}
else{
$important=$id;
$id=""; //empty string
}
You may want to try Ajax to send the modified values to the server one by one instead of all together at the end.

Categories