snakept 0 Posted April 4, 2011 Report Share Posted April 4, 2011 Boa Tarde, Começei a cerca de umas semanas no php, ainda estou um pouco fresco mas estou a ficar interessado, tenho tentado fazer uma BD de PHP+SQL, até agora tenho me desenrrascado sem pedir ajuda, apenas a procurar no google e aos poucos vou percebendo os erros que tenho cometido, mas desta não estou a perceber o porquê do erro. Precisava de uma ajuda, ao clicar no botão "Escolher" da combobox quero que apareça numa textbox o texto que está selecionado na combobox. Aqui vai parte do codigo: if ($_SERVER['REQUEST_METHOD'] == 'POST') { $subadd=$_POST['subadd']; $notadd=$_POST['notadd']; $subalt=$_POST['subalt']; $notalt=$_POST['notalt']; if (isset($_POST['submitadd'])){ if (empty($subadd) or empty($notadd)){ $addMessage="Favor introduzir uma noticia valida!"; }elseif($pesq>0){ $addMessage= '<p align=center>Subject colocado já existente!</p>'; } else { $dbinsert="INSERT INTO Noticias (Subject,Noticia) VALUES ('$subadd','$notadd')"; $dbresult=mysql_query($dbinsert)or die(mysql_error()); if ($dbresult) $addMessage="Noticia adicionada com sucesso"; } }elseif (isset($_POST['submitesc'])) { if(isset($_POST['submitalt'])){ $notMessage="Em desenvolvimento..."; }elseif (isset($_POST['submitdel'])){ $notMessage="Em desenvolvimento..."; } $sql="SELECT * FROM Noticias WHERE ID='$select'"; $result=mysql_query($sql); $row=mysql_fetch_row($result); $subjectesc=$row['Subject']; $noticiaesc=$row['Noticia']; $notMessage=$select; } } <div id="principal"> <h2><p align="center">Noticias</h2> <?php include ("connect.php"); $sql="SELECT * FROM Noticias ORDER BY ID ASC"; $result=mysql_query($sql)or die (mysql_error()); echo "<form action='' method='post'>"; echo "<select name='select' id='mysubmit'>"; while($listar=mysql_fetch_array($result)){ $id=$listar['ID']; $subject=$listar['Subject']; $noticia=$listar['Noticia']; echo "<option value='.$id.' id='mysubmit' name='sub'>$subject</option>"; } echo "</select>"; echo "<input type='submit' id='mysubmit' value='Escolher' name='submitesc'>"; echo "</form>"; mysql_close(); ?> <form method="POST" action=""> Subject: <input type="text" id="mysubmit" name="subalt" value=<?php echo $subjectesc ?> ><br> <textarea name="notalt" id="mysubmit" cols="40" rows="5"> <?php echo $noticiaesc ?> </textarea><br> <input type="submit" name='submitalt' id="mysubmit" value='Alterar noticia'> <input type="submit" name='submitdel' id="mysubmit" value='Apagar noticia'> </form> <?php echo $notMessage ?> Nas variaveis $subjectesc e $noticiaesc não está a aparecer valor nenhum..Podem dar uma ajuda pfv. Link to post Share on other sites
kareka 0 Posted April 4, 2011 Report Share Posted April 4, 2011 Verifica lá os nomes das variáveis outra vez <?php include("assinatura.txt"); ?> Link to post Share on other sites
snakept 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 Hepa não faço a minima, ando a ver isto a horas seguidas a fio e não estou mesmo a ver o que possa ser, podes dar uma pista mais especifica Link to post Share on other sites
kareka 0 Posted April 5, 2011 Report Share Posted April 5, 2011 Não tens o nomes destas variaveis errados ? $subject=$listar['Subject']; $noticia=$listar['Noticia']; <?php include("assinatura.txt"); ?> Link to post Share on other sites
snakept 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 Nop, aí funciona bem. Vai buscar os registos dos campos Subject e Noticia da tabela noticias do SQL e aloja-os nas variaveis $subject e $noticia. Ele lista correctamente na combobox a listagem dos registos que estão no SQL. Eu quero é transferir da combobox para caixa de texto com o name="subalt", assim que pressiono o botão com o name='submitesc'(de acordo com o que estiver selecionado na combobox). Mas ao cliquar no botão, não transfere registo nenhum e o selected item fica sempre no primeiro registo. Link to post Share on other sites
kareka 0 Posted April 5, 2011 Report Share Posted April 5, 2011 Estás a fazer da echo da var $subjectesc quando ela não existe . Onde é que lhe atribuis algum valor ? <?php include("assinatura.txt"); ?> Link to post Share on other sites
snakept 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 Cá em cima, quando pressiono o botão com o nome 'submitesc': elseif (isset($_POST['submitesc'])) { if(isset($_POST['submitalt'])){ $notMessage="Em desenvolvimento..."; }elseif (isset($_POST['submitdel'])){ $notMessage="Em desenvolvimento..."; } $sql="SELECT * FROM Noticias WHERE ID='$select'"; $result=mysql_query($sql); $row=mysql_fetch_row($result); $subjectesc=$row['Subject']; $noticiaesc=$row['Noticia']; $notMessage=$select; } Link to post Share on other sites
kareka 0 Posted April 5, 2011 Report Share Posted April 5, 2011 Onde é que tu sacas o select ? <?php include("assinatura.txt"); ?> Link to post Share on other sites
snakept 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 $select=$_POST['select']; Que vai buscar ao <select name='select' id='mysubmit'> E depois mostra na variavel $notMessage=$select; Na variavel $notMessage é apenas para eu confirmar que o registo é puxado. Nesta variavel é mostrado o ID que seleciono, neste caso deveria mostrar tambem todo o registo pertencente ao ID com o codigo: $sql="SELECT * FROM Noticias WHERE ID='$select'"; $result=mysql_query($sql); $row=mysql_fetch_row($result); $subjectesc=$row['Subject']; $noticiaesc=$row['Noticia']; $notMessage=$select; Certo? Ou estou a falhar em algum lado? Link to post Share on other sites
kareka 0 Posted April 5, 2011 Report Share Posted April 5, 2011 Antes de mais não vi esse $_POST em nenhum lado mas posso não ter reparado também . Já agora (pode não ser disto) mas põe o name e o id iguais no select , just in case <?php include("assinatura.txt"); ?> Link to post Share on other sites
snakept 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 Alterar o id do select apenas me altera o tipo de letra dos valores apresentados dentro do select. Este id é relativo as configurações que estão na folha css Link to post Share on other sites
kareka 0 Posted April 5, 2011 Report Share Posted April 5, 2011 Então põe o nome igual ao id , eu já tive problemas com isso quando estava a tentar sacar variáveis de forms . <?php include("assinatura.txt"); ?> Link to post Share on other sites
snakept 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 Nop, está igual. Link to post Share on other sites
kareka 0 Posted April 5, 2011 Report Share Posted April 5, 2011 Vai fazendo echo's para mostra o valor das variáveis e já agora faz um echo da tua query e testa no mysql para erros . <?php include("assinatura.txt"); ?> Link to post Share on other sites
snakept 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 Descobri o erro: <?php include ("connect.php"); $sql2="SELECT * FROM noticias WHERE ID='$id'"; $search=mysql_query($sql2); if ($_SERVER['REQUEST_METHOD']=='POST'){ if($search){ if(isset($_POST['submitesc'])){ $row=mysql_fetch_row($search); if ($row){ $subjectesc=$row['Subject']; $noticiaesc=$row['Noticia']; }else $noticiaesc="BD vazia"; $notMessage=$select; $noticiaesc=$search; } } } ?> Ao fazer echo da variavel $search que faz a query ao SQL dá : "Resource id #10"; Já é uma ajuda, mas continuo sem perceber qual é o erro Link to post Share on other sites
snakept 0 Posted April 5, 2011 Author Report Share Posted April 5, 2011 Descobri. Estava a fazer echo mysql_query por isso é que dava resource id. Foi só alterar o id das rows e ficou OK. Aqui está o codigo depois de resolvido para quem precise. include ("connect.php"); $sql2="SELECT * FROM noticias WHERE ID='$select'"; $search=mysql_query($sql2); if ($_SERVER['REQUEST_METHOD']=='POST'){ if($search){ if(isset($_POST['submitesc'])){ $row=mysql_fetch_row($search); if ($row){ $noticiaesc=$row[1]; $subjectesc=$row[2]; }else $noticiaesc="BD vazia"; } } } Obrigado pela ajuda kareka. Abraço Link to post Share on other sites
kareka 0 Posted April 5, 2011 Report Share Posted April 5, 2011 Espero ter sido mesmo uma ajuda , na boa <?php include("assinatura.txt"); ?> Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now