Create a user's link - php

I have Created a website that have a login and signup form,
now what I need is to create a profile link for everyone who signup in PHP, Such as in twitter and Facebook.
How do I create a profile link for each user in PHP?

if login is successfull then store user id in session variable and excute the query that will get the detail of the user from database and show the result... as simple as that and on any page if you want to get details of user use session variable and execute the query and display the result or you can do another thing as after login store user information in session and display details using sessions...i think this will help you if not fell free to ask again
Sample code
<?php
require_once('connection.php');
$id=$_SESSION['SESS_MEMBER_ID'];
$result3 = mysql_query("SELECT * FROM member where mem_id='$id'");
while($row3 = mysql_fetch_array($result3))
{
$fname=$row3['fname'];
$lname=$row3['lname'];
$address=$row3['address'];
$contact=$row3['contact'];
$picture=$row3['picture'];
$gender=$row3['gender'];
}
?>
<table width="398" border="0" align="center" cellpadding="0">
<tr>
<td height="26" colspan="2">Your Profile Information </td>
<td><div align="right">logout</div></td>
</tr>
<tr>
<td width="129" rowspan="5"><img src="<?php echo $picture ?>" width="129" height="129" alt="no image found"/></td>
<td width="82" valign="top"><div align="left">FirstName:</div></td>
<td width="165" valign="top"><?php echo $fname ?></td>
</tr>
<tr>
<td valign="top"><div align="left">LastName:</div></td>
<td valign="top"><?php echo $lname ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Gender:</div></td>
<td valign="top"><?php echo $gender ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Address:</div></td>
<td valign="top"><?php echo $address ?></td>
</tr>
<tr>
<td valign="top"><div align="left">Contact No.: </div></td>
<td valign="top"><?php echo $contact ?></td>
</tr>
</table>
<p align="center"></p>

Assuming you have a user id in your database for each user you could do something like this.
User Profile
Then get the id on user_profile.php using $_GET['id']. Once you have this query the database on id and do what you want with the data.

Related

Admin Cannot Update Student Profile

currently im doing my final year project which is advising system for student. My problem is that admin cannot update a certain data of student. At my first try, it says that, admin done update student profile but when i check at the database it does not update at all. After that i change my code, it turns that the data is undefined variable and mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given.
(AdminUpdateStd.php)
<?php
include("db.php");
$StdId = $_GET['id'];
$sql="SELECT * from member where id='$StdId'";
$result=mysqli_query($db,$sql);
while($rows=mysqli_fetch_array($result)){
$student_id=$rows[0];
$student_name=$rows[3];
$student_email=$rows[5];
$student_address=$rows[4];
$student_tel=$rows[6];
$intake=$rows[7];
$total_cred=$rows[8];
$advisor=$rows[9];
}
?>
<form action ="AdminUpdateProcess.php" method="POST">
<div id="reg-head" class="headrg" align="center"><strong>Profile Student <?php echo "$StdId"; ?></strong></div>
<table width="350" height="200" border="1" align="center" cellpadding="2" cellspacing="0" bgcolor="#FFF">
<input type="hidden" name="student_id" value=" <?php echo "$student_id"; ?>">
<tr id="lg-1">
<td class="tl-1"><div align="left" id="tb-name">Full Name:</div></td>
<td class="tl-4"><?php echo "$student_name"; ?></td>
</tr>
<tr id="lg-1">
<td class="tl-1"><div align="left" id="tb-name">Email:</div></td>
<td class="tl-4"><?php echo "$student_email"; ?></td>
</tr>
<tr id="lg-1">
<td class="tl-1"><div align="left" id="tb-name">Adress:</div></td>
<td class="tl-4"><?php echo "$student_address"; ?></td>
</tr>
<tr id="lg-1">
<td class="tl-1"><div align="left" id="tb-name">Tel Number:</div></td>
<td class="tl-4"><?php echo "$student_tel"; ?></td>
</tr>
<tr id="lg-1">
<td class="tl-1"><div align="left" id="tb-name">Intake:</div></td>
<td class="tl-4"><input type="text" name="intake" <?php echo "$intake"; ?>></td>
</tr>
<tr id="lg-1">
<td class="tl-1"><div align="left" id="tb-name">Total Credit Hour:</div></td>
<td class="tl-4"><input type="text" name="total_credit" <?php echo "$total_cred"; ?>></td>
</tr>
<tr id="lg-1">
<td class="tl-1"><div align="left" id="tb-name">Advisor:</div></td>
<td class="tl-4"><input type="text" name="advisor_name" <?php echo "$advisor"; ?>></td>
</tr>
<tr id="lg-1">
<td class="tl-1"><div align="left" id="tb-name"> </div></td>
<td class="tl-4"><input type="submit" name="Update" <?php echo "value='UPDATE'"; ?>></a>
<button type="button">View Profile</button></td>
</tr>
</table>
</form>
(AdminUpdateProcess.php)
<?php
include("db.php");
$intake=$_POST['intake'];
$total_cred=$_POST['total_credit'];
$advisor=$_POST['advisor_name'];
$StdId = $_POST['student_id'];
if (isset ($_POST['Update'])) {
$UpdateQuery ="UPDATE member SET intake = '$intake', total_credit = '$total_cred', advisor_name = '$advisor' WHERE student_id ='$StdId'";
$res = mysqli_query ($UpdateQuery) or die ("Could not update".mysqli_error());
}
?>

