Update Query in PHP - undefined ID error in code - php

I have a problem with my code in php.
Error: Undefined Variable 'id'.
<?php
include_once '../includes/connection.php';
include_once '../includes/functions.php';
session_start();
$posts = $pdo->query("SELECT * FROM posty ORDER BY post_id DESC");
$j=0;
?>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../sources/css/style.css">
<script type="text/javascript" src="../sources/scripts/time.js"></script>
<script type="text/javascript" src="../sources/scripts/scroll.js"></script>
</head>
<body onload="timeLine(); setInterval('timeLine()', 1000 )" >
<header>
<div class = "menu">
<ul>
<li> Blog </li>
<li> Archives </li>
<li> Contact </li>
<li id="addPost"> Add Post </li>
<li id="editPost"> Edit Post </li>
<li id="deletePost"> Delete Post </li>
<li id="showBlogContent"> Hide Blog </li>
<li id="menuRightLogin"> Logout </li>
<li id="menuRightDate"><?php echo date('jS F Y').' '; ?><span id="clock"> </span> </li>
</ul>
</div>
</header>
<div id="showBlog" style="display: block;">
<div class="container">
<div class="postsContainer">
<?php foreach($posts as $post) {
if ($j <= 5) { ?>
<div class="postBox">
<div class="postTitle">
<br />
<?php echo $post['post_title']; ?>
</div>
<div class="postContent">
<br />
<?php if(($wordCount = str_word_count($post['post_content']) <=50)) {
echo $post['post_content'];?>
Edit Post
<br />
<?php } else {
echo excerpts($post['post_content'], 50).'... <br/> <br/>
Edit Post
<br /> <br />';
} ?>
</div>
<div class="postDate">
<?php echo '<b id="author">Author:</b> '.$post['post_author'].' <b id="posted">posted:</b> '.date('jS F Y', $post['add_date']); ?>
</div>
</div>
<?php $j++; } }?>
</div>
</div>
<footer>
<small> © Copyright 2015, n3stis </small>
</footer>
</div>
</body>
</html>
So from here I'm sending a 'id' to edit form:
<?php
include_once '../includes/connection.php';
include_once '../includes/functions.php';
session_start();
$id = $_GET['id'];
if (isset ($id)) {
if (isset($_SESSION['logged_in'])) {
$query = $pdo->prepare("SELECT * FROM posty WHERE post_id='" . $id . "' LIMIT 1");
$query->execute();
$post = $query->fetch();
if (isset($_POST['post_title'], $_POST['post_content'], $_POST['post_author'])) {
$postTitle = $_POST['post_title'];
$postAuthor = $_POST['post_author'];
$postContent = nl2br($_POST['post_content']);
$postTime = time();
if (empty($post_title) or empty($post_content) or empty($post_author)) {
$error = 'All fields required.';
} else {
$sql = ("UPDATE `posty` SET `post_title` = :title, `post_author` = :author, `post_content` = :content, `edit_date` = :editDate WHERE `post_id` = :postID ");
$query = $pdo->prepare($sql);
$query->bindValue(':title', $postTitle);
$query->bindValue(':author', $postAuthor);
$query->bindValue(':content', $postContent);
$query->bindValue(':editDate', $postTime);
$query->bindValue(':postID', $id);
$query->execute();
header('Location: adminPanel.php');
}
}
?>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="../sources/css/style.css">
<script type="text/javascript" src="../sources/scripts/time.js"></script>
<script type="text/javascript" src="../sources/scripts/scroll.js"></script>
</head>
<body onload="timeLine(); setInterval('timeLine()', 1000 )">
<header>
<div class="menu">
<ul>
<li> Blog </li>
<li> Archives </li>
<li> Contact </li>
<li id="#addPost"> Add Post </li>
<li id="#editPost"> Edit Post </li>
<li id="#deletePost"> Delete Post </li>
<li id="showBlogContent"> Hide Blog </li>
<li id="menuRightLogin"> Logout </li>
<li id="menuRightDate"><?php echo date('jS F Y') . ' '; ?><span id="clock"> </span></li>
</ul>
</div>
</header>
<div id="showBlog" style="display: block;">
<div class="container">
<div class="postsContainer">
<?php if (isset($error)) { ?>
<p id="error"><?php echo $error ?> </p>
<?php } ?>
<form action="editPostPanel.php" method="post">
<input type="text" name="post_title" value="<?php echo $post['post_title'];?>"/>
<input type="text" name="post_author" value="<?php echo $post['post_author'];?>"/>
<br/>
<br/>
<textarea name="post_content" rows="15" cols="90"><?php echo $post['post_content'];?></textarea>
<br/>
<br/>
<input type="submit" value="Wyślij post"/>
</form>
</div>
</div>
<footer>
<small> © Copyright 2015, n3stis </small>
</footer>
</div>
</body>
</html>
<?php
} else {
echo 'Error';
} }
else {
echo 'Error';
}
When I send a form I get this:
Notice: Undefined index: id on line 7
I will be very greatfull for yours help :)

