I am getting an error when I try to edit/update my table. It's weird because I can insert new data into the database fine and I can delete it afterwards fine. I only have problems when editing/updating the data.
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id='1'' at line 6' in C:\Program Files (x86)\Ampps\www\Gats\editargat.php on line 64
This is the code:
<?php
error_reporting( ~E_NOTICE );
require_once 'dbconn.php';
if(isset($_GET['edit_id'])){
$id = $_GET['edit_id'];
$editar = $conn->prepare('SELECT nom, sexe, edat, foto FROM TaulaGats WHERE id=:uid');
$editar->execute(array(':uid'=>$id));
$edit_row = $editar->fetch(PDO::FETCH_ASSOC);
extract($edit_row);
}
else{
header("Location: index.php");
}
if(isset($_POST['btn_save_updates'])){
$nom = $_POST['nom'];
$sexe = $_POST['sexe'];
$edat = $_POST['edat'];
$foto = $_FILES['imatge']['name'];
$carpeta_tmp = $_FILES['imatge']['tmp_name'];
$imatge_mida = $_FILES['imatge']['size'];
if($foto){
$carpetaImatges = 'imatges/';
$imatge_ext = strtolower(pathinfo($foto, PATHINFO_EXTENSION));
$extensions_vàlides = array('jpeg', 'jpg', 'png', 'gif');
$pic = rand(1000,1000000).".".$imatge_ext;
if(in_array($imatge_ext, $extensions_vàlides)){
if($imatge_mida < 5000000){
unlink($carpetaImatges.$edit_row['foto']);
move_uploaded_file($carpeta_tmp, $carpetaImatges.$pic);
}
else{
$error = "La foto és massa gran.";
}
}
else{
$error = "Només les extensions JPG, JPEG, PNG & GIF són admeses.";
}
}
else{
$pic = $edit_row['foto'];
}
if(!isset($error)){
$prepIexec = $conn->prepare('UPDATE TaulaGats
SET nom=:unom,
sexe=:usexe,
edat=:uedat,
foto=:ufoto,
WHERE id=:uid');
$prepIexec->bindParam(':unom', $nom);
$prepIexec->bindParam(':usexe', $sexe);
$prepIexec->bindParam(':uedat', $edat);
$prepIexec->bindParam(':ufoto', $pic);
$prepIexec->bindParam(':uid', $id);
if($prepIexec->execute()){
header("refresh:1;index.php");
}
else{
$error = "No s'ha pogut editar.";
}
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Gats</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="estil.css">
<link rel="stylesheet" href="estil_2.css">
<script src="bootstrap/js/bootstrap.min.js"></script>
<script src="jquery-1.11.3-jquery.min.js"></script>
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.php" title='Gats disponibles'>Disponibles</a>
<a class="navbar-brand" href="reservats.php" title='Gats reservats'>Reservats</a>
<a class="navbar-brand" href="adoptats.php" title='Gats adoptats'>Adoptats</a>
<a class="navbar-brand" href="acollida.php" title="Cases d'acollida">Acollida</a>
<a class="navbar-brand" href="voluntaris.php" title="Llista voluntaris">Voluntaris</a>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1 class="h2">Editar<a class="btn btn-default" href="index.php">Veure llista gats</a></h1>
</div>
<div class="clearfix"></div>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<?php
if(isset($error)){
?>
<div class="alert alert-danger">
<?php echo $error; ?>
</div>
<?php
}
?>
<table class="table table-bordered table-responsive">
<tr>
<td><label class="control-label">Nom</label></td>
<td><input class="form-control" type="text" name="nom" value="<?php echo $nom; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Sexe</label></td>
<td><input class="form-control" type="text" name="sexe" value="<?php echo $sexe; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Edat</label></td>
<td><input class="form-control" type="text" name="edat" value="<?php echo $edat; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Foto</label></td>
<td>
<p><img src="imatges/<?php echo $foto; ?>" height="150" width="200" /></p>
<input class="input-group-edit" type="file" name="imatge" accept="image/*" />
</td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="btn_save_updates" class="btn btn-default btn-guardar">
Guardar
</button>
</td>
</tr>
</table>
</form>
</div>
</body>
</html>
Index.php:
<?php
require_once 'dbconn.php';
if(isset($_GET['eliminar_id']))
{
$prepIexec = $conn->prepare('SELECT foto FROM TaulaGats WHERE id =:uid');
$prepIexec->execute(array(':uid'=>$_GET['eliminar_id']));
$imatgeRow = $prepIexec->fetch(PDO::FETCH_ASSOC);
unlink("imatges/".$imatgeRow['foto']);
$eliminar = $conn->prepare('DELETE FROM TaulaGats WHERE id =:uid');
$eliminar->bindParam(':uid', $_GET['eliminar_id']);
$eliminar->execute();
header("Location: index.php");
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" />
<title>Gats</title>
<link rel="stylesheet" href="estil_index.css">
<link rel="stylesheet" href="estil.css">
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.php" title='Gats disponibles'>Disponibles</a>
<a class="navbar-brand" href="reservats.php" title='Gats reservats'>Reservats</a>
<a class="navbar-brand" href="adoptats.php" title='Gats adoptats'>Adoptats</a>
<a class="navbar-brand" href="acollida.php" title="Cases d'acollida">Acollida</a>
<a class="navbar-brand" href="voluntaris.php" title="Llista voluntaris">Voluntaris</a>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1>Gats en adopció
<a class="btn btn-default" href="afegirgat.php"> + Afegir nou gat </a>
</h1>
</div>
<br />
<div class="row">
<?php
$prepIexec = $conn->prepare('SELECT id, nom, sexe, edat, foto FROM TaulaGats ORDER BY id ASC');
$prepIexec->execute();
if($prepIexec->rowCount() > 0)
{
while($row = $prepIexec->fetch(PDO::FETCH_ASSOC))
{
extract($row);
?>
<div class="col-xs-3">
<p class="page-header infoGats_nom"><?php echo $nom ?></p>
<p class="infoGats">
<?php echo $sexe." / ".$edat; ?>
</p>
<img src="imatges/<?php echo $row['foto']; ?>" class="img-rounded" width="250px" height="200px" />
<p class="page-header">
<span>
<a class="btn btn-info" href="editargat.php?edit_id=<?php echo $row['id']; ?>" title="click per editar"><span class="glyphicon glyphicon-edit"></span> Editar </a>
<a class="btn btn-success" href="mouregat.php?moure_id=<?php echo $row['id']; ?>" title="click per moure"><span class="glyphicon glyphicon-edit"></span> Moure </a>
<a class="btn btn-danger" href="?eliminar_id=<?php echo $row['id']; ?>" title="click per eliminar" onclick="return confirm('Estàs segur que vols el·liminar aquestes dades?')"><span class="glyphicon glyphicon-remove-circle"></span> El·liminar </a>
</span>
</p>
</div>
<?php
}
}
else
{
?>
<div class="col-xs-12">
<div class="alert alert-warning">
No hi ha gats per adoptar.
</div>
</div>
<?php
}
?>
</div>
</div>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
Related
I have a set of products in pagination in products.php , and i when i click "Details" button, i want a modal to popup and show each product Specification that i have in products_details.php.
Page Example : https://i.imgur.com/rrcXCGp.jpg
My code right now only show the 5th ID of each page.
If i am at page 1, it will show A05 of that page when i click details.
If i am at page 2, it will show A10 of that page when i click details.
Below is my products.php
<?php
include_once 'products_crud.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Paw Empire : Products</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<?php include_once 'nav_bar.php'; ?>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3">
<div class="page-header">
<h2>Create New Product</h2>
</div>
<form action="products.php" method="post" class="form-horizontal">
<div class="form-group">
<label for="productid" class="col-sm-3 control-label">ID</label>
<div class="col-sm-9">
<input name="pid" type="text" class="form-control" id="productid" placeholder="Product ID" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_num']; ?>" required>
</div>
</div>
<div class="form-group">
<label for="productname" class="col-sm-3 control-label">Name</label>
<div class="col-sm-9">
<input name="name" type="text" class="form-control" id="productname" placeholder="Product Name" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_name']; ?>" required>
</div>
</div>
<div class="form-group">
<label for="productprice" class="col-sm-3 control-label">Price (RM)</label>
<div class="col-sm-9">
<input name="price" type="number" class="form-control" id="productprice" placeholder="Product Price" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_price']; ?>" min="0.0" step="0.01" required>
</div>
</div>
<div class="form-group">
<label for="productq" class="col-sm-3 control-label">Quantity</label>
<div class="col-sm-9">
<input name="quantity" type="number" class="form-control" id="productq" placeholder="Product Quantity" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_quantity']; ?>" min="0" required>
</div>
</div>
<div class="form-group">
<label for="producttype" class="col-sm-3 control-label">Type</label>
<div class="col-sm-9">
<select name="type" class="form-control" id="producttype" required>
<option value="">Please select</option>
<option value="Cat" <?php if(isset($_GET['edit'])) if($editrow['fld_product_type']=="Cat") echo "selected"; ?>>Cat</option>
<option value="Cat Food" <?php if(isset($_GET['edit'])) if($editrow['fld_product_type']=="Cat Food") echo "selected"; ?>>Cat Food</option>
<option value="Cat Toys" <?php if(isset($_GET['edit'])) if($editrow['fld_product_type']=="Cat Toys") echo "selected"; ?>>Cat Toys</option>
</select>
</div>
</div>
<div class="form-group">
<label for="supplier" class="col-sm-3 control-label">Supplier</label>
<div class="col-sm-9">
<select name="supplier" class="form-control" id="supplier" required>
<option value="">Please select</option>
<option value="My Pets Library" <?php if(isset($_GET['edit'])) if($editrow['fld_product_supplier']=="My Pets Library") echo "selected"; ?>>My Pets Library</option>
<option value="Pet Lovers Centre" <?php if(isset($_GET['edit'])) if($editrow['fld_product_supplier']=="Pet Lovers Centre") echo "selected"; ?>>Pet Lovers Centre</option>
<option value="Pet Smart" <?php if(isset($_GET['edit'])) if($editrow['fld_product_supplier']=="Pet Smart") echo "selected"; ?>>Pet Smart</option>
</select>
</div>
</div>
<div class="form-group">
<label for="productshipping" class="col-sm-3 control-label">Shipping Price (RM)</label>
<div class="col-sm-9">
<input name="shipping" type="number" class="form-control" id="productshipping" placeholder="Shipping Price" value="<?php if(isset($_GET['edit'])) echo $editrow['fld_product_price']; ?>" min="0.0" step="0.01" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<?php if (isset($_GET['edit'])) { ?>
<input type="hidden" name="oldpid" value="<?php echo $editrow['fld_product_num']; ?>">
<button class="btn btn-default" type="submit" name="update"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> Update</button>
<?php } else { ?>
<button class="btn btn-default" type="submit" name="create"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Create</button>
<?php } ?>
<button class="btn btn-default" type="reset"><span class="glyphicon glyphicon-erase" aria-hidden="true"></span> Clear</button>
</div>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
<div class="page-header">
<h2>Products List</h2>
</div>
<table class="table table-striped table-bordered">
<tr>
<th>Product ID</th>
<th>Name</th>
<th>Price (RM)</th>
<th>Quantity</th>
<th>Type</th>
<th>Supplier</th>
<th>Shipping Price (RM)</th>
<th></th>
</tr>
<?php
// Read
$per_page = 5;
if (isset($_GET["page"]))
$page = $_GET["page"];
else
$page = 1;
$start_from = ($page-1) * $per_page;
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("select * from tbl_products_a180834_pt2 LIMIT $start_from, $per_page");
$stmt->execute();
$result = $stmt->fetchAll();
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
foreach($result as $readrow) {
?>
<tr>
<td><?php echo $readrow['fld_product_num']; ?></td>
<td><?php echo $readrow['fld_product_name']; ?></td>
<td><?php echo $readrow['fld_product_price']; ?></td>
<td><?php echo $readrow['fld_product_quantity']; ?></td>
<td><?php echo $readrow['fld_product_type']; ?></td>
<td><?php echo $readrow['fld_product_supplier']; ?></td>
<td><?php echo $readrow['fld_product_shipping']; ?></td>
<td>
<button type="button" class="btn btn-warning btn-xs" data-toggle="myModal" data-target="#exampleModal">Details</button>
Edit
Delete
</td>
</tr>
<?php } ?>
</table>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
<nav>
<ul class="pagination">
<?php
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM tbl_products_a180834_pt2");
$stmt->execute();
$result = $stmt->fetchAll();
$total_records = count($result);
}
catch(PDOException $e){
echo "Error: " . $e->getMessage();
}
$total_pages = ceil($total_records / $per_page);
?>
<?php if ($page==1) { ?>
<li class="disabled"><span aria-hidden="true">«</span></li>
<?php } else { ?>
<li><span aria-hidden="true">«</span></li>
<?php
}
for ($i=1; $i<=$total_pages; $i++)
if ($i == $page)
echo "<li class=\"active\">$i</li>";
else
echo "<li>$i</li>";
?>
<?php if ($page==$total_pages) { ?>
<li class="disabled"><span aria-hidden="true">»</span></li>
<?php } else { ?>
<li><span aria-hidden="true">»</span></li>
<?php } ?>
</ul>
</nav>
</div>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Paw Empire : Product Details</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script>
$('.btn-xs').on('click',function(){
$('.modal-body').load('products_details.php?pid=<?php echo $readrow['fld_product_num']; ?>',function(){
$('#myModal').modal({show:true});
});
});
</script>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
And below is my products_details.php
<?php
include_once 'database.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Paw Empire : Products Details</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<?php include_once 'nav_bar.php'; ?>
<?php
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM tbl_products_a180834_pt2 WHERE fld_product_num = :pid");
$stmt->bindParam(':pid', $pid, PDO::PARAM_STR);
$pid = $_GET['pid'];
$stmt->execute();
$readrow = $stmt->fetch(PDO::FETCH_ASSOC);
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
?>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-5 col-sm-offset-1 col-md-4 col-md-offset-2 well well-sm text-center">
<?php if ($readrow['fld_product_image'] == "" ) {
echo "No image";
}
else { ?>
<img src="products/<?php echo $readrow['fld_product_image'] ?>" class="img-responsive">
<?php } ?>
</div>
<div class="col-xs-12 col-sm-5 col-md-4">
<div class="panel panel-default">
<div class="panel-heading"><strong>Product Details</strong></div>
<div class="panel-body">
Below are specifications of the product.
</div>
<table class="table">
<tr>
<td class="col-xs-4 col-sm-4 col-md-4"><strong>Product ID</strong></td>
<td><?php echo $readrow['fld_product_num'] ?></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td><?php echo $readrow['fld_product_name'] ?></td>
</tr>
<tr>
<td><strong>Price (RM)</strong></td>
<td>RM <?php echo $readrow['fld_product_price'] ?></td>
</tr>
<tr>
<td><strong>Quantity</strong></td>
<td><?php echo $readrow['fld_product_quantity'] ?></td>
</tr>
<tr>
<td><strong>Type</strong></td>
<td><?php echo $readrow['fld_product_type'] ?></td>
</tr>
<tr>
<td><strong>Supplier</strong></td>
<td><?php echo $readrow['fld_product_supplier'] ?></td>
</tr>
<tr>
<td><strong>Shipping Price (RM)</strong></td>
<td><?php echo $readrow['fld_product_shipping'] ?></td>
</tr>
</table>
</div>
</div>
</div>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
You can put the block for modal inside the loop. In the modal content, you can show any content. Check the following code. (Updated)
<?php
// include_once 'products_crud.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>Paw Empire : Products</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-sm-10 col-sm-offset-1 col-md-8 col-md-offset-2">
<div class="page-header">
<h2>Products List</h2>
</div>
<table class="table table-striped table-bordered">
<tr>
<th>Product ID</th>
<th>Name</th>
<th>Price (RM)</th>
<th>Quantity</th>
<th>Type</th>
<th>Supplier</th>
<th>Shipping Price (RM)</th>
<th></th>
</tr>
<?php
$result = array(
array('fld_product_num' => '12345', 'fld_product_name' => 'P One','fld_product_price' => '1000'),
array('fld_product_num' => '123456', 'fld_product_name' => 'P Two','fld_product_price' => '2000')
);
foreach($result as $readrow) {
?>
<tr>
<td><?php echo $readrow['fld_product_num']; ?></td>
<td><?php echo $readrow['fld_product_name']; ?></td>
<td><?php echo $readrow['fld_product_price']; ?></td>
<td>
<button type="button" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#exampleModal<?php echo $readrow['fld_product_num']; ?>">Details</button>
<div class="modal fade" id="exampleModal<?php echo $readrow['fld_product_num']; ?>" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Paw Empire : Product Details</h4>
</div>
<div class="modal-body">
<?php echo $readrow['fld_product_name']; ?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" datadismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
<?php } ?>
</table>
</div>
</div>
</body>
</html>
I've created a page but I can´t put the Save button working and regardless of the user I enter with, it appears the same user information for them all.
I think it's a loop problem but I'm not sure what it is.
<?php
require_once('LoginConfig.php');
session_start();
if(isset($_SESSION["email"])){
$user_check = $_SESSION["email"];
$result = $connect->prepare("SELECT * FROM users WHERE user_email = :usercheck");
$result->execute(array(":usercheck"=>$user_check));
$row = $result->fetch(PDO::FETCH_ASSOC);
}else
{
header("location:UsersLogin.php");
}
?>
<!DOCTYPE html>
<html lang="PT-pt">
<head>
<title> Editar Perfil - JamJam </title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:700, 600,500,400,300' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<link rel="stylesheet" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link rel="stylesheet" type="text/css" href="assets/css/style-menu.css">
<link rel="stylesheet" type="text/css" href="assets/css/style-articlelist2.css">
<script src="https://kit.fontawesome.com/8a368d3752.js"></script>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/data.js"></script>
<script src="assets/js/main.js"></script>
<style>
</style>
</head>
<body>
<div class="header">
<div class="logo">
<i style='font-size:24px' class='fas'></i>
<span style='font-size:24px'>JamJam</span>
</div>
<span></span>
</div>
<div class="side-nav">
<div class="logo">
<i style='font-size:24px; color:rgb(0,255,0)' class='fas'></i>
<span style='font-size:24px'>JamJam</span>
</div>
<nav>
<ul>
<li>
<a href="UsersMenu.php">
<span><i style='color:rgb(0,255,0)' class="fas fa-home"></i></span>
<span>Página Inicial</span>
</a>
</li>
<li class="active">
<a href="UsersProfile.php">
<span><i style='color:rgb(0,255,0)' class="fa fa-user"></i></span>
<span>Perfil</span>
</a>
</li>
<li>
<a href="UsersArticleList.php">
<span><i style='color:rgb(0,255,0)' style='font-size:16px' class='fas'></i></span>
<span>Músicas</span>
</a>
</li>
<li>
<a href="Logout.php">
<span><i style='color:rgb(0,255,0)' class="fas fa-sign-out-alt"></i></span>
<span>Logout</span>
</a>
</li>
</ul>
</nav>
</div>
<div class="main-content">
<div class="title">
Editar Perfil
</div>
<div class="main">
<div class="widget" style="lex-basis: 98%; height: 750px;">
<?php
if(isset($_SESSION["email"])){
$result = $connect->prepare("SELECT user_id, user_name, user_bio, user_local, user_image FROM users");
$result->bindParam(':userid', $id);
$result->execute();
$row = $result->fetch()
?>
<form style="text-align: left; margin: 15px; " action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input $id="user_id" name="user_id" type="hidden" value= "<?php echo $row['user_id']; ?>">
<div >
<label for="username">Nome:</label><br>
<input type="text" name="username" value="<?php echo $row['user_name']; ?>">
</div>
<div>
<label for="bio">Bio:</label>
<br>
<textarea type="text" class="textarea" name="bio" rows ="4"><?php echo $row['user_bio']; ?></textarea>
</div>
<div>
<label for="local">Sítio onde vive:</label><br>
<input type="text" name="local" value="<?php echo $row['user_local']; ?>">
</div>
<div>
<label for="image">Foto de perfil atual:</label><br>
<img src="<?php echo 'images/' . $row['user_image']; ?>">
</div>
<div>
<br> <form action="upload.php" method="post" enctype="multipart/form-data">
<lable for="image">Selecionar nova foto de perfil:</lable><br>
<input type="file" name="fileToUpload" id="fileToUpload">
</form>
</div>
<div style="text-align: left; margin: 10px;" class="box">
<button style="float: left;" class="saveButton" type="submit">Guardar</button>
<a style="float: right;" class="cancelButton" href="UsersProfile.php">Cancelar</a>
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>
Here is my code, right now i have 2 filters 1 for displaying just images and the other for displaying just videos. The thing that i am having difficulty with is displaying images and videos in the same row. Because whenever i do so it will show the video/image and then a blank video/image depending on which of the two fields is emtpy in my db. Does anyone know how i can display videos and images as the default?
<?php
include('config.php');
session_start();
?>
<body class="body">
<form method="post" action="index.php">
<input type="submit" name="switch_video" value="switch to videos" id="switch_video">
</form>
<form method="post" action="index.php">
<input type="submit" name="switch_image" value="switch to images" id="switch_image">
</form>
</span>
</div>
</body>
<?php
if(isset($_POST['switch_video'])) {
$query = "SELECT * FROM meme_vote WHERE image = '' ORDER BY vote DESC LIMIT 500";
$res = mysqli_query($conn, $query);
} if(isset($_POST['switch_image'])) {
$query2 = "SELECT * FROM meme_vote WHERE video_location = '' ORDER BY vote DESC LIMIT 500";
$res_image = mysqli_query($conn, $query2); }
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Meme Voting System</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<?php while($row = mysqli_fetch_assoc($res)){ $location = $row['video_location'];?>
<div class="col-md-4">
<div class="post">
<h4 class="post-title"><?php echo $row['title']?></h4>
<div class="postdesign">
<a href="big_meme.php?id=<?=$row['id'];?>">
<video src="<?php echo $row['video_location'] ?>" width="100%" height="240" controls></video>
</a> <?php /*<img src="data:image/jpeg;base64, <?= base64_encode($row['image']) ?>" height="280" width="240px" class="img-thumnail" />*/ ?>
<span>
<a href="javascript:void(0)" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-thumbs-up " onclick="like_update('<?php echo $row['id']?>')">Like
(<span class="like_loop_<?php echo $row['id']?>"><?php echo $row['vote']?></span>)
</span> </a>
<p class="post_info"><?php echo 'Uploaded by: '; echo $row['username'] ?></p>
<p class="post_info"><?php echo 'Category: '; echo $row['category']?></p>
</span>
</div>
</div>
</div>
<?php }?>
</div>
</div>
<div class="container">
<div class="row">
<?php while($row = mysqli_fetch_assoc($res_image)){?>
<div class="col-md-4">
<div class="post">
<h4 class="post-title"><?php echo $row['title']?></h4>
<div class="postdesign">
<a href="big_meme_image.php?id=<?=$row['id'];?>">
<img src="data:image/jpeg;base64, <?= base64_encode($row['image']) ?>" height="280" width="240px" class="img-thumnail" />
</a> <?php /*<img src="data:image/jpeg;base64, <?= base64_encode($row['image']) ?>" height="280" width="240px" class="img-thumnail" />*/ ?>
<span>
<a href="javascript:void(0)" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-thumbs-up " onclick="like_update('<?php echo $row['id']?>')">Like
(<span class="like_loop_<?php echo $row['id']?>"><?php echo $row['vote']?></span>)
</span> </a>
<p class="post_info"><?php echo 'Uploaded by: '; echo $row['username'] ?></p>
<p class="post_info"><?php echo 'Category: '; echo $row['category']?></p>
</span>
</div>
</div>
</div>
<?php }?>
</div>
</div>
<div class="container">
<div class="row">
<?php while($row = mysqli_fetch_assoc($res)){ $location = $row['video_location'];?>
<div class="col-md-4">
<div class="post">
<h4 class="post-title"><?php echo $row['title']?></h4>
<div class="postdesign">
<a href="big_meme.php?id=<?=$row['id'];?>">
<video src="<?php echo $row['video_location'] ?>" width="100%" height="240" controls></video>
</a> <?php /*<img src="data:image/jpeg;base64, <?= base64_encode($row['image']) ?>" height="280" width="240px" class="img-thumnail" />*/ ?>
<span>
<a href="javascript:void(0)" class="btn btn-info btn-lg">
<span class="glyphicon glyphicon-thumbs-up " onclick="like_update('<?php echo $row['id']?>')">Like
(<span class="like_loop_<?php echo $row['id']?>"><?php echo $row['vote']?></span>)
</span> </a>
<p class="post_info"><?php echo 'Uploaded by: '; echo $row['username'] ?></p>
<p class="post_info"><?php echo 'Category: '; echo $row['category']?></p>
</span>
</div>
</div>
</div>
<?php }?>
</div>
</div>
<style>
.post_info {
position: relative; left: 140px; bottom: 40px;
}
.post {
position: relative; top: 100px;
}
</style>
<script>
function like_update(id){
var cur_count = jQuery('.like_loop_'+id).html();
cur_count++
jQuery('.like_loop_'+id).html(cur_count);
jQuery.ajax({
url:'update_count.php',
type:'post',
data:'type=like&id='+id,
success:function(result){
}
})
}
</script>
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
} </script>
</body>
</html>
Im trying to not show the remove button to the users that did not uploaded image to website, and I want the remove button be shown only for the user that uploaded a specific image.
The problem is, it is in foreach loop, I tried with
if($user_id == $_GET['id']
but it show every button, but when I put
if($user_id != $_GET['id'])
all button disappear.
This is the button I would like to show/hide
<?php
require('dbconfig.php');
if(!$user->is_loggedIn()) {
$user->Redirect('index.php');
}
$user_id = $_SESSION['user_session'];
$stmt = $db_conn->prepare("SELECT * FROM users WHERE user_id=:user_id");
$stmt->execute(array(":user_id"=>$user_id));
$userRow=$stmt->fetch(PDO::FETCH_ASSOC);
// print_r($userRow);
if(isset($_POST['ok'])) {
$folder = "/Library/WebServer/Documents/Website/uploads/";
$image = $_FILES['image']['name'];
$path = $folder . $image ;
$target_file=$folder.basename($_FILES["image"]["name"]);
$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
$allowed=array('jpeg','png' ,'jpg'); $filename=$_FILES['image']['name'];
$ext=pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed)) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
} else {
$success = "Image uploaded successfully";
move_uploaded_file( $_FILES['image'] ['tmp_name'], $path);
$stmt = $db_conn->prepare("INSERT INTO images (img, user_id) VALUES (:image, :user_id)");
$stmt->bindparam(":image",$image);
$stmt->bindparam(":user_id",$user_id);
$stmt->execute();
}
}
$subjects = $db_conn->prepare("SELECT img FROM images");
$subjects->setFetchMode(PDO::FETCH_ASSOC);
$subjects->execute();
$stmt = $db_conn->prepare("SELECT user_id FROM images");
$stmt->execute();
$nesto=$stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<pre>';
print_r($nesto);
echo '</pre>';
// echo $nesto['user_id'];
$ids = $_GET['id'];
?>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="Mark Otto, Jacob Thornton, and Bootstrap contributors">
<meta name="generator" content="Jekyll v4.0.1">
<title>Management</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<style>
.bd-placeholder-img {
font-size: 1.125rem;
text-anchor: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#media (min-width: 768px) {
.bd-placeholder-img-lg {
font-size: 3.5rem;
}
}
</style>
<!-- Custom styles for this template -->
<link href="album.css" rel="stylesheet">
</head>
<body>
<header>
<div class="navbar navbar-dark bg-dark shadow-sm">
<div class="container d-flex justify-content-between">
<a href="#" class="navbar-brand d-flex align-items-center">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" aria-hidden="true" class="mr-2" viewBox="0 0 24 24" focusable="false"><path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"></path><circle cx="12" cy="13" r="4"></circle></svg>
<strong>Pictures</strong>
</a>
Home
Profile
</div>
</div>
</header>
<main role="main">
<section class="jumbotron text-center">
<div class="container">
<h1>Shared Gallery</h1>
<p class="lead text-muted"><?php print($userRow['user_name']); ?></p>
<p>
<p><?php echo $success; ?></p>
<!-- Upload Image Form -->
<div>
<form method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="ok"/>
</form>
</div>
<!-- End Upload Image Form -->
Logout
</p>
</div>
</section>
<div class="album py-5 bg-light">
<div class="container">
<div class="row">
<!-- START -->
<?php foreach($subjects as $subject) { ?>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<img src="uploads/<?php echo $subject['img']; ?>" class="bd-placeholder-img card-img-top" width="100%" height="225" focusable="false"/>
<div class="card-body">
<p class="card-text">
<ul>
<li>Username</li>
<li>Email</li>
<li>Address</li>
</ul>
</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
THIS IS THE BUTTON I WANT TO SHOW AND HIDE
<button type="button" class="btn btn-sm btn-outline-secondary">Remove</button>
THIS IS THE BUTTON I WANT TO SHOW AND HIDE
</div>
<small class="text-muted">9 mins</small>
</div>
</div>
</div>
</div>
<?php } ?>
<!-- END -->
</div>
</div>
</div>
</main>
<footer class="text-muted">
<div class="container">
<p class="float-right">
Back to top
Back to index
</p>
</div>
</footer>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="/docs/4.5/assets/js/vendor/jquery.slim.min.js"><\/script>')</script><script src="/docs/4.5/dist/js/bootstrap.bundle.min.js" integrity="sha384-1CmrxMRARb6aLqgBO7yyAxTOQE2AKb9GfXnEo760AUcUmFx3ibVJJAzGytlQcNXd" crossorigin="anonymous"></script>
</body></html>
If you change these 2 queries into one query, you would have a resultset with the img and the users id, you can then use that to compare with the user that is logged in
//$subjects = $db_conn->prepare("SELECT img FROM images");
//$subjects->setFetchMode(PDO::FETCH_ASSOC);
//$subjects->execute();
//$stmt = $db_conn->prepare("SELECT user_id FROM images");
//$stmt->execute();
//$nesto=$stmt->fetchAll(PDO::FETCH_ASSOC);
Replace as
$result = $db_conn->query("SELECT img, user_id FROM images");
$subjects = $result->fetchAll(PDO::FETCH_ASSOC);
Then around your button you can do
<?php
// If this user uploaded this image they are allowed to remove it
if ($subject->user_id == $_SESSION['user_session']) :
<button type="button" class="btn btn-sm btn-outline-secondary">Remove</button>
endif;
?>
Big Note
I dont see a session_start() in this code, as you are using $_SESSION you would need one of those right at the top of this script.
I decided to do it this way
This worked for me perfectly.
<!-- START -->
<?php foreach($subjects as $subject) : ?>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<img src="uploads/<?php echo $subject['img']; ?>" class="bd-placeholder-img card-img-top" width="100%" height="225" focusable="false"/>
<div class="card-body">
<p class="card-text">
<ul>
<li><?php echo $subject['img_id']; ?></li>
<li><?php echo $subject['user_name']; ?></li>
<li><?php echo $subject['user_email']; ?></li>
<li>Address</li>
</ul>
</p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<?php foreach($subject as $val) : ?>
<?php if ($user_id == $_SESSION['user_session'] && $val == $user_id) : ?>
<?php $id = $subject['img_id']; ?>
<form method="POST" action="<?php echo "delete.php?id=" . $subject['img_id']?>">
<!-- <button name="remove" type="button" class="btn btn-sm btn-outline-secondary">Remove</button> -->
<input type="hidden" name="del" value="1" />
<input type="submit" name="del" class="btn btn-sm btn-outline-secondary" value="Remove" />
</form>
<?php endif; ?>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
<!-- END -->
In my database, I have a table column that has a filepath for images. The filepath's name is photo Some of my rows don't have a filepath. Inside my while loop forshowing the table, I would like to add a condition that if there is no filepath, it would prompt the text "User did not upload a photo yet." and when it has filepath, I can show the filepath and link it with a target blank.
This is my php file for it.
<?php require_once 'process.php';
session_start();
$role = $_SESSION['sess_userrole'];
$name = $_SESSION['sess_name'];
if(!isset($_SESSION['sess_username']) && $role!="admin"){
header('Location: index.php?err=2');
}
?>
<html>
<head>
<title>User Accounts</title>
<link rel="icon" href="isalonlogo.png">
<link rel="stylesheet" href="css2/bootstrap.min.css">
<script src="css/jquery-3.3.1.slim.min.js"></script>
<script src="css/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="css/bootstrap.min.js"></script>
</head>
<body>
<?php
if (isset($_SESSION['message'])):?>
<div class="alert alert-<?=$_SESSION['msg_type']?>">
<?php
echo $_SESSION['message'];
unset ($_SESSION['message']);?>
</div>
<?php endif ?>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<span class="navbar-brand" href=""><?php echo " " . "$name"?>, here are the Stylist user lists.</span>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right">
<li>User Lists</li>
<li><?php echo $_SESSION['sess_username'];?></li>
<li>Logout</li>
</ul>
</div>
</div>
</div>
<br> <br> <br>
<div class="container">
<?php
$mysqli = new mysqli("localhost","id7508046_root","123123123as","id7508046_isalon") or die(mysqli_error($mysqli));
$result = $mysqli->query("SELECT * FROM stylist ") or die($mysqli->error);
?>
<div class="row justify-content-center" width="80%">
<table class="table">
<thead>
<tr>
<th>UserName</th>
<th>Name</th>
<th>Image</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php
while($row = $result->fetch_assoc()): ?>
<tr>
<td><?php echo $row['username'] ?></td>
<td><?php echo $row['name'] ?></td>
// THIS PART HERE IS THE PROBLEM
<td><?php if(strcmp($row['photo'],"") == 0): {echo 'Ola';
}else:
{echo '<img src="'.$row['photo'].'">';
}
endif;
?></td>
<td>
<a href="userlist.php?edit=<?php echo $row['stylist_id']; ?>"
class="btn btn-info">Edit</a>
<a href="process.php?delete=<?php echo $row['stylist_id']; ?>"
class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
<?php
function pre_r($array){
echo '<pre>';
print_r($array);
echo '</pre>';
}
?>
<br>
<br>
<div class="container justify-content-center">
<h5 class=" justify-content-center">Admin <?php echo $name;?>, create or edit an account here.</h5>
<form action="process.php" method="POST">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="form-group">
<label>UserName</label>
<input type="text" name="username" class="form-control" value="<?php echo $username;?>" placeholder="Enter new user Username" required>
</div>
<div class="form-group">
<label>Password</label>
<input type="text" name="password" class="form-control" value="<?php echo $password;?>" placeholder="Enter new user Password" required>
</div>
<div class="form-group">
<?php
if($update == TRUE): ?>
<button type="submit" class="btn btn-primary" name="update">Update</button>
<input type="button" class="btn btn-primary" name="reset" value="Reset" onclick="window.location.href='stylistUserlist.php'">
<?php
else: ?>
<button type="submit" class="btn btn-primary" name="save">Save</button>
<input type="button" class="btn btn-primary" name="reset" value="Reset" onclick="window.location.href='stylistUserlist.php'">
<?php
endif; ?>
</div>
</form>
</div>
</div>
</body>
Try
<td>
<?php if(!empty(trim($row['photo']))): ?>
<img src="<?php echo $row['photo'];?>"/>
<?php else: ?>
User did not upload a photo yet
<?php endif; ?>
</td>
Use below code for TD as you currently added same code in if and else both instead of using if else you can use the ternary operator.
<td>
<?php $photo = (isset($row['photo'])) ? $row['photo'] : "";
echo ($photo) ? "'. <img src=".$photo."> .'" : ""; ?>
</td>