Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
bom dia
gostaria de saber por favor fazer o seguinte
criei uma pagina aonde eu tenho 3 inputs aonde a pessoa digita os dados para inserir e alterar...
e tem uma tabela que é criada e atualizada apos inserir, atualizar ou deletar dados...
Fiz uma arquivo separado de funcoes, como
Inserir, alterar, deletar e mostrar...fazendo transação com o mysql
E ai queria saber...como eu posso fazer para alterar e deletar pois o inserir esta funcionando facil...
Problema seria os 2 acima citados
Gostaria de usar um figura simbolizando o editar e deletar...
Obrigado
>
pra editar e deletar os dados você precisa primeiro selecioná-los..
na hora de puxar os dados do banco você põe os dados em um laço de repetição e vai salvando os ids no action do form, ficando desta maneira:
<form action='editardados.php?id=1' method='post'>
Edite o nome: <input type='text' name='nome'>
</form>
aí na página editardados.php, você pega o id e o valor do nome e da um simples update..
acho q essa é a maneira mais simples de se fazer isso.
Tipo eu ja vi uns exemplo igual você me sugeriu
Mas estou fazendo tudo junto nas funcoes sql...em um unico arquivo...
<html>
<head>
<title>Estudo</title>
<script type="text/javascript" src="funcao.js"></script>
<meta charset="utf-8"/>
</head>
<body>
<form action="index.php" method="post">
<fieldset>
<legend>Estudo</legend>
<label>Pneu: <input type="text" name="pneu" /></label>
<label>Oleo: <input type="text" name="oleo" /></label>
<label>Vela: <input type="text" name="vela" /></label>
<input type="submit" name="submit" value="Inserir" />
</fieldset>
<fieldset>
<legend>Dados Registrados</legend>
<table>
<?php include_once('sql.php'); mostrar(); ?>
</table>
</fieldset>
</form>
</body>
</html>
<?php
include_once('sql.php');
if(isset($_REQUEST['submit'])){
switch($_REQUEST['submit']){
case 'Inserir':
inserir();
break;
case 'Alterar':
alterar();
break;
case 'Deletar':
deletar();
break;
}
}
?>
<?php
function inserir(){
include_once('funcao.php');
$pneu = validaCampos($_REQUEST['pneu'],"pneu");
$oleo = validaCampos($_REQUEST['oleo'],"oleo");
$vela = validaCampos($_REQUEST['vela'],"vela");
include_once('configuracao.php');
$sql = "INSERT INTO pedido (pneu, oleo, vela) VALUES ('$pneu','$oleo','$vela')";
mysql_query($sql) or die ('<script>alert("Falha ao inserir o registro")</script>');
mysql_close($conexao);
}
function alterar(){
include_once('funcao.php');
$id = $_REQUEST['id'];
$pneu = validaCampos($_REQUEST['pneu']);
$oleo = validaCampos($_REQUEST['oleo']);
$vela = validaCampos($_REQUEST['vela']);
include_once('configuracao.php');
$sql = "UPDATE pedido SET pneu = '$pneu',
oleo = '$oleo',
vela = '$vela'
WHERE id = '$id'";
mysql_query($sql) or die ('<script>alert("Falha ao alterar o registro")</script>');
mysql_close($conexao);
}
function deletar(){
$id = $_REQUEST['id'];
include_once('configuracao.php');
$sql = "DELETE FROM pedido WHERE id = '$id'";
mysql_query($sql) or die ('<script>alert("Falha ao deletar o registro")</script>');
mysql_close($conexao);
}
function mostrar(){
include_once('configuracao.php');
$sql = "SELECT * FROM pedido";
$query = mysql_query($sql) or die ('<script>alert("Falha ao carregar os registros")</script>');
mysql_close($conexao);
$topo = '<thead>
<tr>
<th>Código</th>
<th>Pneu</th>
<th>Oleo</th>
<th>Vela</th>
<th>Alterar</th>
<th>Excluir</th>
</tr>
</thead>';
$formato = '<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td></td>
<td></td>
</tr>';
echo($topo);
while($linha = mysql_fetch_array($query)){
printf($formato,$linha['id'],$linha['pneu'],$linha['oleo'],$linha['vela']);
}
}
?>
a funcao tabela esta incompleta por conta dessa parte que não sei muito bem o q fazer....
Consegui resolver usando
input do tipo hidden e input do tipo image...
segue o código abaixo...
<html>
<head>
<title>Estudo</title>
<script type="text/javascript" src="funcao.js"></script>
<meta charset="utf-8"/>
<?php include_once('sql.php');?>
</head>
<body>
<form action="index.php" method="post">
<fieldset>
<legend>Estudo</legend>
<label>Pneu: <input type="text" name="pneu" /></label>
<label>Oleo: <input type="text" name="oleo" /></label>
<label>Vela: <input type="text" name="vela" /></label>
<input type="image" src="ins.png" name="submit" value="Inserir" />
</fieldset>
<fieldset>
<legend>Dados Registrados</legend>
<table>
<?php mostrar(); ?>
</table>
</fieldset>
</form>
</body>
</html>
<?php
if(isset($_REQUEST['submit'])){
switch($_REQUEST['submit']){
case 'Inserir':
inserir();
break;
case 'Alterar':
alterar();
break;
case 'Deletar':
deletar();
break;
}
}
?>
<?php
include_once('configuracao.php');
include_once('funcao.php');
function inserir(){
$pneu = validaCampos($_REQUEST['pneu'],"pneu");
$oleo = validaCampos($_REQUEST['oleo'],"oleo");
$vela = validaCampos($_REQUEST['vela'],"vela");
$sql = "INSERT INTO pedido (pneu, oleo, vela) VALUES ('$pneu','$oleo','$vela')";
mysql_query($sql) or die ('<script>alert("Falha ao inserir o registro")</script>');
atualizarRegistro();
}
function alterar(){
$id = $_REQUEST['id'];
$pneu = validaCampos($_REQUEST['pneu']);
$oleo = validaCampos($_REQUEST['oleo']);
$vela = validaCampos($_REQUEST['vela']);
$sql = "UPDATE pedido SET pneu = '$pneu',
oleo = '$oleo',
vela = '$vela'
WHERE id = '$id'";
mysql_query($sql) or die ('<script>alert("Falha ao alterar o registro")</script>');
atualizarRegistro();
}
function deletar(){
$id = $_REQUEST['id'];
$sql = "DELETE FROM pedido WHERE id = '$id'";
mysql_query($sql) or die ('<script>alert("Falha ao deletar o registro")</script>');
atualizarRegistro();
}
function mostrar(){
$sql = "SELECT * FROM pedido";
$query = mysql_query($sql) or die ('<script>alert("Falha ao carregar os registros")</script>');
$topo = '<thead>
<tr>
<th>Código</th>
<th>Pneu</th>
<th>Oleo</th>
<th>Vela</th>
<th>Alterar</th>
<th>Excluir</th>
</tr>
</thead>';
$formato = '<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>%s</td>
<td>
<input type="image" src="alt.png" name="submit" value="Alterar" />
<input type="hidden" name="id" value="%s" />
</td>
<td>
<input type="image" src="del.png" name="submit" value="Deletar" />
<input type="hidden" name="id" value="%s" />
</td>
</tr>';
echo($topo);
while($linha = mysql_fetch_array($query)){
printf($formato,$linha['id'],$linha['pneu'],$linha['oleo'],$linha['vela'],$linha['id'],$linha['id']);
}
}
?>
pra editar e deletar os dados você precisa primeiro selecioná-los..
na hora de puxar os dados do banco você põe os dados em um laço de repetição e vai salvando os ids no action do form, ficando desta maneira:
acho q essa é a maneira mais simples de se fazer isso.