change
$id = $_GET['id'];
if (isset ($id))
to
if (isset ($_GET['id'])) {
$id = $_GET['id'];

Your form doesn't have the id parameter in it. You need to change
<form action="editPostPanel.php" method="post">
to:
<form action="editPostPanel.php?id=<?php echo $id; ?>" method="post">
This is in addition to correcting the isset check that the other answers noted:
if (isset($_GET['id'])) {
$id = $_GET['id'];

While I don't have your line numbers, I'd guess that line 7 is $id = $_GET['id'];. This indicates that you don't have a GET (query) param called id set. To fix that Notice, you probably want to swap the two lines as follows:
if (isset($_GET['id'])){
$id = $_GET['id']
But all that will do is start echoing 'Error' back to you. You'll also want to figure out why your line editPostPanel.php?id='.$post['post_id'] is not apparently setting the id as desired.

Related

php pagination has some error

I'm trying to materialize pagination. I want to fetch like
$sql2 = "select * from phptest.memo order by num desc limit $start,$start+$scale";
and i used while paragraph to show memo and ripple (ripple database has parent field that what number of memo and it's order num primary field) but I think it seems like typed wrong code what should i do? It's image that I want make
Expected result:
Error:
<?php
session_start();
// echo "<a href='memo.php?page=$i'> $i </a>";
$scale=5; // page per writing
// start recored number newest writing is up
// $page 값에 다른 시작 레코드 넘버값, 가장 높은 글번호(최신글 부터 밑으로)
$start = ($page - 1) * $scale; //0,5,10 ...
if(!isset($_REQUEST["page"]))
{
$page = 1; // initialize seeing page
}else{
$page = $_REQUEST["page"];
}
require_once '../lib/dbconn.php';
$pdo = db_connect();
if(isset($_SESSION["userid"])){
$userid = $_SESSION["userid"];
}else{
$userid = "";
}
// start recored number newest writing is up
try{
//$sql = "select * from phptest.memo order by num desc";
$sql2 = "select * from phptest.memo order by num desc limit $start,$start+$scale";
$stmh = $pdo->query($sql2);
} catch (PDOException $ex) {`enter code here`
print "오류: ".$ex->getMessage();
}
$total_record = $stmh->rowCount();
$number = $total_record - $start;
// 전체 페이지 수 계산..
if ($total_record % $scale == 0)
$total_page = floor($total_record/$scale);
else
$total_page = floor($total_record/$scale) + 1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<link href="../css/common.css" rel="stylesheet" type="text/css" media="all">
<link href="../css/memo.css" rel="stylesheet" type="text/css" media="all">
</head>
<body>
<div id="wrap">
<div id="header">
<?php include "../lib/top_login2.php"; ?>
</div> <!-- end of header -->
<div id="menu">
<?php include "../lib/top_menu2.php"; ?>
</div> <!-- end of menu -->
<div id="content">
<div id="col1">
<div id="left_menu">
<?php
include "../lib/left_menu.php";
?>
</div>
</div>
<div id="col2">
<div id="title">
<img src="../img/title_memo.gif">
</div>
<?php
// if(isset($_SESSION["userid"])){
?>
<div id="memo_row1">
<form name="memo_form" method="post" action="insert.php">
<?php if(isset($_SESSION['nick'])){ ?>
<div id="memo_writer"><span >▷ <?=$_SESSION['nick'] ?> </span></div>
<?php }?>
<div id="memo1"><textarea rows="6" cols="95" name="content"></textarea></div>
<div id="memo2"><input type="image" src="../img/memo_button.gif"></div>
</form>
</div> <!-- end of memo_row1 -->
<?php // }
while ($row = $stmh->fetch(PDO::FETCH_ASSOC))
{
// $stmh = $pdo->query($sql2);
//$row[$i] = $row;
// $row = $stmh2->fetch(PDO::FETCH_ASSOC);
$memo_id = $row['id'];
$memo_num = $row['num'];
$memo_date = $row['regist_day'];
$memo_nick = $row['nick'];
$memo_content = $row['content'];
$memo_content = str_replace("\n", "<br>", $row['content']);
$memo_content = str_replace(" ", " ", $memo_content);
?>
<div id="memo_writer_title">
<ul>
<li id="writer_title1"><?= $memo_num ?></li> <!--article number -->
<li id="writer_title2"><?= $memo_nick ?></li>
<li id="writer_title3"><?= $memo_date ?></li>
<li id="writer_title4">
<?php
if($userid=="admin" || $userid==$memo_id)
echo "<a href='delete.php?num=$memo_num'>[삭제]</a>";
?>
</li>
</ul>
</div>
<div id="memo_content"><?= $memo_content ?>
</div>
<div id="ripple">
<div id="ripple1">덧글</div>
<div id="ripple2">
<?php
$sql3 = "select * from phptest.memo_ripple where parent=$memo_num";
$stmh3 = $pdo->query($sql3);
while ($row_ripple = $stmh3->fetch(PDO::FETCH_ASSOC))
{
$ripple_num = $row_ripple["num"];
$ripple_id = $row_ripple["id"];
$ripple_nick = $row_ripple["nick"];
$ripple_content = str_replace("\n", "<br>", $row_ripple["content"]);
$ripple_content = str_replace(" ", " ", $ripple_content);
$ripple_date = $row_ripple["regist_day"];
?>
<div id="ripple_title">
<ul>
<li><?= $ripple_nick ?> <?= $ripple_date ?></li>
<li id="mdi_del">
<?php
if($userid=="admin" || $userid==$ripple_id)
echo "<a href='delete_ripple.php?num=$ripple_num'>삭제</a>";
?>
</li>
</ul>
</div>
<div id="ripple_content"> <?= $ripple_content ?></div>
<?php
}
if(isset($_SESSION["userid"])){
?>
<form name="ripple_form" method="post" action="insert_ripple.php">
<input type="hidden" name="num" value="<?= $memo_num ?>">
<div id="ripple_insert">
<div id="ripple_textarea">
<textarea rows="3" cols="80" name="ripple_content"></textarea>
</div>
<div id="ripple_button"><input type="image" src="../img/memo_ripple_button.png"></div>
</div>
</form>
<?php } ?>
</div> <!-- end of ripple2 -->
<div class="clear"></div>
<div class="linespace_10"></div>
<?php
//$number--;
}
?>
<div id="page_num"> ◀ 이전
<?php
// page link num
// 게시판 목록 하단에 페이지 링크 번호 출력
for ($i=1; $i<=$total_page; $i++)
{
if ($page == $i) // ���� ������ ��ȣ ��ũ ����
{
echo "<b> $i </b>";
}
else
{
echo "<a href='memo.php?page=$i'> $i </a>";
}
}
?>
다음 ▶</div>
</div> <!-- end of ripple -->
</div> <!-- end of col2 -->
</div> <!-- end of content -->
</div> <!-- end of wrap -->
</body>
</html>
The SQL doesn't like doing maths in the limit clause. Do it first in PHP:
$sql2 = "select * from phptest.memo order by num desc limit $start," . ($start+$scale);

PHP array output to list

I am trying to output my array to list items within my HTML. It echo out fine but I want to format the output as a list. When I surround the echo $row["item"] in a <li> <?php echo $row["item"] ?> </li> it errors out. I don't know why.
Right now it will echo out from the PHP echo functions just fine into a large block but I want to format the array output into list items which I can style using css and I can't get this to function.
Code
<?php include 'database.php' ; ?>
<?php
$result = mysqli_query($con, "SELECT * FROM shouts");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Shout IT!</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<div id="container">
<header>
<h1>SHOUT IT! shoutbox</h1>
</header>
<div id="shouts">
<?php while($row = mysqli_fetch_array($result)){
echo $row["user"] , $row["message"], $row{"time"};
}
?>
<ul>
<li class="shout">test</li>
<li class="shout"><?php echo $row["user"] ?></li>
<li class="shout"></li>
</ul>
</div>
<div id="input">
<form method="post" action="process.php">
<input type="text" name="user" placeholder="Enter Your Name" />
<input type="text" name="message" placeholder="Enter A Message" />
<br>
<input class="btn" type="submit" name="submit" value="Shout it Out" />
</form>
</div>
</div>
</body>
</html>
You get an error because your <?php echo $row["item"] ?> is outside the while that gets the records from the database. What yout need is something like:
<ul>
<?php while($row = mysqli_fetch_array($result)){
?>
<li class="shout">test</li>
<li class="shout"><?=$row["user"]?></li>
<li class="shout"><?=$row["message"]?></li>
<li class="shout"><?=$row["time"]?></li>
<?php
}
?>
</ul>
instead of the :
<?php while($row = mysqli_fetch_array($result)){
echo $row["user"] , $row["message"], $row{"time"};
}
?>
<ul>
<li class="shout">test</li>
<li class="shout"><? php echo $row["user"] ?></li>
<li class="shout"></li>
</ul>

Blog, adding post doesn't work

So I made a blog with a function to add a post.
This is my code for adding a post
<?php
include_once('resources/init.php');
if(isset($_POST['title'],$_POST['contents'])){
$errors = array();
$title = trim($_POST['title']);
$contents = trim($_POST['contents']);
if(empty($title)){
$errors[] = 'You need to supply a title';
}
else if(strlen($title)>255){
$errors[] = 'The title can not be longer than 255 characters';
}
if(empty($contents)){
$errors[] = 'You need to supply some text';
}
if(empty($errors)){
add_post($title,$contents,$_POST);
header("Location:index.php?id={$id}");
die();
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Post</title>
<style>
label{display: block;}
ul{list-style: none;}
li{display: inline; margin-right: 20px;}
</style>
</head>
<body>
<nav>
<ul>
<li><a href='index.php' >Index</a></li>
<li><a href='add_post.php' >Add a Post</a></li>
<!--li><a href='' ></a></li-->
</ul>
</nav>
<h1>Rogier's blog</h1>
<h2>Add a Post</h2>
<?php
if(isset($errors) && !empty($errors)){
echo"<ul><li>",implode("</li><li>",$errors),"</li></ul>";
}
?>
<form action='' method='post'>
<div>
<label for='title'>Title</label>
<input type='text' name='title' value='<?php if(isset($_POST['title'])) echo $_POST['title']; ?>' />
</div>
<div>
<label for='contents'>Content</label>
<textarea name='contents' cols=20 rows=10><?php if(isset($_POST['contents'])) echo $_POST['contents']; ?></textarea>
</div>
<p><input type='submit' value='Add Post' /></p>
</form>
</body>
</html>
and this is my index.php
<?php
include_once('verwerk.php');
include_once('resources/init.php');
//$posts = (isset($_GET['id'])) ? get_posts($_GET['id']) : get_posts();
$posts = get_posts((isset($_GET['id']))? $_GET['id'] : null);
?>
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
<style>
ul{list-style: none;}
li{display: inline; margin-right: 20px;}
</style>
</head>
<body>
<nav>
<ul>
<li><a href='index.php' >Index</a></li>
<li><a href='add_post.php' >Add a Post</a></li>
<!--li><a href='' ></a></li-->
</ul>
</nav>
<h1>Rogier's blog</h1>
<?php
foreach($posts as $post){
?>
<h2><a href='index.php?id=<?php echo $post['post_id']; ?>' ><?php echo $post['title']; ?></a></h2>
<p>
Posted on <?php echo date('d-m-y h:i:s',strtotime($post['date_posted'])); ?>
In AMP<?php echo $post['name']; ?></a>
</p>
<div><?php echo nl2br($post['contents']); ?></div>
<menu>
<ul>
<li><a href='delete_post.php?id=<?php echo $post['post_id']; ?>' >Delete This Post</a></li>
<li><a href='edit_post.php?id=<?php echo $post['post_id']; ?>' >Edit This Post</a></li>
</ul>
</menu>
<?php
}
?>
</body>
</html>
So the problem is when I click on add post it takes me to the add_post.php where I can write stuff down and add it, only problem is when I click on add post it takes me back to the index.php but my post is not shown.
Can someone help me fix this?
Thanks.

php form submitting info twice

I have a form that's supposed to enter a reply to a forum topic into the database and redirect the user back to the same topic. After much trial and error I have finally got the form to work, only it is putting two identical entries into the db every time. I cannot figure out why. I have looked up this same problem and most of the other people were not redirecting after the form submission or they were using AJAX or jquery or something. Here is my page info:
<?php
session_start();
include_once('includes/config.php');
include_once('classes/topic.php');
include_once('classes/post.php');
include('includes/header.php');
?>
<link rel="stylesheet" href="css/dd.css">
<?php
$topic = new Topic;
if (isset($_GET['id']))
{
$topic_id = $_GET['id'];
$data = $topic->fetch_data($topic_id);
if (isset($_POST['content']))
{
// someone posted a reply
$date = date('Y-m-d H:i:s');
$by = $_SESSION['user_id'];
$query = $pdo->prepare("INSERT INTO dd_posts (post_content, post_date, post_by, post_topic) VALUES (? ,? ,?, ?)");
$query->bindParam(1, $_POST['content']);
$query->bindParam(2, $date);
$query->bindParam(3, $by);
$query->bindParam(4, $_GET['id']);
$query->execute();
$result = $query->execute();
header("location:topic.php?id=".$_GET['id']);
exit;
}
?>
<div id ="wrapper">
<div class="drop-section">
<div id="menu">
<a class="item" href="drop_index.php">Dead Drop</a>
<a class="item" href="add_topic.php">New Post</a>
<a class="item" href="admin/add_cat.php">New Category</a>
<div id="userbar">
<?php
if( $user->is_logged_in() ) {
echo 'Hello ' . $_SESSION['user_name'] . '. How are you?';
} else {
echo '<a class="item" href="login.php">Sign in</a> or <a class="item" href="index.php">Create an account</a>';
}
?>
</div>
</div>
<table>
<tr class = "header-row">
<div id = "sans">
<?php echo $data['topic_subject']; ?>
- <small>started by <?php echo $data['user_name']; ?> </small><br />
<?php echo $data['topic_content']; ?>
</div>
</tr>
<?php
// retrieve all the replies to the original topic
$post = new Post;
$topic_id = $_GET['id'];
$posts = $post->fetch_all_posts_by_topic($topic_id);
?>
<tr>
<td class="first-column">
<?php foreach ($posts as $post) { ?>
<div class="drop-content-box">
<li><?php echo $post['post_content']; ?><br />
<div class = "forum-user-info">
<a href="player.php?id=<?php echo $post['user_id']; ?>">
<?php echo $post['user_name']; ?></a> - level:
<?php echo $post['user_level']; ?>
</div>
</li>
</div>
<?php } ?>
</td>
</tr>
</table>
<?php
if( $user->is_logged_in() )
{
?>
<div id = "header-section">Reply</div>
<?php if (isset($error)) { ?>
<small><?php echo $error; ?></small>
<?php } ?>
<form action="<?php echo "topic.php?id=".$_GET['id']?>" method="post" autocomplete="off">
<small><i>Do not post the actual answer to any level.</i></small><br />
<textarea rows="15" cols="50" name="content" placeholder="Give us your thoughts..."></textarea><br />
<input type="submit" value="Post" />
</form>
</div>
</div>
<?php
} else {
echo '<div id = "errors"><small>You must be signed in to reply.</div></small>';
}
}
include_once('includes/footer.php');
?>
You're executing the query twice.
$query->execute();
$result = $query->execute();

PHP Code will not validate

I am trying to edit a PHP IF statement however, there is a syntax issue according to this online validator:
http://www.piliapp.com/php-syntax-check/
Here is the code:
<?php
if (strpos($row['caption'],'DIGITAL') !== false)
{?><input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
<?php
elseif (strpos($row['price'] != NULL && strpos($row['price']) != '0' && strpos($row['caption'],'DIGITAL') !== true)
{?><input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
This is what I a trying to achieve:
If the "Caption" begins with the text "DIGITAL" then the add to cart button should be active.
If the "Caption does not begin with the text "DIGITAL" AND the "Price" is not NULL or 0 then the shopping cart button should be active. In all other instances, it should be INACTIVE.
Here is the full source code:
<?php include_once('admin/config/config.php'); ?>
<?php include_once('admin/libs/functions.php'); ?>
<?php $obj = new Functions(); ?>
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
<link rel="stylesheet" type="text/css" href="reset.css">
<link rel="stylesheet" type="text/css" href="text.css">
<link rel="stylesheet" type="text/css" href="960.css">
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="stylesheet" type="text/css" href="assets/css/style.css">
<link rel="stylesheet" type="text/css" href="assets/css/nivo-lightbox.css">
<link rel="stylesheet" type="text/css" href="assets/css/default.css">
<script type="text/javascript" src="assets/js/jquery-2.1.1.js"></script>
<script type="text/javascript" src="assets/js/nivo-lightbox.js"></script>
<script type="text/javascript">
$(function(){
$(".view-img").nivoLightbox();
});
</script>
<script type="text/javascript" src="assets/js/shoppingcart.js"></script>
<script type="text/javascript" src="assets/js/general.js"></script>
<meta charset="utf-8">
<meta name="description" content="CENSORED Photography provides the best quality photography in the SENSORED Region. Whether you are looking for a photographer for a wedding, portrait, event or literally anything else, Let your story begin with SENSORED Photography. ">
<meta name="keywords" content="Photography, , Videography, , Photo, Portrait, Best">
<title>SENSORED Photography</title>
<script type="text/javascript">
$(function(){
$(".item_size").change(function(){
item_val = $(this).val();
n = $(this).attr("custval");
if(item_val != "" && item_val != null) {
$("#s"+n).removeAttr("disabled");
}
else {
$("#s"+n).attr("disabled","disabled");
}
});
});
</script>
</head>
<!-- oncontextmenu="return false;"-->
<body>
<div id="wrapper" class="container_12">
<div id="sidebar" class="grid_3">
<img src="images/sidebar_02.png" alt="sidebar"/>
<ul>
<li class="home">HOME</li>
<li class="about">ABOUT</li>
<li class="contact">CONTACT</li>
<li class="gallery" style="color:#AD6FC4;">GALLERY</li>
</ul>
<p>© Copyright SENSORED 2013</p>
</div><!--end sidebar-->
<div id="content" class="grid_9">
<div id="contact" class="grid_3 alpha push_6">
</div><!--end contact-->
<div id="shop_cart">
<span class="shop_cart_quantity"></span> items
<span class="totalfront"></span><span class="shop_cart_total"></span>
View Details
Checkout
Empty Cart
</div>
<div id="cart_overview" style="background: #ffffff; position: absolute; top: 10px; left: 10px; display: none">
<div id="cart_overviewitems" class="shop_cart_items"></div>
<div style="clear:left"></div>
</div>
<div id="action_overview" style="position: absolute; top: 10px; left: 10px; display: none">
<!-- Product Added -->
<div style="clear:left"></div>
</div>
<div id="content2" class="omega grid_9 content2 newpages">
<?php if(isset($_GET['cat']) && $_GET['cat']!= NULL): ?>
<?php
$query = "SELECT * FROM categories WHERE id={$_GET['cat']}";
$data = $obj->select($query);
?>
<h1 class="normalh1 newpagesnew"><?php echo strtoupper($data['name']); ?></h1>
<?php
if($data['protected'] == '1'):
?>
<?php
$cid = $_GET['cat'];
$cookName = "cat".$cid;
if(!isset($_COOKIE[$cookName]) || $_COOKIE[$cookName] != "checked"):
?>
<div id="formWrap">
<form action="category-login.php" method="post" id="category_login">
<?php if(isset($_GET['mess']) && $_GET['mess'] == "err"): ?>
<p>Incorrect Password. Please try again.</p>
<?php else: ?>
<p>This gallery is protected. Please enter the password.</p>
<?php endif; ?>
<input type="hidden" name="cid" value="<?echo $cid; ?>" />
<div class="row">
<div class="input">
<input type="password" id="password" class="detail" name="password" placeholder="Password" required />
</div><!-- end .input -->
</div><!-- end .row -->
<div class="submit">
<input type="submit" id="submit" name="submit" value="login"/>
</div><!-- end .submit -->
</form>
</div>
<?php else: ?>
<?php
$sql = "SELECT * FROM categories WHERE parent_id = {$_GET['cat']} ORDER BY id DESC";
$res = $obj->selectAll($sql);
if($res):
?>
<?php $flag = 1; ?>
<?php foreach($res as $dat): ?>
<div class="grid_2 gal product">
<a href="viewgallery.php?cat=<?php echo $dat['id']; ?>">
<?php
$sql1 = "SELECT * FROM images WHERE category_id={$dat['id']} AND cover=1";
$dat1 = $obj->select($sql1);
if($dat1):
?>
<img src="gal/thumbs/<?php echo $dat1['filename']; ?>" />
<?php else:?>
<img src="images/nopic.png" />
<?php endif; ?>
</a>
<span>
<a href="viewgallery.php?cat=<?php echo $dat['id']; ?>"><?php echo $dat['name']; ?>
</a>
</span>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php if($flag == 1): ?>
<hr />
<?php endif; ?>
<?php
$n=1;
$query1 = "SELECT * FROM images WHERE category_id={$data['id']} ORDER BY id DESC";
$result = $obj->selectAll($query1);
if($result):
?>
<?php foreach($result as $row): ?>
<div class="grid_2 gal product">
<div class="product_item">
<a class="view-img" href="gal/<?php echo $row['filename']; ?>" data-lightbox-gallery="gallery1" <?php echo $row['caption'] != NULL ? 'title="'.$row['caption'].'"' : ''; ?>>
<img src="gal/thumbs/<?php echo $row['filename']; ?>">
</a>
<span class="item_name" style="display:none">Photo <?php echo $data['id']."-".$row['id']; ?></span>
<?php if($row['price'] != NULL && $row['price'] != '0'): ?><span style="margin-bottom: 0">Price: <i class="item_price"><?php echo $row['priceunit'].$row['price']; ?></i></span> <?php endif; ?>
<input type="hidden" class="item_quantity" value="1" style="width:20px;">
<?php if($row['price'] != NULL && $row['price'] != '0'): ?>
<?php
$sql = "SELECT * FROM sizes";
$res = $obj->selectAll($sql);
?>
<select name="item_size" class="item_size" custval="<?php echo $n; ?>" required>
<option value="">Select Size</option>
<?php foreach($res as $line): ?>
<option value="<?php echo $line['width'].' x '.$line['height']; ?>"><?php echo $line['width'].' x '.$line['height']; ?></option>
<?php endforeach; ?>
</select>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart" disabled />
<?php else: ?>
<div style="height:100px; float: left; clear:both"></div>
<?php endif; ?>
</div>
</div>
<?php $n++; ?>
<?php endforeach; ?>
<?php else: ?>
<?php if($flag != 1): ?>
No Image avilable.
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php else: ?>
<?php
$sql = "SELECT * FROM categories WHERE parent_id = {$_GET['cat']} ORDER BY id DESC";
$res = $obj->selectAll($sql);
if($res):
?>
<?php $flag = 1; ?>
<?php foreach($res as $dat): ?>
<div class="grid_2 gal product">
<a href="viewgallery.php?cat=<?php echo $dat['id']; ?>">
<?php
$sql1 = "SELECT * FROM images WHERE category_id={$dat['id']} AND cover=1";
$dat1 = $obj->select($sql1);
if($dat1):
?>
<img src="gal/thumbs/<?php echo $dat1['filename']; ?>" />
<?php else:?>
<img src="images/nopic.png" />
<?php endif; ?>
</a>
<span>
<a href="viewgallery.php?cat=<?php echo $dat['id']; ?>"><?php echo $dat['name']; ?>
</a>
</span>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php if($flag == 1): ?>
<hr />
<?php endif; ?>
<?php
$n=1;
$query1 = "SELECT * FROM images WHERE category_id={$data['id']} ORDER BY id DESC";
$result = $obj->selectAll($query1);
if($result):
?>
<?php foreach($result as $row): ?>
<div class="grid_2 gal product">
<div class="product_item">
<a class="view-img" href="gal/<?php echo $row['filename']; ?>" data-lightbox-gallery="gallery1" <?php echo $row['caption'] != NULL ? 'title="'.$row['caption'].'"' : ''; ?>>
<img src="gal/thumbs/<?php echo $row['filename']; ?>">
</a>
<span class="item_name" style="display:none">Photo <?php echo $data['id']."-".$row['id']; ?></span>
<?php if($row['price'] != NULL && $row['price'] != '0'): ?><span style="margin-bottom: 0">Price: <i class="item_price"><?php echo $row['priceunit'].$row['price']; ?></i></span> <?php endif; ?>
<input type="hidden" class="item_quantity" value="1" style="width:20px;">
<?php if($row['price'] != NULL && $row['price'] != '0'): ?>
<?php
$sql = "SELECT * FROM sizes";
$res = $obj->selectAll($sql);
?>
<select name="item_size" class="item_size" custval="<?php echo $n; ?>" required <?php if (strpos($row['caption'],'DIGITAL') !== false) {?>disabled="disabled"<?php }?>>
<option value="">Select Size</option>
<?php foreach($res as $line): ?>
<option value="<?php echo $line['width'].' x '.$line['height']; ?>"><?php echo $line['width'].' x '.$line['height']; ?></option>
<?php endforeach; ?>
</select>
<?php
if (strpos($row['caption'],'DIGITAL') !== false)
{?><input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
<?php
elseif (strpos($row['price'] != NULL && strpos($row['price']) != '0' && strpos($row['caption'],'DIGITAL') !== true)
{?><input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
<?php else: ?>
<div style="height:100px; float: left; clear:both"></div>
<?php endif; ?>
</div>
</div>
<?php $n++; ?>
<?php endforeach; ?>
<?php else: ?>
<?php if($flag != 1): ?>
No Image avilable.
<?php endif; ?>
<?php endif; ?>
<?php endif;?>
<?php else: ?><!-- post -->
<h1 class="normalh1">Invalid action.</h1>
<?php endif;?>
</div><!--end content2-->
</div><!--end content-->
</div><!--end wrapper-->
<script type="text/javascript" src="assets/js/docevents.js"></script>
</body>
</html>
<!-- Localized -->
Can someone advise why I cannot get my code to validate? it looks fine to me.
The validator states " Parse error: syntax error, unexpected "{" in CODE on line 271.
Error Parsing CODE"
The code returns 500 Internal server error if I run it.
The error is in your elseif condition
(strpos($row['price'] != NULL && strpos($row['price']) != '0' && strpos($row['caption'],'DIGITAL') !== true)
^^ ^
|missing ")" missing 2nd argument
missing 2nd argument
I can't even tell what you're trying to do here. Also, strpos will never return boolean true so your check at the end of this line will never be successful.
I would also suggest using PHP's alternative syntax for control structure when mixing PHP and HTML. Something like this...
<?php if (strpos($row['caption'],'DIGITAL') !== false) : ?>
<input type="button" class="item_add" id="s<?= $n ?>" value="Add to cart"/>
<?php elseif (/* some other logic */) : ?>
<input type="button" class="item_add" id="s<?= $n ?>" value="Add to cart"/>
<!-- this line is exactly the same as the previous one?!? -->
<?php endif ?>
You can check below code in http://www.piliapp.com/php-syntax-check/ url for validation and get "No syntax errors detected in CODE" message.
You can use this code instead of your code definitely work.
<?php if (strpos($row['caption'],'DIGITAL') !== false){ ?>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } elseif ($row['price'] != NULL && $row['price'] != '0' && strpos($row['caption'],'DIGITAL') !== true) {?>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
This got the code to work:
<?php if (strpos($row['caption'],'DIGITAL') !== false){ ?>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php }
elseif ($row['price'] != NULL && $row['price'] != '0' && strpos($row['caption'],'DIGITAL') !== true) {?>
<input type="button" class="item_add" id="s<?php echo $n; ?>" value="Add to cart"/>
<?php } ?>
<?php else: ?>
<div style="height:100px; float: left; clear:both"></div>
<?php endif; ?>

Categories