I have a login script with a Twitter-like posting script and I tried to insert the user name into the posts but that didn't work. They are both using the same database but I can't figure out why. Here is the php.
<?php
session_start();
require_once 'database.php';
if (isset($_SESSION['user'])){
echo "Welcome ".$_SESSION['user'];
?>
<?php
$posts = show_posts($_SESSION['userid']);
if (count($posts)){
?>
Now the posts part
<table class="imagetable">
<table align="center" border='0' cellspacing='0' cellpadding='5' width='300'>
<td background="cell-blue.jpg">
<?php
foreach ($posts as $key => $list){
echo "<tr valign='middle'>\n";
echo "<td>".".$_SESSION['user']" . "<p>'s BFFL is</p>".$list['body'] ."<br/>\n ";
echo "<small>".$list['stamp'] ."<hr>"."</small></td>\n";
echo "</tr>\n";
}
?>
</table>
I'm a noob to php, and I can't get why it's not working... I just want the name of the user to be in the post.
There was an extra period (concat operator) before your session variable. I cleaned it up for you.
echo "<td>" . $_SESSION['user'] . "<p>'s BFFL is</p>" . $list['body'] . "<br/>\n";
Related
I've gotten into trouble while working on a school project, and I'm stuck.
I'm making a website where you can easily search for amusement parks.
I made a while loop with PHP and put information from the DB into the table which I looped. I've put all the information in a TD and I want to create a-href so when I click on one of the first parks it will redirect to an example: example.php?park=1 for the second park example: example.php?park=2.
Here is the while:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
Each pretpark has an ID, the DB table of it is called ParkId.
Can someone help me where I have to put the a href. And how I can do it like its like this example: example.php?park= ParkId
I hope everything is understandable what I just said :/
Thanks,
You can do like this:
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
echo "<td><a href='example.php?park=".$pretpark['Parkid']."'> More Details</a></td>";
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
<div class="pretpark-container">
<table id="pretpark-table">
<?php while($pretpark = mysqli_fetch_array($result)){
echo "<tr>";
if(file_exists(__DIR__ . '/img/'. $pretpark['ParkImage'])): ?>
<td><img height="125px;" width="200px;"" src="/WikiParksWeb/Wikiparks-Website/img/<?php print($pretpark['ParkImage']); ?>"></td>
<?php else: ?>
<?php endif;
echo "<td>".$pretpark['ParkName']."</td>";
echo "<td>".$pretpark['ParkLocation']."</td>";
echo "<td>".$pretpark['ParkOpeninsTime']."</td>";
echo "<td>".$pretpark['ParkPrices']."</td>";
echo "<td>".$pretpark['ParkShortDescription']."</td>";
**echo "<td><a href='detailpage.php?id=".$pretpark['ParkId']."'></td>";**
echo "</tr>";
?>
<tr class="filler"></tr>
<?php
}
?>
</table>
</div>
I have added a line with bold formatting. You can get clue from this.
instead of the line
echo "<td>".$pretpark['ParkName']."</td>";
it would be something like..
echo "<td>".$pretpark['ParkName']."</td>";
backslashes are needed to escape the quotes so they come out in the HTML and are not interpreted by php
Right guys here is my big ass question, I'm just a begginer and all of that so it's probably just a silly and easy thing but how from this code
<?php
session_start();
if(!isset($_SESSION['password']))
{
header('Location:index.php');
exit();
if ($_SESSION['logged_in']= 0)
{
header('Location:index.php');
exit();
}
}
?>
<?php include 'includes/header.php';?>
<div class="page_menu_box">
<ul>
<li>USER PROFILE</li>
<li>ABOUT US</li>
</ul>
</div>
<div class="page_content">
<table class="viewbook_table">
<tr>
<th>ISBN</th>
<th>AUTHOR</th>
<th>DATE WHEN PUBLISHED</th>
<th>COVER</th>
</tr>
<?php
include("core/database/connect.php");
$connection = #new mysqli('localhost','root','','bs_admin_tools');
$sql = "SELECT * FROM books";
$query = $connection->query( $sql );
while ($row = mysqli_fetch_array($query))
{
echo "<tr>";
echo "<td width = 15%>";
echo $row['ISBN'];
echo "</td>";
echo "<td width = 15%>";
echo $row['author'];
echo "</td>";
echo "<td width = 15%>";
echo $row['datepublished'];
echo "</td>";
//check whether an image is available for the record chosen
if ( $row['imgdir'] )
{
//create a variable which holds data from the images folder
$imageDir = "image/";
//create a second variable which holds the value of the image linked to the record selected
$img = $imageDir . $row['imgdir'];
/*link to the directory*/
echo "<td width ='15%' padding='30%'>";
//display the image
echo "<left><a href='viewdetails'><img src='$img'></a>";
echo "</td></tr>";
}
//echo "<br />"; //display a line break
}//end of the while loop
//release connection from database
mysqli_close($connection);
?>
</table>
</div>
<?php
echo "<div class='page_menu_box'><ul><li>
".$_SESSION['login']."
</ul></li></div>'";
include 'includes/footer.php';?>
I just want to basically click on the $img and view the book details, in other words I make one of the variables into a hyperlink and whenever someone is transferred onto another page I want a variable to be sended to that file where I would facilitate the file to intake the variable and display other shit about that book using 'Select from books where $theVariable="XD"'. I would be really happy if anyone could help me, I'm bouncing my head over a wall for the 2nd day to work that out.
I can only do stuff on php so please solutions only involving php or html ;_;
I have a script written to grab a row from my database based on current session user, which outputs the row correctly, however I want to insert a small image to be displayed alongside of the echo'd row, and cannot figure out the proper syntax.
if ($row['lifetime']!="")
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div> ".$row['lifetime'];
else
echo '';
?>
basically I want the image to appear right before or after the .$row appears, either or.
You can try:
<?php
if ($row['lifetime'] !== "") {
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div>";
echo $row['lifetime'];
echo "<img src='' alt='' style='width:100px'/>";
}
?>
Just put the HTML for the image into the string you're echoing:
echo "<div style ='font:12px Arial;color:#2F6054'><img src="fill in URL here"> Lifetime Member: </div> ".$row['lifetime'];
You can try as below example
HTML
<table>
<thead>
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Photo</th>
<th ></th>
</tr>
</thead>
<tbody>
<?php
$tst=0;
$result = mysql_query("select * from acc_cust");
while($row = mysql_fetch_array($result))
{
echo "<tr class='odd gradeX'>";
echo "<td width=5%'>" . $row['ent_no']. "</td>";
echo "<td>" . $row['cust_name']. "</td>";
echo "<td><img src='[path]" . $row['cust_img'] . "' /></td>";
}
?>
</tbody>
</table>
here is my code. actually i am displaying some data from mysql on the page and creating dynamic link.i want started a session with session_start() in the very begining of code before starting any code. i want to store the value of the link that is to be display on other pagepage..
page1.php
<a style="color:#F00; font-family:Arial, Helvetica, sans-serif; margin-left:33px; font-weight:bold">
No. of registered students:
</a>
<table border='1' align="center" style="font-size:14px" width="95%" cellspacing="3" class="db_table">
<tr class="db_table_tr" >
<th class="db_table_th" name="submit">USN</th>
</tr>
<?php
include('includes/login_connection.php');
$query = "select p.usn, p.name from personal_details p, course_codes c where p.usn = c.usn order by p.usn";
$run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$num = mysql_numrows($run);
echo $num;
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['usn'] . "" . "</td>";
echo "<td>" . $row['name'] . " </td>";
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
}
echo "</tr>";
mysql_close($bd);
?>
</table>
page2.php
<?php
session_start();
if(isset($_SESSION['session_usn']))
{
$_POST['usn'] = $_SESSION['session_usn'];
echo $_POST['usn'];
}
?>
You need to provide a fall-back, in case the URL provided does not contain the proper variables in the $_GET section.
You have:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
You should do something else if $_GET['submit'] isn't set:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
} else {
$_SESSION['session_usn'] = "unset";
// or set a warning flag like "unset"
}
You should be feeding your php file a url like:
http://yoururl.com/page1.php?usn='333'
Where 333 is the value you want to store.
I am new to php.
I made a member registration on login page and adm too. So inside admin I wanted to get the list of the members and delete the members I dont want. So I took the a code from a sample code for phone book from http://localhost/xamp and editted it to my requirement I am able to retrieve the members but unable to delete the members. See the code below:
<?php
require_once('auth.php');
require_once('../config.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
?>
<html>
<head>
<meta name="author" content="Kai Oswald Seidler">
<link href="../loginmodule.css" rel="stylesheet" type="text/css">
<title></title>
</head>
<body>
<p>
<h2><?php echo "User list"; ?></h2>
<table border="0" cellpadding="0" cellspacing="0">
<tr bgcolor="#f87820">
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib1']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib2']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="150" height="6"><br><b><?php echo $TEXT['phonebook-attrib3']; ?></b></td>
<td class="tabhead"><img src="img/blank.gif" alt="" width="50" height="6"><br><b><?php echo $TEXT['phonebook-attrib4']; ?></b></td>
<td><img src="img/blank.gif" alt="" width="10" height="25"></td>
</tr>
<?php
$firstname=$_REQUEST['firstname'];
$lastname=$_REQUEST['lastname'];
$phone=$_REQUEST['phone'];
if($_REQUEST['action']=="del")
{
$result=mysql_query("DELETE FROM members WHERE member_id={$_REQUEST['member_id']}");
}
$result=mysql_query("SELECT member_id,firstname,lastname,login FROM members ORDER BY lastname");
$i = 0;
while($row = mysql_fetch_array($result)) {
if ($i > 0) {
echo "<tr valign='bottom'>";
echo "<td bgcolor='#ffffff' height='1' style='background-image:url(img/strichel.gif)' colspan='6'></td>";
echo "</tr>";
}
echo "<tr valign='middle'>";
echo "<td class='tabval'><img src='img/blank.gif' alt='' width='10' height='20'></td>";
echo "<td class='tabval'><b>".$row['lastname']."</b></td>";
echo "<td class='tabval'>".$row['firstname']." </td>";
echo "<td class='tabval'>".$row['member_id']." </td>";
echo "<td class='tabval'><a onclick=\"return confirm('".$TEXT['userlist-sure']."');\" href='userlist.php?action=del&member_1d=".$row['member_id']."'><span class='red'>[".$TEXT['userlist-button1']."]</span></a></td>";
echo "<td class='tabval'></td>";
echo "</tr>";
$i++;
}
echo "<tr valign='bottom'>";
echo "<td bgcolor='#fb7922' colspan='6'><img src='img/blank.gif' alt='' width='1' height='8'></td>";
echo "</tr>";
?>
</table>
</body>
</html>
I haven't editted it that properly and the looks in all.
Please help me in making it able to delete the members also.
I didn't understand what .$TEXT['userlist-button1'].,'".$TEXT['userlist-sure']. variables are?
I also want to include an approved and disapproved radio button in table for each members.
How can I do that?
Please if you can help me.
This should be a POST via a FORM not a href link (GET).
$TEXT is obviously an array holding the text you want printed.
You need to replace &member_1d in the href with a real & and a real i as &member_id.
$TEXT is an array contaning all the language strings for the selected language.
You find the strings defined unter /lang/yourlanguage.php
In general this is not a very good example to start coding with IMO.
But I think your app may start working, if you make sure, the language files and other include files are available and you change this &member_1d with &member_id
An example of a list of members with delete links:
$query = mysql_query("SELECT member_id,firstname,lastname,login FROM members ORDER BY lastname");
if(mysql_num_row($query)!= 0){ //only continue if there are members in the database
while($row = mysql_fetch_assoc($query)){ //loop through each row in the database
$member_id = $row['member_id'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
echo '<p>' . $firstname . ' - ' delete '</p>';
}
}
A simple script on delete_member.php to delete the member from the database.
if(isset($_GET['id'])){
$member_id = $_GET['id'];
$query = mysql_query("DELETE FROM members WHERE member_id='$member_id'");
echo '<p>This user was deleted from database</p>';
}
This code is only basic to give an example.
I would however prefer to use a simple form and $_POST for something like this instead of using $_GET which is very vulnerable in this kind of instance.
After getting the list of members use a form with input field to type the id you want to delete.