Hi i have an address book I added the possibility of add a photo.
This is the function for view Name,Surname,Email and telephone
// Display users
public function display(){
$temp_arr = array();
$res = $this->db->run("SELECT * FROM users ORDER by cognome,nome ");
$count=$this->db->rowCount();
while($row = $this->db->fetchArray()) {
$temp_arr[] =$row;
}
return $temp_arr;
}
<?php
$data = $user->display();
$i = 0;
foreach( $data as $eachrecord ) {
$i++;
?>
my idea was to do so how do i implement it?
$sql = "SELECT name FROM upload where id=$id";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
$image = $row['name'];
$image_src = "uploads/".$image;
<td><img src='<?php echo $image_src; ?>' ></td>
to take id use this function
// get id upload
public function getid(){
$db = new mysqli("localhost","root","", "rubrica");
if ($db-> connect_errno) {
exit();
}
$nome = $this->unome;
$cognome = $this->ucognome;
$email = $this->uemail;
$telefono = $this->utel;
$query = $db->query("SELECT id FROM users WHERE nome='$nome' and cognome='$cognome' and email='$email' and telefono='$telefono'");
$id = $query->fetch_assoc()['id'];
$db->close();
return $id;
}
thank you in advance who will help me!
Related
I have a showProduct.php file from where i want to call a function showProduct() in another file. In showProduct() i want to extract all rows from database and to showProduct.php file. the issue is that when i return the array only last row is showing. I want to show all the rows.
The showProduct.php is:
<?php
require_once '../includes/DbOperations.php';
$response = array();
$result = array();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$db = new DbOperations();
$result = $db->showProduct();
if(!empty($result))
{
$response["prod_name"] = $result["prod_name"];
$response["prod_desc"] = $result["prod_desc"];
$response["prod_image"] = $result["prod_image"];
}
else
{
$response["error"] = true;
$response["message"] = "products are not shown";
}
}
echo json_encode($response);
?>
and showProduct() function is:
public function showProduct(){
$menu = array();
$query = mysqli_query($this->con,"SELECT * FROM `products` WHERE 1");
while ($row = mysqli_fetch_array($query)) {
$menu['prod_name'] = $row['prod_name'] ;
$menu['prod_desc'] = $row['prod_desc'] ;
$menu['prod_image'] = $row['prod_image'];
}
return $menu;
}
In your function, you are just overwriting the last data each time, you need to build this data up. Create an array with the new data and use $menu[] to add this new data to the list of menus...
public function showProduct(){
$menu = array();
$query = mysqli_query($this->con,"SELECT * FROM `products` WHERE 1");
while ($row = mysqli_fetch_array($query)) {
$newMenu = []; // Clear array to ensure no details left over
$newMenu['prod_name'] = $row['prod_name'] ;
$newMenu['prod_desc'] = $row['prod_desc'] ;
$newMenu['prod_image'] = $row['prod_image'];
$menu[] = $newMenu;
}
return $menu;
}
Firstly I got the workers name from BIRTHDAYS and then want to get e-mail address from USERS.There is no problem to take workers name's from Table1 but when I try to get the e-mail addresses the db returns me NULL.My DB is mssql.
<?php
include_once("connect.php");
$today = '05.07';
$today1 = $today . "%";
$sql = "SELECT NAME FROM BIRTHDAYS WHERE BIRTH LIKE '$today1' ";
$stmt = sqlsrv_query($conn,$sql);
if($stmt == false){
echo "failed";
}else{
$dizi = array();
while($rows = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC))
{
$dizi[] = array('NAME' =>$rows['NAME']);
$newarray = json_encode($dizi,JSON_UNESCAPED_UNICODE);
}
}
foreach(json_decode($newarray) as $nameObj)
{
$nameArr = (array) $nameObj;
$names = reset($nameArr);
mb_convert_case($names, MB_CASE_UPPER, 'UTF-8');
echo $sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
echo "<br>";
$stmt2 = sqlsrv_query($conn,$sql2);
if($stmt2 == false)
{
echo "failed";
}
else
{
$dizi2 = array();
while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
{
$dizi1[] = array('EMAIL' =>$rows['EMAIL']);
echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
}
}
}
?>
while($rows1 = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC))
{
$dizi1[] = array('EMAIL' =>$rows['EMAIL']);
echo $newarray1 = json_encode($dizi1,JSON_UNESCAPED_UNICODE);
}
you put in $rows1 and would take it from $rows NULL is correct answer :)
take $rows1['EMAIL'] and it would work
and why foreach =?
you can put the statement in while-loop like this:
while ($rows = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$names = $rows['NAME'];
$sql2 = "SELECT EMAIL FROM USERS WHERE NAME = '$names' ";
echo "<br>";
$stmt2 = sqlsrv_query($conn, $sql2);
if ($stmt2 == false) {
echo "failed";
} else {
$dizi2 = array();
while ($rows1 = sqlsrv_fetch_array($stmt2, SQLSRV_FETCH_ASSOC)) {
$dizi1[] = array('EMAIL' => $rows1['EMAIL']);
echo $newarray1 = json_encode($dizi1, JSON_UNESCAPED_UNICODE);
}
}
}
I'm trying to fetch couple of single data in my server database but this is throwing some errors. The incoming data is correct. The search function just don't get completed.
Here's the code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxx');
define('PASS','xxxxxxxxx');
define('DB','xxxxxxxxxx');
$con = mysqli_connect(HOST,USER,PASS,DB);
$post_id = $_POST['id'];
$buyer_mobile = $_POST['mobile'];
$buyer_name = $_POST['name'];
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysqli_query($con,$sql);
$owner_mobile = $row['mobile'];
$sql = "select name from user where mobile='$owner_mobile'";
$r = mysqli_query($con,$sql);
$owner_name = $row['name'];
$sql = "INSERT INTO flat_booking (post_id,owner_mobile,owner_name,buyer_mobile,buyer_name) VALUES ('$post_id','$owner_mobile','$owner_name','$buyer_mobile','$buyer_name')";
if(mysqli_query($con,$sql)){
echo "Success";
}
else{
echo "error";
}
mysqli_close($con);
}else{
echo 'error1';
}
What am I doing wrong here? Maybe this:
$owner_mobile = $row['mobile'];
Thanks in advance!
create table flatower and add mobile column
$post_id = 1;
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysql_query($con,$sql);
$row = mysql_fetch_array($res);
$owner_mobile = $row[0]['mobile'];
Your problem is this line:
$owner_mobile = $row['mobile'];
You have not created the $row variable. For this you would need to do something such as:
Do this first:
<?php
$row = array();
while ($result = mysqli_fetch_assoc($res))
{
$row[] = $result;
}
?>
This allows you to do this:
<?php
foreach ($row as $r)
{
var_dump($r); print "<br />"; // One row from the DB per var dump
}
?>
I want show my message system user Profile name(If have) else show user name.
In my every code I used as below, which work well.
if (empty($pname)) $pname = $username;
But in below I cannot understand how to Return 'profile name else user name' in my "function getusername($userid)".
Here if I use return $row[0] at my "function getusername" Its show username, But I want to show Profile name and If profile name empty then show user name.
Get profile name/user name code:
function getusername($userid) {
$sql = "SELECT username,pname FROM users WHERE `id` = '".$userid."' LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
$row = mysql_fetch_array($result);
$username = $row['username'];
$pname = $row['pname'];
if (empty($pname)) $pname = $username;
// Now here return $row[0] show only username But How to return pname else username?
return $row[0];
} else {
return "Unknown";
}
}
This code fetch a specific message
function getmessage($message) {
$sql = "SELECT * FROM mail WHERE `id` = '".$message."' && (`from` = '".$this->userid."' || `to` = '".$this->userid."') LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
// reset the array
$this->messages = array();
$row = mysql_fetch_assoc($result);
$this->messages[0]['id'] = $row['id'];
$this->messages[0]['title'] = $row['title'];
$this->messages[0]['message'] = $row['message'];
$this->messages[0]['from'] = $this->getusername($row['from']);
$this->messages[0]['to'] = $this->getusername($row['to']);
} else {
return false;
}
}
Use this
function getusername($userid) {
$sql = "SELECT username,pname FROM users WHERE `id` = '".$userid."' LIMIT 1";
$result = mysql_query($sql);
if(mysql_num_rows($result)) {
$row = mysql_fetch_array($result);
$username = $row['username'];
$pname = $row['pname'];
if (empty($pname)) $pname = $username;
// Now here return $row[0] show only username But How to return pname else username?
return $pname;
} else {
return "Unknown";
}
}
The problem is with how you are returning the value.
Change:
if (empty($pname)) $pname = $username;
// Now here return $row[0] show only username But How to return pname else username?
return $row[0];
To:
if(empty($pname)) return $username;
else return $pname;
Also, it is suggested you use mysqli instead of mysql.
Can't you use something like this?
return isset($pname) ? $pname : $username;
This basically is: if $pname is set, then return $pname, else return $username
I need help with this code, i need it to add an array in Gangs
under members and seperate them with -
I need every member in the Gang to be listed under Members and separated with -
so i can explode them below.
This is the line that adds the members to gangs but its without - and i think it erases members that are already there.
$result = mysql_query("UPDATE Gangs SET members='".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());
full code
if(isset($_POST['creategang'])){
if(empty($_POST['gang_name'])){
echo "Enter a Gang Name.";
} else {
if (strlen($_POST['gang_name']) > "20"){
echo "The username may not consist out of more then 20 characters.";
}else{
if (ereg('[^A-Za-z0-9]', $_POST['gang_name'])) {
echo "Invalid Name only A-Z,a-z and 0-9 is allowed.";
}else{
$sql = "SELECT name FROM Gangs WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$m_count = mysql_num_rows($query);
if($m_count >= "1"){
echo 'This name has already been used.!';
}else{
$sql = "INSERT INTO Gangs SET name = '".$_POST['gang_name']."' , owner= '$name'";
$res = mysql_query($sql);
$result = mysql_query("UPDATE users SET gang='".mysql_real_escape_string($_POST['gang_name'])."' WHERE id='" .mysql_real_escape_string($_SESSION['user_id']). "'")
or die(mysql_error());
$result = mysql_query("UPDATE Gangs SET members='".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());
echo 'Gang successfully created!';
}
}
}
}
}
?>
this is the code i will use to separate the array
$Gang_array = explode("-", $Gang_members);
Thanks for viewing my question and thanks in advance for helping me
new code to add to gang
<? include_once("connect.php"); ?>
<?
if(isset($_SESSION['user_id'])) {
// Login OK, update last active
$sql = "UPDATE users SET lastactive=NOW() WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
mysql_query($sql);
}else{
header("Location: index.php");
exit();
}
$sql = "SELECT * FROM users WHERE id='".mysql_real_escape_string($_SESSION['user_id'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$id = htmlspecialchars($row->id);
$userip = htmlspecialchars($row->userip);
$name = htmlspecialchars($row->name);
$sitestate = htmlspecialchars($row->sitestate);
$password = htmlspecialchars($row->password);
$mail = htmlspecialchars($row->mail);
$money = htmlspecialchars($row->money);
$exp = htmlspecialchars($row->exp);
$rank = htmlspecialchars($row->rank);
$health = htmlspecialchars($row->health);
$points = htmlspecialchars($row->points);
$profile = htmlspecialchars($row->profile);
$gang = htmlspecialchars($row->gang);
?>
<?php
$sql = "SELECT * FROM Gangs WHERE name='".mysql_real_escape_string($_GET['name'])."'";
$query = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_object($query);
$Gang_name = htmlspecialchars($row->name);
$Gang_owner = htmlspecialchars($row->owner);
$Gang_money = htmlspecialchars($row->money);
$Gang_exp = htmlspecialchars($row->exp);
$Gang_level = htmlspecialchars($row->level);
$Gang_members = htmlspecialchars($row->members);
$Gang_array = explode("-", $Gang_members);
$Gang_profile = htmlspecialchars($row->profile);
?>
<div id="content" class="profile">
<h2>Gang Profile</h2>
<form method="post" >
<input type="submit" name="Petition" id="Petition" value="Petition">
</form>
<center>
<h1><?php echo $Gang_name; ?></h1>
Owner: <?php echo $Gang_owner; ?><br>
Gang Cash: $<?php echo $Gang_money; ?><br>
Gang Exp: <?php echo $Gang_exp; ?><br>
Gang Level: <?php echo $Gang_level; ?><br>
Gang Members: <?php echo $Gang_array; ?><br>
</center><br>
<p>Gang Quote</p>
<div id="UserText">
<?php
$Gang_profile = htmlentities($Gang_profile);
$Gang_profile = nl2br($Gang_profile);
$Gang_profile = stripslashes($Gang_profile);
echo $Gang_profile; ?>
</div>
</div>
<?
if (isset($_POST['Petition'])) {
$result = mysql_query("SELECT members FROM Gangs
WHERE name='".$Gang_name."'");
if ($result) {
while($row = mysql_fetch_assoc($result)) {
$members = $row['members'];
}
}
if ($members != '') $members .= '-'.$name;
else $members = $name;
$result = mysql_query("UPDATE Gangs SET members='".$members."' WHERE name='".$Gang_name."'");
}
If I understand your question correctly. You can try this :
Get the members first :
$result = mysql_query("SELECT members FROM Gangs
WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'");
if ($result) {
while($row = mysql_fetch_assoc($result)) {
$members = $row['members'];
}
}
Then add new member and do update :
//This is to check whether $name is already in the gangs
if (strpos($members,$name) !== false) {
if ($members != '') $members .= '-'.$name;
else $members = $name;
//Update to gangs
$result = mysql_query("UPDATE Gangs SET members='".$members."' WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'");
}
else sprintf("%s is in the gangs already",$name);
Hope it helps.
Pseudo code, but this should probably work:
$result = mysql_query("UPDATE Gangs SET members = members + '-' + '".$name."'WHERE name='".mysql_real_escape_string($_POST['gang_name'])."'")
or die(mysql_error());