Displaying table from MySQL and link to delete each row from database

I've stucked a bit when it comes to a small piece of my PHP code. The role of this script is to display whole table from mysql plus adding to each row a hyperlink to delete such row.
<?php
$connection=mysql_connect('localhost','root','') or die(mysql_error());
error
mysql_select_db('localhost_db',$connection) or die(mysql_error());
$query=mysql_query("SELECT * FROM cms_contest") or die(mysql_error());
if(mysql_num_rows($query)>0):
?>
<table width="100%" border="0">
<tr style="font-weight:bold;">
<td align="center">Id</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Email</td>
<td align="center">Phone</td>
<td align="center">Answer</td>
<td align="center">Remove</td>
</tr>
<?php
while($row=mysql_fetch_object($query)):?>
<tr>
<td align="center"><?php echo $row->ID; //row id ?></td>
<td align="center"><?php echo $row->name; // row first name ?></td>
<td align="center"><?php echo $row->surname; //row las tname ?></td>
<td align="center"><?php echo $row->email; //row created time ?></td>
<td align="center"><?php echo $row->phone_number; //row created time ?></td>
<td align="center"><?php echo $row->contest_answer; //row created time ?></td>
<td align="center">REMOVE</td>
</tr>
<?php endwhile;?>
</table>
<?php
else: ?>
<h3>No Results found.</h3>
<?php endif; ?>
It would be perfect if there appear a short javascript popup with question like: are you sure you want to remove this entry? OK / Cancel.
I have no clue how to do it.. thanks for any tips!
Simple/stupid method:
<td>nuke me</td>
But if a web spider or a browser link pre-fetch tool gets loose on this page, you'll be nuking ALL of your records.
Somewhat better:
<td>
<form method="post" action="deleteme.php">
<input type="hidden" name="id" value="<?php echo $row->ID ?>" />
<input type="submit" value="Nuke me" />
</form></td>
And then there's various options involving radio buttons/checkboxes, or JS to trap the click-on-the-link etc...
But, in the end, they ALL boil down to "you have to sent the ID of the row back to the server". How you go about that is up to you... just don't use the plain "click here" version.
Indeed there are several different ways to do this. One way I like is to wrap the entire table in a form that submits to the delete script, and use a button for each row with the row ID as its value.
<form method="post" action="delete.php">
<table width="100%" border="0">
<tr style="font-weight:bold;">
<td align="center">Id</td>
<td align="center">First Name</td>
<td align="center">Last Name</td>
<td align="center">Email</td>
<td align="center">Phone</td>
<td align="center">Answer</td>
<td align="center">Remove</td>
</tr>
<?php while($row=mysql_fetch_object($query)): ?>
<tr>
<td align="center"><?php echo $row->ID; //row id ?></td>
<td align="center"><?php echo $row->name; // row first name ?></td>
<td align="center"><?php echo $row->surname; //row las tname ?></td>
<td align="center"><?php echo $row->email; //row created time ?></td>
<td align="center"><?php echo $row->phone_number; //row created time ?></td>
<td align="center"><?php echo $row->contest_answer; //row created time ?></td>
<td align="center">
<button name="delete-id" type="submit" value="<?php echo $row->ID; ?>">
REMOVE
</button>
</td>
</tr>
<?php endwhile;?>
</table>
</form>
There are also many different ways to confirm the deletion. The simplest I know of on the client side is to add onclick="return confirm('Are you sure?');" to the delete button.
click event:
DELETE
<script type="text(javascript">
function confirmDelete(id){
if(confirm('sure u want to delete entry with id: ' + id + '?')){
window.location.href = "ursite.php?id="+id+"&delete=true";
}
}
</script>
PHP
if(isset($_GET['delete'])){
$stmt = "DELETE FROM cms_contest WHERE ID = ".$_GET['id'];
mysql_query($stmt);
}
it's the worst way to do it.
I recommend you to look at:
jQuery $.post()jQuery $.ajax()
I hope it will help you.

php code for displaying relational tables

