Jump to content

update


Go to solution Solved by Barand,

Recommended Posts

Hi, i'm having a problem with update data:

 

I have a page to create a category (category = categoria ,in my code) and it's working, but now i need another page to update the category if it's need, and that's where the error begings...

 

First i create this form: 

<form action='altcategoria.php' method='POST' enctype='multipart/form-data'>
<select name="updateCategoria">
<?php
$consulta = "SELECT nome FROM categoria ORDER BY id";
$resultado = mysqli_query($con, $consulta);
while ($row = mysqli_fetch_array($resultado)){
echo "<option value=''>", $row{"nome"}, "</option>";
}
?>
</select>
<?php 	
echo"<br />";
?>
			
			
<input type="text" name="novoCat" placeholder="Novo nome">
<input type='file' name='myfile2' id='myfile2'>
<input type="submit" name="alterarCat" value="Alterar">
</form>

And then i created the page altcategoria.php with this code:

<?php
$con=mysqli_connect("localhost","root","6794","website");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$novoCat = $_POST["novoCat"];
$selectCat = $S_POST["updateCategoria"];

	
	
//INSERIR IMAGEM

$name = $_FILES["myfile2"]["name"];
$type = $_FILES["myfile2"]["type"];
$size = $_FILES["myfile2"]["size"];
$temp = $_FILES["myfile2"]["tmp_name"];
$error = $_FILES["myfile2"]["error"];
	
$i = 1;
$actual_name = pathinfo($name,PATHINFO_FILENAME);
$original_name = $actual_name;
$extension = pathinfo($name, PATHINFO_EXTENSION);

$nome = $actual_name.".".$extension;
$caminho = $name;
	
if($error > 0){
die("Erro");
}
else{
if($type == "image/jpg" || $type == "image/png" || $type == "image/jpeg"){
	
if(file_exists('imgcategoria/' . isset($_FILES[$name]['name']))){
				
while(file_exists('imgcategoria/'.$actual_name.".".$extension)){           
$actual_name = (string)$original_name.$i;
$nome = $actual_name.".".$extension;
$i++;
}
			
echo "O upload do ficheiro " . $nome .  " deu certo.";
				
//mover ficheiro para pasta imgcategoria
move_uploaded_file($temp,"imgcategoria/".$nome);
				
//inserir caminho do ficheiro na base dados
$caminho2 = $nome;
				
$update2 = "INSERT INTO imagem(id, caminho) VALUES (DEFAULT, '$caminho2')";
mysqli_query($con, $update2);
				
$updateCat= "UPDATE categoria SET id = DEFAULT, nome = '$novoCat', caminhoImagem = '$caminho2' WHERE nome = '$selectCat' ";

mysqli_query($con, $updateCat);			
}
else{

//mover ficheiro para pasta imgcategoria
move_uploaded_file($temp,"imgcategoria/".$name);
			
//inserir caminho do ficheiro na base dados
$caminho = $name;
	
$insereIMG = "INSERT INTO imagem(id, caminho) VALUES (DEFAULT, '$caminho')";
mysqli_query($con, $insereIMG);				
				
$updateCat2= "UPDATE categoria SET id = DEFAULT, nome = '$novoCat', caminhoImagem = '$caminho' WHERE nome = '$selectCat' ";

mysqli_query($con, $updateCat2);
			
}
}
else{
die("Tipo/tamanho nao suportado");
}		
}	
	
header('Location: inicio.php');

mysqli_close($con);
?>

With that i can upload the file to the table "imagem" but the update of data in the table "categoria" it's not working.

Anyone can help me?

 

Btw: i'm sending the code in attachment to a better read.

altcategoria.php

Link to comment
https://forums.phpfreaks.com/topic/296890-update/
Share on other sites

  • Solution

 

while ($row = mysqli_fetch_array($resultado)){

echo "<option value=''>", $row{"nome"}, "</option>";

}

All your options have an empty string as their value. Either

  • omit the value=''so it defaults to to the "nome" value.
  • Explicitly specify the $row['nome'] as the value
  • Better still, specify the id as the value and use the id instead of name in related records
Edited by Barand
Link to comment
https://forums.phpfreaks.com/topic/296890-update/#findComment-1514228
Share on other sites

 

All your options have an empty string as their value. Either

  • omit the value=''so it defaults to to the "nome" value.
  • Explicitly specify the $row['nome'] as the value
  • Better still, specify the id as the value and use the id instead of name in related records

 

 thx for the help, i made it 

echo "<option value='", $row{"nome"}, "'>", $row{"nome"}, "</option>";
Edited by magcr23
Link to comment
https://forums.phpfreaks.com/topic/296890-update/#findComment-1514230
Share on other sites

I have another problem, when i do 

$updateCat2= "UPDATE categoria SET id = DEFAULT, nome = '$novoCat', caminhoImagem = '$caminho' WHERE nome = '$selectCat' ";

all the updates categorys will get id=0. how can i make it continue the auto increment? 

I mean, if i had id = 1,2,3,4,5 and i update the id=3 it will keep id=3

Link to comment
https://forums.phpfreaks.com/topic/296890-update/#findComment-1514232
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.