Jump to content
Fórum Script Brasil
  • 0

Ajuda Mysql PHP


evertonlima
 Share

Question

Pessoal estou com uma dificuldade em resolver um problema de edição no mysql usando php, tenho as telas abaixo que ao editar não visualiza mais o e-mail.

O sistema cadastra e exclui normalmente. Também não consegui colocar as páginas php em utf-8. Agradeço a ajuda.

bd

CREATE TABLE IF NOT EXISTS `aniversarios` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`email` longtext COLLATE utf8_unicode_ci NOT NULL,
`dataniver` date NOT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

tela listar.php

screen1.png

tela após editar da listar.php

screen2.png

código listar.php

<a href="index.html">Voltar</a> | <a href="cadastro.php">Novo Cadastro</a>
<?php
include 'conn.php';
$re = mysql_query("SELECT * FROM aniversarios ORDER BY id;");
?>
<br />
<table width="100%" border="1">
	<tr>
		<td><b>Acoes</b></td>
		<td><b>Nome</b></td>
		<td><b>Email</b></td>
		<td><b>Data Aniversário</b></td>
	</tr>
<?php
while($l = mysql_fetch_array($re)) {
	$id   		 = $l["id"];
	$nome        = $l["nome"];
	$email       = $l["email"];
	$dataniver   = $l["dataniver"];
	$data   = implode("/",array_reverse(explode("-",$dataniver)));
echo "
	<tr>
		<td><a href=\"editar.php?id=$id\">[Editar]</a> <a href=\"excluir.php?id=$id\">[Excluir]</a></td>
		<td> $nome</td>		
		<td> $email</td>
		<td> $data</td>
	</tr>\n";
}	
@mysql_close();
?>
</table>

código editar.php

<?php
include 'conn.php';
// Recuperando ID do cliente
$id = $_GET['id'];
// Verificando ação a ser executada
$acao = $_GET['acao'];
	if($acao == "editar") {
		$nome         = $_POST["nome"];
		$email        = $_POST["email"];
		$dataniver 	  = $_POST['dataniver'];
		$data = implode("-",array_reverse(explode("/",$dataniver)));
		$sql = mysql_query("UPDATE aniversarios SET nome='$nome',email='$email',dataniver='$data' WHERE id='$id'");
		if ($sql){
		echo "<script language='javascript'>alert('Cliente editado com sucesso'); window.location.href = 'listar.php'</script>";
		}
		else {
		echo "<script language='javascript'>alert('Não foi possível editar o cliente'); window.location.href = 'listar.php'</script>";
		}

}
$order = "SELECT * FROM aniversarios where id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Editar</title>
<style>
<!--
.textBox { border:1px solid gray; width:200px;} 
-->
</style>
</head>
<body>
<form method="post" action="editar.php?id=<?php echo $id ?>&acao=editar">
  <table width="400" border="0" align="center">
    <tr>
      <td width="145">Nome</td>
      <td width="245"><input name="nome" type="text" id="nome" maxlength="100" class="textBox" value="<? echo "$row[nome]"?>" /></td>
    </tr>
    <tr>
      <td>Email</td>
      <td><input name="email" type="text" id="email" maxlength="100" class="textBox" value="<? echo "$row[email]"?>" /></td>
    </tr>
    <tr>
      <td>Data Nascimento</td>
      <td><input name="email" type="text" id="data" maxlength="10" class="textBox" value="<? echo date("d/m/Y", strtotime($row[dataniver]));?>" /></td>
    </tr>  
<td>
<input type="submit" name="submit" value="Editar">
</table>
</form>
<a href="listar.php">Voltar</a>
</body>
</html>
Edited by evertonlima
Link to comment
Share on other sites

1 answer to this question

Recommended Posts

  • 0

O atributo name do campo data do form está erradamente colocado como "email". Provavelmente é isso que está a fazer com que a actualização seja mal feita.

Para além disso, considera passar a usar mysqli em vez de mysql para acesso à base de dados.

<!-- listar.php -->
<body>
	<a href="index.html">Voltar</a> | <a href="cadastro.php">Novo Cadastro</a>
	<?php
		include 'conn.php';
		$re = $mysqli->query("SELECT * FROM aniversarios ORDER BY id");
	?>
	<br />
	<table width="100%" border="1">
		<tr>
			<td><b>Acoes</b></td>
			<td><b>Nome</b></td>
			<td><b>Email</b></td>
			<td><b>Data Anivers&aacute;rio</b></td>
		</tr>
	<?php
		while($l = $re->fetch_array()) {
			$id   	     = $l["id"];
			$nome        = $l["nome"];
			$email       = $l["email"];
			$dataniver   = implode("/",array_reverse(explode("-",$l["dataniver"])));
		echo "
			<tr>
				<td><a href=\"editar.php?id=$id\">[Editar]</a> <a href=\"excluir.php?id=$id\">[Excluir]</a></td>
				<td> $nome</td>		
				<td> $email</td>
				<td> $dataniver</td>
			</tr>\n";
		}	
	?>
	</table>
</body>

<!-- editar.php -->
<?php
	include 'conn.php';

	// Recuperando ID do cliente
	$id = $_GET['id'];
	// Verificando ação a ser executada
	$acao = $_GET['acao'];
	if ($acao == "editar") {
		$nome         = $_POST["nome"];
		$email        = $_POST["email"];
		$dataniver    = implode("-",array_reverse(explode("/",$_POST['data'])));
		$sql = $mysqli->query("UPDATE aniversarios SET nome='$nome',email='$email',dataniver='$dataniver' WHERE id='$id'");
		if ($sql) {
			echo "<script language='javascript'>alert('Cliente editado com sucesso');</script>";
		}
		else {
			echo "<script language='javascript'>alert('Não foi possível editar o cliente');</script>";
		}
	}
	$order = "SELECT * FROM aniversarios where id=$id";
	$result = $mysqli->query($order);
	$row = $result->fetch_assoc();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<meta charset="utf-8" />
		<title>Editar</title>
		<style>
		<!--
		.textBox { border:1px solid gray; width:200px;} 
		-->
		</style>
	</head>
	<body>
		<form method="post" action="editar.php?id=<?php echo $id; ?>&acao=editar">
		  <table width="400" border="0" align="center">
			<tr>
			  <td width="145">Nome</td>
			  <td width="245"><input name="nome" type="text" id="nome" maxlength="100" class="textBox" value="<?php echo $row['nome']; ?>" /></td>
			</tr>
			<tr>
			  <td>Email</td>
			  <td><input name="email" type="text" id="email" maxlength="100" class="textBox" value="<?php echo $row['email']; ?>" /></td>
			</tr>
			<tr>
			  <td>Data Nascimento</td>
			  <td><input name="data" type="text" id="data" maxlength="10" class="textBox" value="<?php echo date("d/m/Y", strtotime($row["dataniver"])); ?>" /></td>
			</tr>  
		<td>
		<input type="submit" name="submit" value="Editar">
		</table>
		</form>
		<a href="listar.php">Voltar</a>
	</body>
</html>
Edited by wootzor
Link to comment
Share on other sites

Join the conversation

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

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  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.

 Share



  • Forum Statistics

    • Total Topics
      150.1k
    • Total Posts
      647k
×
×
  • Create New...