Olá estou com um problema no meu form ao editar(update) ele salva a mesma coisa que eu fiz num coluna para todos os outros. tipo:
ID: | Nome: | Local: | Telefone: |
1 | Lucas | Av.Paris | 3361-2492 | EDITAR / EXCLUIR
2 | Hercules | Av. Brasil | 3361-7160 | EDITAR / EXCLUIR
_____________________________________________________________
Por exemplo quando vou editar o ID 1 ele salva o ID 2 igual a do 1. fica assim:
ID: | Nome: | Local: | Telefone: |
1 | Lucas | Av.Paris | 3361-2492 | EDITAR / EXCLUIR
2 | Lucas | Av.Paris | 3361-2492 | EDITAR / EXCLUIR
index.php
<?php include 'DBConfig.php'; //Insert or Update contact information if(isset($_POST['action_type'])) { if ($_POST['action_type'] == 'add' or $_POST['action_type'] == 'edit') { // Higienizar os dados e atribuir a variáveis $id = mysqli_real_escape_string($link, strip_tags($_POST['id'])); $titulo = mysqli_real_escape_string($link, strip_tags($_POST['titulo'])); $categoria = mysqli_real_escape_string($link, strip_tags($_POST['categoria'])); $tag = mysqli_real_escape_string($link, strip_tags($_POST['tag'])); $imagem = mysqli_real_escape_string($link, strip_tags($_POST['imagem'])); $local = mysqli_real_escape_string($link, strip_tags($_POST['local'])); $numero = mysqli_real_escape_string($link, strip_tags($_POST['numero'])); $bairro = mysqli_real_escape_string($link, strip_tags($_POST['bairro'])); $cidade = mysqli_real_escape_string($link, strip_tags($_POST['cidade'])); $estado = mysqli_real_escape_string($link, strip_tags($_POST['estado'])); $telefone = mysqli_real_escape_string($link, strip_tags($_POST['telefone'])); $site = mysqli_real_escape_string($link, strip_tags($_POST['site'])); if ($_POST['action_type'] == 'add') { $sql = "insert into academias set titulo = '$titulo', categoria = '$categoria', tag = '$tag', imagem = '$imagem', local = '$local', numero = '$numero', bairro = '$bairro', cidade = '$cidade', estado = '$estado', telefone = '$telefone', site = '$site'"; }else{ $sql = "update academias set titulo = '$titulo', categoria = '$categoria', tag = '$tag', imagem = '$imagem', local = '$local', numero = '$numero', bairro = '$bairro', cidade = '$cidade', estado = '$estado', telefone = '$telefone', site = '$site'"; } if (!mysqli_query($link, $sql)) { echo 'Erro ao Salvar os Dados. ' . mysqli_error($link); exit(); } } header('Location: contactlist.php'); exit(); } //End Insert or Update contact information //Start of edit contact read $gresult = ''; //declare global variable if(isset($_POST["action"]) and $_POST["action"]=="edit"){ $id = (isset($_POST["ci"])? $_POST["ci"] : ''); $sql = "select id, titulo, categoria, tag, imagem, local, numero, bairro, cidade, estado, telefone, site from academias where id = $id"; $result = mysqli_query($link, $sql); if(!$result) { echo mysqli_error($link); exit(); } $gresult = mysqli_fetch_array($result); include 'update.php'; exit(); } //end of edit contact read //Start Delete Contact if(isset($_POST["action"]) and $_POST["action"]=="delete"){ $id = (isset($_POST["ci"])? $_POST["ci"] : ''); $sql = "delete from academias where id = $id"; $result = mysqli_query($link, $sql); if(!$result) { echo mysqli_error($link); exit(); } } //End Delete Contact //Read contact information from database $sql = "select id, titulo, categoria, tag, imagem, local, numero, bairro, cidade, estado, telefone, site from academias"; $result = mysqli_query($link, $sql); if(!$result) { echo mysqli_error($link); exit(); } $contact_list = array(); //Loo through each row on array and store the data to $contact_list[] while($rows = mysqli_fetch_array($result)) { $contact_list[] = array('id' => $rows['id'], 'titulo' => $rows['titulo'], 'categoria' => $rows['categoria'], 'tag' => $rows['tag'], 'imagem' => $rows['imagem'], 'local' => $rows['local'], 'numero' => $rows['numero'], 'bairro' => $rows['bairro'], 'cidade' => $rows['cidade'], 'estado' => $rows['estado'], 'telefone' => $rows['telefone'], 'site' => $rows['site']); } include 'contactlist.php'; exit(); ?>
update.php
<!DOCTYPE html> <html> <head> <title>Phonebook - Update</title> <script type="text/javascript"> function Validate(){ var valid = true; var message = ''; var fname = document.getElementById("titulo"); var lname = document.getElementById("categoria"); if(fname.value.trim() == ''){ valid = false; message = message + '* Titulo e necessario ' + '\n'; } if(lname.value.trim() == ''){ valid = false; message = message + '* Porfavor verifiquem as TAGS'; } if (valid == false){ alert(message); return false; } } function GotoHome(){ window.location = 'index.php?'; } </script> </head> <body> <center> <div class="form-style-6"> <div class="form-style-6" style="width: 500px !important;"> <?php include 'header.php'; ?><br/> <div> <form id="frmContact" method="POST" action="index.php" onSubmit="return Validate();"> <input type="hidden" name="is"/> <table> <tr> <td> <label for="titulo">Titulo: </label> </td> <td> <input type="text" name="titulo" value="<?php echo (isset($gresult) ? $gresult["titulo"] : ''); ?>" id="titulo" class="txt-fld"/> </td> </tr> <tr> <td> <label for="categoria">Categoria: </label> </td> <td> <input type="text" name="categoria" value="academias" id="categoria" class="txt-fld"/> </td> </tr> <tr> <td> <label for="tag">TAG: </label> </td> <td> <input type="text" name="tag" value="<?php echo (isset($gresult) ? $gresult["tag"] : ''); ?>" class="txt-fld"/> </td> </tr> <tr> <td> <label for="imagem"> Imagem </label> </td> <td> <input type="text" name="imagem" value="<?php echo (isset($gresult) ? $gresult["imagem"] : ''); ?>" class="txt-fld"/> </td> </tr> <tr> <td> <label for="local">Local: </label> </td> <td> <input type="text" name="local" value="<?php echo (isset($gresult) ? $gresult["local"] : ''); ?>" class="txt-fld"/> </td> </tr> <tr> <td> <label for="numero">Numero: </label> </td> <td> <input type="text" name="numero" value="<?php echo (isset($gresult) ? $gresult["numero"] : '');?>" class="txt-fld"/> </td> </tr> <tr> <td> <label for="bairro">Bairro: </label> </td> <td> <input type="text" name="bairro" value="<?php echo (isset($gresult) ? $gresult["bairro"] : '');?>" class="txt-fld"/> </td> </tr> <tr> <td> <label for="cidade">Cidade: </label> </td> <td> <input type="text" name="cidade" value="<?php echo (isset($gresult) ? $gresult["cidade"] : '');?>" class="txt-fld"/> </td> </tr> <tr> <td> <label for="estado">Estado: </label> </td> <td> <input type="text" name="estado" value="<?php echo (isset($gresult) ? $gresult["estado"] : '');?>" class="txt-fld"/> </td> </tr> <tr> <td> <label for="telefone">Telefone: </label> </td> <td> <input type="text" name="telefone" value="<?php echo (isset($gresult) ? $gresult["telefone"] : '');?>" class="txt-fld"/> </td> </tr> <tr> <td> <label for="site">Site: </label> </td> <td> <input type="text" name="site" value="<?php echo (isset($gresult) ? $gresult["site"] : '');?>" class="txt-fld"/> </td> </tr> </table> <input type="hidden" name="action_type" value="<?php echo (isset($gresult) ? 'edit' : 'add');?>"/> <div style="text-align: center; padding-top: 30px;"> <input class="btn" type="submit" name="save" id="save" value="Salvar" /> <input class="btn" type="submit" name="save" id="cancel" value="Cancelar" onclick=" return GotoHome();"/> </div> </form> </div> </div> </div> </center> </body> </html>