Cannot update my table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Cannot update my table

Post by mariolopes »

I show all the records of my database and i want to update one value of that table
If the input Idade is different ' ' I need to update my table with the value wich is in the input field valor. Here is my code, plese tell me were I'm wrong.

Code: Select all

<?php
$mysql_id = mysql_connect('localhost', 'curso', '123');
mysql_select_db('vendas',$mysql_id);
/*---actualização da tabela*/
if($_POST['Idade']=''){
$query='update noite set idade=0';
$result=mysql_query($query);
}else{
$query='update noite set idade=$_POST["Valor"]';
$result=mysql_query($query);    
}
/*----fim da actualização da tabela*/
$query='Select * from noite';
$result=mysql_query($query);
?>
/*construçao da tabela */
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<table border="1">
<?php
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><?php print $row['Nome'];?></td>
<td><?php print $row['Idade'];?></td>
</tr>
<?php
}
?>
</table>
<form action="selecciona.php" method="post">
<p>Idade:<input type="text" name="Idade" value="" label="Idade"/></p>
<p>Valor:<input type="text" name="Valor" value="" /></p>
 <p><input type="submit" value="Enviar!" /></p>
 </form>
 
</html>
mariolopes
Forum Contributor
Posts: 102
Joined: Sun May 22, 2005 7:08 am

Re: Cannot update my table

Post by mariolopes »

Solved
<?php
$mysql_id = mysql_connect('localhost', 'curso', '123');
mysql_select_db('vendas',$mysql_id);
/*---actualização da tabela*/
if(isset($_POST['Idade']) || $_POST['Idade']&&""){
$query='update noite set idade='.$_POST["Idade"];
$result=mysql_query($query);
}
/*----fim da actualização da tabela*/
$query='Select * from noite';
$result=mysql_query($query);
?>
/*construçao da tabela */
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<table border="1">
<?php
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><?php print $row['Nome'];?></td>
<td><?php print $row['Idade'];?></td>
</tr>
<?php
}
?>
</table>
<form action="selecciona.php" method="post">
<p>Idade:<input type="text" name="Idade" value="" label="Idade"/></p>
<p><input type="submit" value="Enviar!" /></p>
</form>

</html>
Post Reply