Get user id

0

I would like you to help me I am making a mobile application and I have two tables in my database one called "users" and the other "activities" inside this second table I have a foreign key of the user id, what I want is get the id of the logged in user and insert it in the activities table, every time a user posts an activity

I leave the php code I use to make an insert in the table activities

    <?php

 if($_SERVER['REQUEST_METHOD']=='POST'){

$imagen= $_POST['foto'];
$nombre = $_POST["Titulo"];
$des = $_POST["Descripcion"];
$aut = $_POST["Autor"];
$fec = $_POST["Fecha"];
$hor = $_POST["Hora"];

 require_once('../registro/Conexion_db.php');

 $sql ="SELECT idActividad FROM actividades ORDER BY idActividad DES";

 $res = mysqli_query($con,$sql);

 $id = 0;

 while($row = mysqli_fetch_array($res)){
 $id = $row['idActividad'];
 }

 if($imagen!="no imagen"){
 $path = "uploads/$id.png";
 $actualpath = "https://pitav2.000webhostapp.com/$path";
 $sql = "INSERT INTO actividades (foto,titulo,descripcion,autor,fecha_entrega,hora_entrega) VALUES ('$actualpath','$nombre','$des','$aut','$fec','$hor')";
 }else{
    $actualpath = "sin imagen";
    $sql = "INSERT INTO actividades (foto,titulo,descripcion,autor,fecha_entrega,hora_entrega) VALUES ('$actualpath','$nombre','$des','$aut','$fec','$hor')";
}
 if(mysqli_query($con,$sql)){
 file_put_contents($path,base64_decode($imagen));
 echo "Subio imagen Correctamente";
 }

 mysqli_close($con);
 }else{
 echo "Error";
 }
    
asked by Jenny Gutiérrez 18.09.2018 в 08:22
source

2 answers

2

You have several alternatives to save the id of the connected user. Once the user makes the login, you can save the data of the id in one of these php variables, $GLOBALS, $_COOKIE o $_SESSION .

The most advisable thing would be to use the $_SESSION and you would have to do it in the following way:

$_SESSION['usuario'] = $id

Where variable $id you get from a select when the user logs in.

    
answered by 18.09.2018 в 08:42
0

The first question, does the query work? is that you have BY idActivity DES and it would be DESC, as for your question, when you log in I imagine that before you query your users table, so you can get the user id and save it as Iñigo says in a session variable something like $_SESSION['logged'] = $id , it's that simple, and as a tip and avoid duplicating code and this part

if($imagen!="no imagen"){
   $path = "uploads/$id.png";
   $actualpath = "https://pitav2.000webhostapp.com/$path";
   $sql = "INSERT INTO actividades (foto,titulo,descripcion,autor,fecha_entrega,hora_entrega) VALUES ('$actualpath','$nombre','$des','$aut','$fec','$hor')";
 }else{
$actualpath = "sin imagen";
$sql = "INSERT INTO actividades    (foto,titulo,descripcion,autor,fecha_entrega,hora_entrega) VALUES        ('$actualpath','$nombre','$des','$aut','$fec','$hor')";
 }
 if(mysqli_query($con,$sql)){
     file_put_contents($path,base64_decode($imagen));
     echo "Subio imagen Correctamente";
  }

I would put it like this

$actualpath = $imagen!='' ? 'https://pitav2.000webhostapp.com/'.$path : '';
$sql = "INSERT INTO actividades (foto,titulo,descripcion,autor,fecha_entrega,hora_entrega) VALUES ('$actualpath','$nombre','$des','$aut','$fec','$hor')";
if(mysqli_query($con,$sql)){
     file_put_contents($path,base64_decode($imagen));
      echo "Subio imagen Correctamente";
}

I really do not know what you send in the photo field, I see it is a post and not $ _FILE, but in any case if you do not have a photo change it by empty string unless you want to save the literal in the database. Greetings

    
answered by 18.09.2018 в 09:39