This code is running, this is the member page after a member login with their account number and password this page will show with correspondent details about their account.
Now I want to add the fields from another table here.
My first table is "member" then I created another which is "account" I put relation to the mem_id field of both table in my database in phpmyadmin.
Now my problem is how to display the relation table in the this page. when a member login with their account number automatically both field in the relational table will display here.
<?php
require_once('connection.php');
$id=$_SESSION['SESS_MEMBER_ID'];
$result3 = mysql_query("SELECT * FROM member where mem_id='$id'");
while($row3 = mysql_fetch_array($result3))
{
$fname=$row3['fname'];
$lname=$row3['lname'];
$address=$row3['address'];
$contact=$row3['contact'];
$picture=$row3['picture'];
$gender=$row3['gender'];
}
?>
</h2>
</span>
<table width="597" border="0" cellpadding="0">
<tr>
<td height="26" colspan="2" class="style11"></td>
</tr>
<tr>
<td width="166" rowspan="5" class="style11"><div align="left"><img src="style/LOGO
GRAY.jpg" alt="no image found"" width="129" height="129" border="1" "<?php echo
$picture ?>/></div></td>
<td width="126" valign="top" class="style11"><div align="left">First Name:</div>
</td>
<td width="297" valign="top" class="style11"><span class="style16"><?php echo
$fname ?></span></td>
</tr>
<tr>
<td valign="top" class="style11"><div align="left">Last Name:</div></td>
<td valign="top" class="style11"><span class="style16"><?php echo $lname ?></span>
</td>
</tr>
<tr>
<td valign="top" class="style11"><div align="left">Gender:</div></td>
<td valign="top" class="style11"><span class="style16"><?php echo $gender ?></span>
</td>
</tr>
<tr>
<td valign="top" class="style11"><div align="left">Address:</div></td>
<td valign="top" class="style11"><span class="style16"><?php echo $address ?>
</span></td>
</tr>
<tr>
<td height="42" valign="top" class="style11"><div align="left">Contact No.: </div>
</td>
<td valign="top" class="style11"><span class="style16"><?php echo $contact ?>
</span></td>
</tr>
</table>
<!--<?php echo $power ?> -->
Thank you in advance
You need to LEFT JOIN the other table. Try this query:
SELECT mem.*, det.* FROM members AS mem LEFT JOIN details AS det ON details.mem_id = members.mem_id WHERE mem_id = {$id};
Select which fields you want to retrieve, using their prefixes you specify later.
Select the base table, giving its fields a prefix.
Perform the join, specifying the other table the same way.
Specify the relationship between fields and how the other table should be joined. In this case it's where the member IDs are the same.
Profit. :)
When you loop through the resulting data, you can reference the new fields just like any other field, because the data is joined together.
Here's a simple tutorial I found on Google and the technical reference. Your best bet is to play with it yourself. That way you'll learn along the way. :)
You have to use LEFT join in order to display the relevant data of the member.
Hope following tutorial will help you to understand the basics.
http://www.tutorialspoint.com/mysql/mysql-using-joins.htm

How would I make the most recently posted in topic in a forum show up first?

I am making a forum from one I borrowed online. I would like the main_forum page to show the most recently posted in topic first, and then the second most recently, etc. My code for the main_forum.php is this:
<?php
require_once 'includes/overall/header.php';
$sql="SELECT * FROM `forum_question` ORDER BY id DESC";
// OREDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<h1>Forum</h1>
<table width=700 class="outer">
<tr>
<td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td>
<td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td>
</tr>
<?php
// Start looping table row
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $rows['topic']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['view']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['reply']; ?></td>
<td align="center" bgcolor="#FFFFFF"><?php echo $rows['datetime']; ?></td>
</tr>
<?php
// Exit looping and close connection
}
mysql_close();
?>
<tr>
<td colspan="5" align="right" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
</table>
<?php
require_once 'includes/overall/footer.php';
ob_end_flush();
?>
Change your query to retrieve your posts in the order you want them. Taking a guess based on your post I would say your query should look more like:
$sql="SELECT * FROM `forum_question` ORDER BY datetime DESC";

Delete record from table using Checkbox [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Deleing Multiple Rows Using Checkboxes, PHP and MySQL
I wonder whether someone may be able to help me please.
I'm trying to put together a script which creates a form which gives the user the ability to delete a record via the selection of a checkbox and then pressing a 'submit' button.
From reading through many articles, I've put together the following script which is the section of code that builds the table, checkboxes and submit button.
<?php
$query = "SELECT l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '$idnum' GROUP BY l.locationname";
$result=mysql_query($query);
$count=mysql_num_rows($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="del" id="del" action="deletelocation.php" method="post">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['locationid']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['locationid']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['locationname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['returnedaddress']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['totalfinds']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input type="submit" value="Delete" /></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
The code below, then deals with the deletion of the record.
<?php
$del_id = $_POST['checkbox'];
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $detectinglocations WHERE locationid='$del_id'";
$result = mysql_query($sql);
}
?>
The correct information is retrieved and shown in the form table, but the problem I'm having is that I'm unable to get the deletion of the record to work. I've run this through JavaScript Console, but unfortunately I don't receive an error message which may help me to solve the problem.
I just wondered whether someone could possibly take a look at this please and let me know where I'm going wrong.
Change your PHP code as below
$del_id = $_POST['checkbox'];
$detectinglocations = 'your database table name';
foreach($del_id as $value){
$sql = "DELETE FROM ".$detectinglocations." WHERE id='".$value."'";
$result = mysql_query($sql);
}

Categories