I wanna pick up record and show but this error happen.
this error was not happen when I add ['] both sides of post and id in line 30.
anyway I don't no why $post['name'] and $post['content'] doesn't appear.
[table has a data by me on this program.]
please give me advice.
<?php
include'php.php';
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>board</title>
</head>
<body>
<form action="#" method="post">
<?php if(count($errors)): ?>
<ul>
<?php foreach($errors as $error): ?>
<li>
<?php echo htmlspecialchars($error, ENT_QUOTES, 'UTF-8') ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<p>name</p>
<input type="text" name="name">
<p>comment</p>
<textarea type="textarea" name="content"></textarea>
<br>
<input type="submit">
</form>
<?php
$sql = "SELECT * FROM post ORDER BY id DESC";
$sth = $dsn->prepare($sql);
$result = $sth->execute();
?>
<?php if ($result !== false && pg_num_rows($result)): ?>
<ul>
<?php while ($post = pg_fetch_assoc($result)): ?>
<li>
<?php echo htmlspecialchars($post['name'], ENT_QUOTES, 'UTF-8'); ?>
<?php echo htmlspecialchars($post['contents'], ENT_QUOTES, 'UTF-8'); ?>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
</body>
</html>
php.php
$dsn = new PDO('pgsql:dbname=board host=127.0.0.1 port=5432', 'postgres', 'Bossmanbig123');
Related
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>
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.
I am struggling to get the value of an element in a particular array. I would like to get the value of the "logo" in this case to return "Logo Google 2013 Official.svg" from the code below.
Any help is greatly appreciated.
<html>
<head>
</head>
<body>
<html>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" value="google" />
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['q'])) {
$search = $_POST['q'];
$url_2 =
"http://en.wikipedia.org/w/api.php?
action=query&prop=revisions&rvprop=content&format=json&titles=$search&rvsection=0&continue=";
$res_2 = file_get_contents($url_2);
$data_2 = json_decode($res_2);
?>
<h2>Search results for '<?php echo $search; ?>'</h2>
<ol>
<?php foreach ($data_2->query->pages as $r):
?>
<li>
<?php foreach($r->revisions[0] as $a);
echo $a; ?>
</li>
<?php endforeach; ?>
</ol>
<?php
}
?>
</body>
</html>
The resulting $url_2 is http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=json&titles=google&rvsection=0&continue=
Use a regular expression to capture what you want:
<ol>
<?php foreach ($data_2->query->pages as $r): ?>
<?php foreach($r->revisions[0] as $a): ?>
<li>
<?php
preg_match_all('/ logo += +([^|]+)/', $a, $result, PREG_PATTERN_ORDER);
echo trim($result[0][0]); // prints 'Logo Google 2013 Official.svg'
?>
</li>
<?php endforeach; ?>
<?php endforeach; ?>
</ol>
Hi could someone point out and explain where I am going wrong in trying to retrieve data from Wikipedia based on a user's search? Please see the code below. Thanks.
<html>
<head></head>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" />
</form>
<?php
// if form submitted
if (isset($_POST['q'])) {
$search = $_POST['q'];
$url = "http://en.wikipedia.org/w/api.php?
action=query&list=search&srwhat=text&format=json&srsearch={$search}&continue=";
$res = file_get_contents($url);
$data = json_decode($res);
?>
<h2>Search results for '<?php echo $_POST['q']; ?>'</h2>
<ol>
<?php foreach ($data->query->search as $r): ?>
<li><a href="http://www.wikipedia.org/wiki/
<?php echo $r['title']; ?>">
<?php echo $r['title']; ?></a> <br/>
<small><?php echo $r['snippet']; ?></small></li>
<?php endforeach; ?>
</ol>
<?php
}
?>
</body>
</html>
Try following updated code:
<html>
<head></head>
<body>
<h2>Search</h2>
<form method="post">
Search: <input type="text" name="q" />
</form>
<?php
// if form submitted
if (isset($_POST['q'])) {
$search = $_POST['q'];
$url = "http://en.wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=json&srsearch=$search&continue=";
$res = file_get_contents($url);
$data = json_decode($res);
echo "<pre>";
print_r($data);
echo "</pre>";exit;
?>
<h2>Search results for '<?php echo $_POST['q']; ?>'</h2>
<ol>
<?php foreach ($data->query->search as $r): ?>
<li><a href="http://www.wikipedia.org/wiki/
<?php echo $r->title; ?>">
<?php echo $r->title; ?></a> <br/>
<small><?php echo $r->snippet; ?></small></li>
<?php endforeach; ?>
</ol>
<?php
}
?>
</body>
</html>
Please remove space before "action" in url
$url = "http://en.wikipedia.org/w/api.php?
action=query&list=search&srwhat=text&format=json&srsearch={$search}&continue=";
Should Be
$url = "http://en.wikipedia.org/w/api.php?action=query&list=search&srwhat=text&format=json&srsearch={$search}&continue=";
You can also use urlencode.
I have two tables listed (on screen) in PHP and the left should be hyperlinked so when click on it the right table will show a query. So at the beginning it should be empty then when clicked refresh the page with the selected listname's result.
unfortunately I have no experience with these things so i don't know the concept of it yet, but I am happy to learn:)
<form method="post" action="test.php">
<div id="left"><table>
<?php
$left="SELECT * FROM groups";
$resultleft=mysql_query($left);
while ($resultleft=mysql_query($left)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
?>
</table></div>
<div id="right"><table>
<?php
$right="SELECT * FROM grouplink WHERE grouplink.group_id= ";
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
echo "<tr><td>'.$right['people_name']."</td></tr>";
}
?>
</table></div>
<?php
if (isset($_POST('???'))){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=test.php\">";
}
?>
</form>
any help would be appreciated
Can for example link to table.php?gid=n, where n would be the group id. You can then check if $_GET['gid'] isset, and if it is, take that id and put it in your query.
if(isset($_GET['gid']))
$right = sprintf("SELECT * FROM grouplink WHERE grouplink.group_id=%u", $_GET['gid']);
You need mysql_fetch_assoc
instead of mysql_query
in:
while ($resultleft=mysql_query($left)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
and also in:
$resultright=mysql_query($right);
while ($resultright=mysql_query($right)) {
echo "<tr><td>'.$right['people_name']."</td></tr>";
so this will be:
while ($left=mysql_fetch_assoc($resultleft)) {
echo "<tr><td>".$left['id'].'</td><td>'.$left['listname']."</td></tr>";
}
and similar issue with right
Thanks to Svish here is my new working code:
<?php include("db_con1.php");?>
<html>
<head>
</head>
<body>
<form method="post" action="test.php">
<div id="left">
<?php
$queryl = $pdo->prepare('SELECT id, name FROM test1 ORDER BY name ASC');
$queryl->execute();
?>
<ul>
<?php foreach ($queryl as $i => $rowl) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_add[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowl['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $rowl['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
<div id="right">
<?php
if(isset($_GET['gid'])) {
$gid=$_GET['gid'];
$queryr = $pdo->prepare('SELECT test3.name FROM test1, test2, test3 WHERE test1.id=test2.groupid AND test3.id=test2.peopleid AND test1.id='.$gid.' ORDER BY test3.name ASC');
$queryr->execute();
}
?>
<ul>
<?php foreach ($queryr as $i => $rowr) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_del[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $rowr['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $rowr['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
</form>
</body>
</html>