1
I recorded this image in the database generated me the ID:20.
Form to save : INICIAL.PHP (Code to save image)
<html>
<head>
<title></title>
</head>
<body>
<form enctype="multipart/form-data" method="post">
<input type="file" name="image" /><br />
<input type="submit" value="Enviar" name="sumit" />
</form>
<?php
if(isset($_POST['sumit']))
{
if(getimagesize($_FILES['image']['tmp_name'])== FALSE)
{
echo "Selecione uma imagem.";
}
else
{
$image = addslashes($_FILES['image']['tmp_name']);
$name = addslashes($_FILES['image']['name']);
$image = file_get_contents($image);
$image = base64_encode($image);
saveimagem($name, $image);
}
}
mostrarimagem();
function saveimagem($name, $image)
{
$host = "localhost";
$user = "root";
$pass = "";
$conexao = mysqli_connect($host, $user, $pass) or die (mysql_error());
mysqli_select_db($conexao, "teste");
$sql = "insert into cadastro (foto) values ('$image')";
$result = mysqli_query($conexao, $sql);
if($result)
{
echo "<br/>Foi feito o upload" ;
$id = mysqli_insert_id($conexao);
echo $id;
}else
{
echo "<br/>Não foi feito o upload";
}
}
function mostrarimagem()
{
$host = "localhost";
$user = "root";
$pass = "";
$conexao = mysqli_connect($host, $user, $pass) or die (mysql_error());
mysqli_select_db($conexao, "teste");
$sql = "select * from cadastro";
$result = mysqli_query($conexao, $sql);
while($row = mysqli_fetch_array($result))
{
echo '<img height="300" width="300" src="data:image;base64,'.$row[11].'">';
}
mysqli_close($conexao);
}
?>
</body>
Where has the $id = mysqli_insert_id($conexao);
is the variable that stored the last record in the database. I need to use it for the update in another form.
WRITE.PHP
<?php
//Irá receber os valores enviados e guardar no banco de dados
<?php
//Irá receber os valores enviados e guardar no banco de dados
if($_POST['enviar']){
include 'inicial.php';
$nome = $_POST['NomeAluno'];
$dataNasc = $_POST['DataNasc'];
$sexo = $_POST['Sexo'];
$numcel = $_POST['Celular'];
$numtel = $_POST['Telefone'];
$endereco = $_POST['Endereco'];
$numres = $_POST['NumResid'];
$uf = $_POST['UF'];
$rg = $_POST['RG'];
$prontuario = $_POST['Prontuario'];
$datavalidade = $_POST['DataValidade'];
$curso = $_POST['Curso'];
$semestre = $_POST['Semestre'];
$periodo = $_POST['Periodo'];
$email = $_POST['Email'];
$senha = $_POST['Senha'];
//"$sql = "SELECT MAX(ID) FROM cadastro";
// Insere os dados no banco
$sql = mysqli_query($conexao, "UPDATE cadastro set
nome_aluno = ':$nome',
data_nascimento = ':$dataNasc',
sexo = ':$sexo',
celular = ':$numcel',
telefone = ':$numtel',
endereco = ':$endereco',
numero = ':$numres',
uf = ':$uf',
rg = ':$rg',
prontuario = ':$prontuario',
data_validade = ':$datavalidade',
curso = ':$curso',
semestre = ':$semestre',
periodo = ':$periodo',
email = ':$email',
senha = ':$senha'
where id = '$id'");
// Se os dados forem inseridos com sucesso
if ($sql){
echo "Você foi cadastrado com sucesso.";
}
}
?>
I can’t update the table with the other values, without touching the photo that has already been saved. And using the $id variable, I even put the include 'inicial.php';
.
I don’t know if it is my Update SQL that is wrong, but when I click save button this error appears: P Notice: Undefined variable: id in C:\wamp\www\ifsp\gravar.php on line 45
Those
:
in front of the variables is what?– rray
I don’t know why I put it, but it didn’t change anything
– WSS
Where you make the call from
saveimage()
? missing to return the$id
in this function ;). takes these out:
of everything.– rray
I just put the include initial.php, with it will not everything that is on the page? I already took
– WSS
Yes, but you need to call
saveimage()
pass the values and place at the end of the functionreturn $id;
– rray
What values? So:
include 'inicial.php'
saveimagem($id){
 return $id
 }
. Of error yetParse error: syntax error, unexpected '{' in C:\wamp\www\ifsp\gravar.php on line 22
, something I noticed too, that when I click save in the form it redirects me to the pageinicial.php
, only because of include– WSS
in initial.php has more code than the function?
– rray
Got it, I’ll put it all up there
– WSS
Dude, you have to see if the other fields accept null, because I realized you add the photo first and then you want to get the generated id to update the data right, you have to see tbm if the id is generated automatically.
– Romario Pires
Yes ID is auto_increment and when the photo is saved the fields are to accept null. : s
– WSS
Has been answered then!
– Romario Pires
Similar: http://answall.com/questions/89986/pega-o-id-da-%C3%Baltima-line-in-database
– Edilson
@Edilson, no guy are not similar. This first didn’t even know how to get the ID. Now I don’t know use it
– WSS
It’s basically the same thing. And another thing, even though you showed me what the problem is, it would help more if you said what you really want, or what you want the whole script to do.
– Edilson
Sorry. I’m not used to this forum yet. : x
– WSS