i used the crud delete method and it works for me, but when i tried it with a different database and table it keeps saying "Invalid argument supplied for foreach()"
this is my "vacature-verwijderen.php" where you should be able to delete it
<div>
<table id="tabel">
<thead>
<tr>
<th>Functie</th>
<th>Omschrijving</th>
<th>Salaris</th>
</tr>
</thead>
<tbody>
<?php
include 'database.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM vacature ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['functie'] . '</td>';
echo '<td>'. $row['omschrijving'] . '</td>';
echo '<td>'. $row['salaris'] . '</td>';
echo '<td width=250>';
echo '<a class="button " href="update.php?id='.$row['id'].'">Update</a>';
echo ' ';
echo '<a class="button" href="delete.php?id='.$row['id'].'">Delete</a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div>
and then when you click delete you go to "delete.php" which looks like this
<?php
require 'database.php';
$id = 0;
if ( !empty($_GET['id'])) {
$id = $_REQUEST['id'];
}
if ( !empty($_POST)) {
$id = $_POST['id'];
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "DELETE FROM vacature WHERE id = ?";
$q = $pdo->prepare($sql);
$q->execute(array($id));
Database::disconnect();
header("Location: vacature-verwijderen.php");
}
?>
i don't know what is wrong since it used to work, also my database table looks like this
foreach ($pdo->query($sql) as $row) {
That's what is wrong. $pdo->query($sql) already returns your $row.
Use a while instead so that it do the query and returns you the new row as long as there is data to fetch :
while ($row = $pdo->query($sql)) {
Also :
$sql = "DELETE FROM vacature WHERE id = ?";
The table you provided doesn't contain any field named id, although there is one named vacature_id. You might want to change that too :
$sql = "DELETE FROM vacature WHERE vacature_id = ?";
Don't hesitate to come back if it doesn't solve your issue.
Related
I want my data to be displayed in a row.
Inside my database there are a lot iPhone pictures and some text associated to that particular picture.
I want that data to be shown left to right , I tried and it's working fine for picture NOT for text.
I would like the text to be displayed from left to right instead of top to bottom.
<h2 class="apple">Apple</h2>
<br>
<?php include 'config.php';
$sql="SELECT * FROM apple";
$result=mysqli_query($con, $sql);
if(mysqli_num_rows($result)>0){
while ($fetch=mysqli_fetch_assoc($result)) {
?>
<img src="<?php ECHO $fetch["url"];?>"width="100" height="100" alt="">
<h1> Hello</h1>
<?php
}
}
?>
You can try to display the data in a table with table rows and table data inside it.
<?php
include ('config.php');
echo '<h2 class="apple">Apple</h2>';
$sql = "SELECT * FROM apple";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
echo '<table>';
while ($fetch = mysqli_fetch_all($result)) {
echo '<tr>';
foreach ($fetch as $values) {
echo '<td>'. $values['index of image'] . '</td>';
}
echo '</tr>';
echo '<tr>';
foreach ($fetch as $values) {
echo '<td>'. $values['index of names'] . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
This should get the job done.
I have a PHP table which consists of three catagories:
finit "material"
disc "discription"
size "dimension"
Similar 'finit' comes in different 'disc' and 'size'
I would like to display the results in a way that 'finit' are displayed only once depending upon the quantity while 'disc' and 'size' are listed within the table under their associated 'finit'
I am able to display 'finit' only once if it contains multiple discriptions etc.
But the listings are not properly set within the table.
they are listed directly under 'finit' and horizontally listed.
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
$finit = $rows['finit'];
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($rows = mysqli_fetch_assoc($info)):
if($rows['finit'] != $finit) {
echo '<tr><td>'.$rows['finit'].'</td></tr>';
$finit = $rows['finit'];
}
echo'<td>'.$rows['disc'].'</td>';
echo'<td>'.$rows['size'].'</td>';
endwhile;
?>
</table>
</div>
</body>
</html>
Your code was not generating correct HTML which may well explain the presentation issues. You would also have lost the first finit value. See comments in code
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
// this will cause the first finit to be completely lost
//$finit = $rows['finit'];
$finit = NULL;
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($info)):
echo '<tr>';
if($row['finit'] != $finit) {
echo '<td>'.$row['finit'].'</td>';
$finit = $row['finit'];
} else {
// when you dont print a finit, you need to output something in that column
echo '<td> </td>';
}
echo '<td>' . $rows['disc'] . '</td>';
echo '<td>' . $rows['size'] . '</td>';
echo '</tr>';
endwhile;
?>
</table>
UPDATE
To format the table so that the finit value is always on its own row followed by rows containing only the other 2 columns, you could try.
All you have to remember is that you have to output the same number of <td>'s on each line, so in this case 3, unless you use the colspan="2" attribute, but try that once you have the simple route working.
<?php
include("connect.php");
$query = "SELECT * FROM prime order by finit asc";
$info = mysqli_query($conn, $query);
// this will cause the first finit to be completely lost
//$finit = $rows['finit'];
$finit = NULL;
?>
<table class="table table-striped">
<tr>
<th>Material</th>
<th>Discription</th>
<th>Dimension</th>
</tr>
<?php
while($row = mysqli_fetch_assoc($info)):
if($row['finit'] != $finit) {
echo '<tr><td>'.$row['finit'].'</td><td> </td><td> </td></tr>';
$finit = $row['finit'];
}
echo '<tr><td> </td>';
echo '<td>' . $rows['disc'] . '</td>';
echo '<td>' . $rows['size'] . '</td>';
echo '</tr>';
endwhile;
?>
</table>
I have a form, wich sends information to the database (When filled in)
Form function:
function train_add() {
$sql = "INSERT INTO train_information "
. "(train_id, image, train_name, tare_weight, number_of_bogies, number_of_axles, wheel_diameter_min, wheel_diameter_max)"
. "VALUES (train_id, :image, :train_name, :tare_weight, :number_of_bogies, :number_of_axles, :wheel_diameter_min, :wheel_diameter_max) ";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(':image', $_POST['image'], PDO::PARAM_STR);
$sth->bindParam(':train_name', $_POST['train_name'], PDO::PARAM_STR);
$sth->bindParam(':tare_weight', $_POST['tare_weight'], PDO::PARAM_STR);
$sth->bindParam(':number_of_bogies', $_POST['number_of_bogies'], PDO::PARAM_STR);
$sth->bindParam(':number_of_axles', $_POST['number_of_axles'], PDO::PARAM_STR);
$sth->bindParam(':wheel_diameter_min', $_POST['wheel_diameter_min'], PDO::PARAM_STR);
$sth->bindParam(':wheel_diameter_max', $_POST['wheel_diameter_max'], PDO::PARAM_STR);
$sth->execute();
return $this->pdo->lastInsertId('train_id');
}
Right. So when the form is filled in, it is send to the database (works).
The user will be redirected in 10 seconds (Did this for myself to see if any errors come up).
<?php
//Train information is send to the database, and selects the ID of the just added train//
$add_train = $database->train_add();
?>
<meta http-equiv="Refresh" content="10;url=http://localhost:8080/show_axle_table.php?train_id="<?php ['$id'] ?> >
show_axle_table.php:
<?php
$show_axle = $database->axles();
?>
<div id="train_select_table">
<table>
<tr>
<th>Train id</th>
<th>Number of bogies</th>
<th>Number of axles</th>
</tr>
<div id="loopRow">
<?php
foreach($show_axle as $res){
//Loop trough results, generate a tablerow every time
?>
<tr>
<?php
echo "<td>" . $res['train_id'] . "</td>";
echo "<td>" . $res['number_of_bogies'] . "</td>";
echo "<td>" . $res['number_of_axles'] . "</td>";
?>
</tr>
<?php
}
?>
</div>
</table>
</div>
And the function:
function axles(){
$id2 = $this->train_add($id);
$sql = "SELECT * FROM train_information WHERE train_id = '$id2'";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":id2", $_GET["train_id"], PDO::PARAM_STR);
$sth->execute();
return $sth->fetchAll();
}
Now the table shows me a ID of a train. But it adds 1 so everything else is empty.
For example:
User fills in form. Sends it to Database (train_add) It gets and id of 1 and is done.
Then the page redirects to the show_axle_table.php. Function should get the last inserted id. but it shows me a ID of 2.
Also i want the ID to be shown in the top of my page like:
show_axle_table.php?train_id="<?php ['$id'] ?>
But right now it shows nothing. (train_id=)
Update like this:
<?php
//Train information is send to the database, and selects the ID of the just added train//
if (!(isset($_GET['train_id]) && $_GET['train_id'])) {
$add_train = $database->train_add();
}
?>
<meta http-equiv="Refresh" content="10;url=http://localhost:8080/show_axle_table.php?train_id="<?php echo $add_train; ?> >
<?php
$show_axle = $database->axles($_GET['train_id']);
?>
<div id="train_select_table">
<table>
<tr>
<th>Train id</th>
<th>Number of bogies</th>
<th>Number of axles</th>
</tr>
<div id="loopRow">
<?php
foreach($show_axle as $res){
//Loop trough results, generate a tablerow every time
?>
<tr>
<?php
echo "<td>" . $res['train_id'] . "</td>";
echo "<td>" . $res['number_of_bogies'] . "</td>";
echo "<td>" . $res['number_of_axles'] . "</td>";
?>
</tr>
<?php
}
?>
</div>
</table>
</div>
And function as
function axles($id){
$sql = "SELECT * FROM train_information WHERE train_id = '$id'";
$sth = $this->pdo->prepare($sql);
$sth->bindParam(":id2", $_GET["train_id"], PDO::PARAM_STR);
$sth->execute();
return $sth->fetchAll();
}
I have table which is populated from mysql. One of the rows is status which is 0 by default and is have dropdown menu where I can choose 1 or 2. Then there is button update which should update that row in the table. The problem is that didn't update.
this is the table form
if(isset($_GET['rest_id']) && is_numeric($_GET['rest_id'])){
$rest_id = $_GET['rest_id'];
$query = mysqli_query($con, "SELECT * FROM reservation
WHERE table_res_id = $rest_id
ORDER BY `DateTime` DESC ");
echo "</p>";
// display data in table
echo '<table class="table table-striped table-bordered responsive">';
echo "<thead>";
echo '<tr>
<th>Name</th>
<th>Comment</th>
<th>Status</th>
<th></th>
</tr>
</thead>';
echo '<div class="row">';
echo '<div class="box col-md-12">';
echo '<div class="box-content">';
echo "<tbody>";
// loop through results of database query, displaying them in the table
while ($res = mysqli_fetch_assoc($query))
{
echo '<tr>';
echo '<td>' . $query['name'] . '</td>';
echo '<td>' . $query['comment'] . '</td>';
echo '<td>
<div class="btn-group">
<select>
<ul class="dropdown-menu">
<li><option> Status:'. $query['status'] .'
<span class="caret"></span>
</option></li>
<li><option>1</option></li>
<li><option>2</option></li>
</ul>
</select>
</div>
</td>';
echo '<td>
<a class="btn btn-info" href="users/Confirm.php?id=' . $query['id'] . '">
<i class="glyphicon glyphicon-edit icon-white"></i>
Change status</a></td>';
echo "</tr>";
This is confirm.php
session_start();
include '../misc/db.inc.php';
ob_start();
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
$query = "SELECT * FROM reservation WHERE id=$id" or die(mysqli_error($con));
$res = mysqli_query($con, $query);
$row = mysqli_fetch_assoc($res);
if ($stmt = $con->prepare("UPDATE reservation SET status = ? LIMIT 1"))
{
$stmt->bind_param("i",$res['status');
$stmt->execute();
$stmt->close();
}
else
{
echo "ERROR: could not prepare SQL statement.";
}
$con->close();
As per your code your are sending database value again to confirm.php not the user selected value.
For this, assign name to like ..and instead of anchor click use form submission.. it will work.
Another solution.
You can call onchange event on where javascript function will get call and will update selected value in database via Ajax..Hope this logic will help you
According to our previous comments:
Make a button <input type="button" onclick="saveChange('.$id.')"/>
Then set the function 'url' to users/confirm.php
and set the 'data' to the id..
function saveChange(id){
$.ajax({
type: "POST",
url: "users/confirm.php",
data: 'id='+id
});
}
Old
I suggest you use something like that:
$.ajax({
type: "POST",
url: "save.php",
data: data,
success: function(msg){}
});
with onchange="" event, and at save.php you simply use an update method.
By the way
$query = "SELECT * FROM reservation WHERE id=$id" or die(mysqli_error($con));
$res = mysqli_query($con, $query);
was ment to be like:
$query = "SELECT * FROM reservation WHERE id=$id";
$res = mysqli_query($con, $query) or die(mysqli_error($con));
I'm trying to make a simple blog to my site and there is no tutorial of how to make one properly, so i'm trying to make one from scratch. My problem right now is to display a template of posts in a loop with variables. Here are the columns of my table:
Table blog
id
user_id
date
title
content
Code of variables:
<?php
// query //
$sql = mysql_query("SELECT id, user_id, date, title, content FROM blog");
$row = mysql_fetch_array($sql);
// variables //
$user_id = $row['user_id']; //only the id, not the name
$date = $row['date'];
$title = $row['title'];
$content = $row['content'];
// get user_id name //
$sql2 = mysql_query("SELECT name FROM users WHERE id = '$user_id'");
$row2 = mysql_fetch_array($sql2);
$author = $row2['name']; //last variable
?>
What i'm trying to do, is to display them all descendant from id value from the next template vertically:
<div id="container">
<div align="center" style="background-color:gray"><b><?php echo $title;?></b></div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php echo $content;?></div>
</div>
<hr>
How could i loop the template with different variables values in a simple page?
try this:
<?php
// query //
$sql = mysql_query ( "
SELECT b.id, b.user_id, b.date, b.title, b.content, u.name
FROM blog b, users u
WHERE b.user_id = u.id
ORDER BY b.id DESC" );
while ( $row = mysql_fetch_array ( $sql ) ) {
// variables //
$user_id = $row ['user_id']; // only the id, not the name
$date = $row ['date'];
$title = $row ['title'];
$content = $row ['content'];
$author = $row ['name']; // last variable
?>
<div id="container">
<div align="center" style="background-color: gray">
<b><?php echo $title;?></b>
</div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php echo $content;?></div>
</div>
<hr>
<?php
}
?>
first make one query for info if you have a lot of rows it will eat resources
SELECT b.id, b.user_id, b.date, b.title, b.content, u.name
FROM blog b, users u
WHERE b.user_id = u.id
ORDER BY b.id DESC
then you need to loop the query using:
while ( $row = mysql_fetch_array ( $sql ) ) {}
First you need to put your blog table in an array:
$sql = mysql_query("SELECT id, user_id, date, title, content FROM blog");
while($row = mysql_fetch_array($sql)) {
$blog[] = $row;
}
Then, in the template:
<div id="container">
<div align="center" style="background-color:gray"><b><?php echo $title;?></b></div>
<div align="right">Author: <?php echo $author;?> Date: <?php echo $date;?></div>
<div><?php
foreach($blog as $val) {
echo "<h1>".$val['title']."</h1>"M
echo $val['content'];
}
?></div>
</div>
<hr>
$records = mysql_fetch_array($sql);
$blogs_table = '<table> <tr> <th>User id</th> <th>Date</th> <th>Title</th> <th>Content</th></tr>';
foreach($records as $row) {
$blogs_table .= '<tr>';
$blogs_table .= '<td>' . $row ['user_id'] .'</td>';
$blogs_table .= '<td>' . $row ['date'] .'</td>';
$blogs_table .= '<td>' . $row ['title'] .'</td>';
$blogs_table .= '<td>' . $row ['content'] .'</td>';
$blogs_table .= '</tr>';
}
$blogs_table .= '</table>';
At last you can print your $blogs_table table to any where like,
<div align="right"><?php echo $blogs_table ?></div>