Get image name in mysql for id

Asked

Viewed 804 times

0

I would like to know how to get the image name by user id, I have this comic: inserir a descrição da imagem aqui

I would like to show only the image of the Claudiomiro, and not all that are in the comic... this way: inserir a descrição da imagem aqui

I have this code for the user upload/password/name/image, the image is in the images directory, and only the name goes to the bd...

Index.html

<html>
<head>
</head>
<body>
    <form method="post" action="index.php">
<label>Usuario:</label><input type="text" name="user">
<label>Senha:</label><input type="password" name="pass"><input type="submit" value="Entrar" />
    </form>
    <form method="post" enctype="multipart/form-data" action="cadastrar.php">
        <table>
  <input type="hidden" name="id"/> 
  <tr><td>Nome:</td><td><input type="text"  name="nome" maxlength="50" /></td></tr>
  <tr><td>Usuário:</td><td><input type="text" name="usuario" maxlength="50" /></td></tr>
  <tr><td>Email:</td><td><input type="text" name="email" maxlength="200" /></td></tr>
  <tr><td>Senha:</td><td><input type="password" name="senha" maxlength="50" /></td></tr>
  <tr><td></td><td><input type="password" name="senha2" maxlength="50"  />
  <tr><td>imagem:</td><td><input type="file" size="60" name="arq" value=""></td></tr>
  <tr><td></td><td><input type="submit" size="60" name="enviar" value="Upload"></td></tr>


 </table>
</form>
</body>
</html>

Index.php:

<?php
require_once("config.php");
mysql_connect("$host", "$usuario", "$senha");
mysql_select_db("$db");
  $user = $_POST['user'];
  $pass = $_POST['pass'];
  $resultado = mysql_query("SELECT * FROM notas WHERE id='".$id."'");
$verificacao = "SELECT * FROM users WHERE usuario ='$user' AND senha = '$pass'";
$resultado = mysql_query($verificacao);
$iniciar = mysql_num_rows($resultado);
mysql_select_db("usuarios", mysql_connect('localhost','root','')); 
$linha = mysql_fetch_array($resultado);
if ($iniciar == 1){

  ?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

  <div id="header"><div id="logo">Logo</div>
    <div id="menu">
    <li><a href="perfil.php?id=<?php echo $linha['nome']; ?>"><?php echo $_POST['user'];?></a></li>
    <li><a href="#">Encontre alguém</a></li>
  </div>
  </div>
</body>
</html>
<?php
}else{
   header("Location: index.html");
}
?>

Register.php

<?php
require_once("config.php");
mysql_connect("$host", "$usuario", "$senha");
mysql_select_db("$db");
if ($_POST['enviar']) {
    $tempname = $_FILES['arq']['tmp_name'];
    $new_name = uniqid();
    $extension = strtolower(pathinfo($_FILES['arq']['name'], PATHINFO_EXTENSION));
    $folder = "imagens";
    $name = $_POST['nome'];
    $usuario = $_POST['usuario'];
    $email = $_POST['email'];
    $senha = $_POST['senha'];
    $senha2 = $_POST['senha2'];
    $verifica = "SELECT *FROM users WHERE usuario = '$usuario'";
    $checar = mysql_num_rows($verifica);
    if ($checar == 0) {
    mysql_query("INSERT INTO users (nome,usuario,email,senha,senha2,foto) VALUES ('".$name."','".$usuario."','".$email."','".$senha."','".$senha2."','".$new_name.".".$extension."')");
    move_uploaded_file($tempname, $folder."/".$new_name.".".$extension);
    echo "Usuario cadastrado com sucesso";
    }else{
        echo "O usuario já existe!";
    }

}
?>

Profile.php

<?php 
require_once('config.php');
require_once("config.php");
mysql_connect("$host", "$usuario", "$senha");
mysql_select_db("$db");
$sql = mysql_query("SELECT * FROM users")OR DIE(mysql_error());

  while ($res = mysql_fetch_array($sql)) {
    $id    = $res[0];      

echo ("<img src=imagens/".$id.$res[foto]);    

}
?>  

(where the image should go) But it will all stored images, but I would like only the Audiomiro to appear... I am waiting =D

  • The.php profile you need to add a where with only the id of that user.

  • I tried, but my way still appears the other images, I would give an example?

  • How do you call this.php profile page? for a link? explains this better.

  • The way I tried was to search the user through index.php on line 24, clicking on it opens the page /profile.php? id="name" in the case of Bruno

  • 1

    id should be a number, ai in php profile you do, $id = $_GET['id']; .... then sets the select to SELECT * FROM users where id = $id

  • Thank you @rray I will update the final code :D

  • If you were able to solve, create an answer :P, do not edit the question to add the answer hehe.

Show 2 more comments

1 answer

1


I changed line 24 to id, and used $_GET to get the id in the profile.php :D

<?php
require_once("config.php");
mysql_connect("$host", "$usuario", "$senha");
mysql_select_db("$db");

  $user = $_POST['user'];
  $pass = $_POST['pass'];
  $resultado = mysql_query("SELECT * FROM notas WHERE id='".$id."'");
$verificacao = "SELECT * FROM users WHERE usuario ='$user' AND senha = '$pass'";
$resultado = mysql_query($verificacao);
$iniciar = mysql_num_rows($resultado);
mysql_select_db("usuarios", mysql_connect('localhost','root','')); 
$linha = mysql_fetch_array($resultado);
if ($iniciar == 1){

  ?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

  <div id="header"><div id="logo">Logo</div>
    <div id="menu">
    <li><a href="perfil.php?id=<?php echo $linha['id']; ?>"><?php echo $_POST['user'];?></a></li>
    <li><a href="#">Encontre alguém</a></li>
  </div>
  </div>
</body>
</html>
<?php
}else{
   header("Location: index.html");
}
?>



<?php 
require_once('config.php');
$id = $_GET['id'];
mysql_connect("$host", "$usuario", "$senha");
mysql_select_db("$db");
$sql = mysql_query("SELECT * FROM users where id = '$id'")OR DIE(mysql_error());

  while ($res = mysql_fetch_array($sql)) {   

?>
<img src="imagens/<?php echo $res['foto']?>" width="260" height="260">
<?php
}
?>  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.