Mailbox in website - php

I have a "Contact us" form in my website with the mailto: function. The mail goes to my yahoo account. I want to implement a mailbox in my website such that i can receive those mail directly from the admin page and reply or delete them accordingly.
How I can do that?
I already have the template I need an explanation on the functions and dynamic content to be implemented.

This is the sample of my code .. I have change some variable name here ..
$sql = "SELECT * FROM request where user_id='$user'";
$result = $conn->query($sql);
$sr=1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$sender_id=$row["sender_id"];
$receiver_id=$row["receiver_id"];
$req=$row['req_id'];
$stat=$row['status'];
if($stat==0){
$status="Pending...";
}
elseif ($stat==1) {
$status="Accepted.";
}
elseif ($stat==2) {
$status="Rejected.";
}
$sql1 = "SELECT * FROM usertable1 where id='$sender_id' ";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$username=$row1['user_name'];
$email=$row1['email'];
}
}
$sql2 = "SELECT * FROM usertable2 where id='$receiver_id' ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
$source=$row2['source'];
$destination=$row2['destination'];
}
}
you will get an idea from this so you can implement as per your requirement.

Related

PHP foreach echo prints “Symbols” as value

I fetched data dynamically from from MySQL using a drop down menu through Ajax which was successful but when I echo the array values, instead of giving me the list of emails it was showing just symbols.
Take a look at the image below:
From the Email List, those are the symbols that were echo out.
here is my php code
if(isset($_POST["confirm_no"])){
$d = $_POST['confirm_no'];
$query = mysqli_query($mysqli, "select * from jobseeker WHERE confirm_no LIKE '$d%'");
//Display list
if(mysqli_num_rows($query) > 0){
$row = mysqli_fetch_array($query);
foreach ($row as $r) {
$emailArr[] = $r["mails"];
}
$emails = implode(";", $emailArr);
echo $emails;
}else{
echo 'No email for this selection.';
}
}
And the jQuery
$(document).ready(function(){
$('#smode').change(function(){
var confirm_no = $(this).val();
$.ajax({
type:'POST',
data:"confirm_no="+confirm_no,
url:'get_email.php',
success:function(data){
$('#emaillist').val(data);
}
});
});
});
Why is it echoing out this symbols?
Rebuild your code as:
//Display list
if(mysqli_num_rows($query) > 0){
// You have many results - fetch them all iteratively
// use `fetch_assoc` to have ability to use `mails`
while ($row = mysqli_fetch_assoc($query)) {
$emailArr[] = $row["mails"];
}
$emails = implode(";", $emailArr);
echo $emails;
}else{
echo 'No email for this selection.';
}
You are fetching the data incorrectly, you only fetch the one row (the call to mysqli_fetch_array()
$row = mysqli_fetch_array($query);
foreach ($row as $r) {
$emailArr[] = $r["mails"];
}
Could be better written as
while( $row = mysqli_fetch_assoc($query)) {
$emailArr[] = $row["mails"];
}
Or...
$emailArr = mysqli_fetch_all($query, MYSQLI_ASSOC);
$emailArr = array_column($emailArr, "mails");

how to check whether a value is present in database or not in php

i know this is simple..but i am unable to find the error..i have a login page where i take the input from user i.e. the username and password.then another page i am checking whether the values are present in the database or not.but neither it is giving me an error nor its working..moreover its not entering the first if condition..if ($result->num_rows > 0)
<body style="background-color:lightgrey;">
<?php
include('custdb.php');
session_start();
$uname=$_POST['username'];
$pass=$_POST['password'];
$sql = "SELECT * FROM `info` WHERE `username`='".$uname."';";
//echo $sql;
$result = $conn->query($sql);
echo"1";
echo $result;
if ($result->num_rows > 0)
{
echo"1";
while($row = $result->fetch_assoc())
{
if($uname==$row["username"])
{
header("location:custprofile.php");
}
else
{
header("Location:custindex.php");
}
}
}
else
{
echo "invalid input";
echo '<h4 align="left">LOGIN </h4>';
}
?>
Try to change this portion of your code,
$uname=$_POST['username'];
$pass=$_POST['password'];
$sql = "SELECT * FROM `info` WHERE username='".$uname."' AND password='".$pass."'";

Passing php variables through pages / sql

i have the following information displayed
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0)
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
?>
as you can see i have created a game_details page which will display that specific Game_id when the image is clicked
im having trouble understanding how to pull the data out from that game_id in sql on the other page.
here is my attempt on the game_details page
<?php
if (!isset($_GET['$game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id={$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['$game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
This attempt is giving me the "invalid category ID" error
Would appreciate help
There are a few issues with your code.
Let's start from the top.
['$game_id'] you need to remove the dollar sign from it in $_GET['$game_id']
Then, $row['$game_id'] same thing; remove the dollar sign.
Then, game_id={$game_id}' will throw a syntax error.
In your first body of code; you should also use proper bracing for all your conditional statements.
This one has none if (mysqli_num_rows($result) > 0) and will cause potential havoc.
Rewrites:
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0){
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
}
?>
Sidenote for WHERE game_id='{$game_id}' in below. If that doesn't work, remove the quotes from it.
WHERE game_id={$game_id}
2nd body:
<?php
if (!isset($_GET['game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id='{$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
Use error checking tools at your disposal during testing:
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.error-reporting.php
You want to be using $_GET['gameid'] as that's the parameter you passed.
You are calling for game_id when the link to go to game_details.php has the variable gameid. Either change the parameter in the link to game_id or call for gameid in your $_GET['$game_id'].
Also, as Fred -ii- said, take out the dollar sign in $_GET['$game_id']

URL friendly from DB, php and .htaccess

can help me?
I've a site url like:
http://www.example.com/episodio/ID
ID = row number from Database.
I want to display the page title.
I've this:
.htaccess file:
RewriteEngine on
RewriteRule ^episodio/(\w+)/?$ episodio.php?id=$1
php file:
$sql = "SELECT * FROM "episodios" WHERE serie = ".$id." AND temporada = ".$i." ORDER BY "episodio" ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row['episodio'] == 0) {
echo '<li><strong>Episodio doble:</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li>';
}else{
echo '<li><strong>Episodio '.$row['episodio'].':</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li>';
}
?>
<?php } ?>
You're using $_GET in your ID anywhere before this code?
I mean, $_GET['id'].
Most PHP versions (since 4...) doesn't catch the var just for its name.
I'd recommend getting all your variables from the DB first, and then printing out the HTML (perhaps from a template file). We assume that $row contains more than just an ID? Perhaps an "título del episodio", also?
if ($result->num_rows > 0) {
$episodios = array();
$x = 0;
while($row = $result->fetch_assoc()) {
$episodios[$x]['id'] = $row['id'];
if ($x == 0) {
$PageTitle = $row['title'];
}
$x++;
}
Then your template can use $PageTitle (from the first episode), and you can loop through the $episodios array to construct the links for the other episodes.

PHP MYSQL Get data from database and echo message if no data retrieved

Im trying to make a code where it displays all names in database where "clanwars" is set to 1 (int) and if all of them is 0 echo an message like: "Noone has signed up yet!"
This is some of the code i have:
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
while($row = mysqli_fetch_array($result))
{
if ($row['clanwars'] != '0') {
echo $row['mantra']."<br>";
} else {
echo 'Noone has signed up yet!';
}
}
mysqli_close($con);
First, in your example row['clanwars'] will never equal to 0 because you already specified WHERE clanwars = 1 in your query, so MySQL will return only those that have clanwars=1. If I understand well, you need to do something like:
<?
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result)==0) echo 'Noone has signed up yet';
else {
while ($row = mysqli_fetch_array($result)) {
//do what you need
}
}
?>
So basically, you retrieve everyone who has CLANWARS set to 1 in the database. If there are records, process them, if there are no records, it means that nobody has signed up.
Is this what you need?
Try this:
$result = mysqli_query($con,"SELECT * FROM users WHERE clanwars = 1 ORDER BY mantra");
if (mysqli_num_rows($result) == 0)
{
echo 'Noone has signed up yet!';
}
else
{
while ($row = mysqli_fetch_array($result))
{
echo $row['mantra']."<br>";
}
}
mysqli_close($con);

Categories