magcr23 Posted June 18, 2015 Share Posted June 18, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296890-update/ Share on other sites More sharing options...
magcr23 Posted June 18, 2015 Author Share Posted June 18, 2015 Hi again, i uses an echo $updateCat; and that's what i got: O upload do ficheiro bianca5.PNG deu certo. UPDATE categoria SET id = DEFAULT, nome = 'tas', caminhoImagem = 'bianca5.PNG' WHERE nome = '' I know where the error is, but not how fix it... Quote Link to comment https://forums.phpfreaks.com/topic/296890-update/#findComment-1514224 Share on other sites More sharing options...
Ch0cu3r Posted June 18, 2015 Share Posted June 18, 2015 $S_POST should be $_POST on this line $selectCat = $S_POST["updateCategoria"]; Quote Link to comment https://forums.phpfreaks.com/topic/296890-update/#findComment-1514226 Share on other sites More sharing options...
magcr23 Posted June 18, 2015 Author Share Posted June 18, 2015 $S_POST should be $_POST on this line $selectCat = $S_POST["updateCategoria"]; yeah i found that, but keeps the same error: UPDATE categoria SET id = DEFAULT, nome = 'tas', caminhoImagem = 'bianca5.PNG' WHERE nome = '' Quote Link to comment https://forums.phpfreaks.com/topic/296890-update/#findComment-1514227 Share on other sites More sharing options...
Solution Barand Posted June 18, 2015 Solution Share Posted June 18, 2015 (edited) 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 June 18, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/296890-update/#findComment-1514228 Share on other sites More sharing options...
magcr23 Posted June 18, 2015 Author Share Posted June 18, 2015 (edited) 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 June 18, 2015 by magcr23 Quote Link to comment https://forums.phpfreaks.com/topic/296890-update/#findComment-1514230 Share on other sites More sharing options...
magcr23 Posted June 18, 2015 Author Share Posted June 18, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296890-update/#findComment-1514232 Share on other sites More sharing options...
Barand Posted June 18, 2015 Share Posted June 18, 2015 Autoincrement applies to insert queries. Don't set a value for the id in the update or inserts, let the autoincrement look after the values. Once an id has been allocated on INSERT, it should never be changed Quote Link to comment https://forums.phpfreaks.com/topic/296890-update/#findComment-1514234 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.