We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion
Page 2 of Article: Select and update querry
I need to, select data from a table and then make changes and update is the same tabele and another table. Right now my code is very simple and is giving some erros, right now, he just search the table. can you hepl me? <?php //conecção ao servidor mysql_connect("localhost", "root")or…

I still think you are using $_POST a bit...awkwardly...

I also think youre using MySQL very ineffeciently... you can make combined queries, I feel, for much of what you are doing.

However, line 28 is the only query that matches your error. On line 27 and 28, your comma is being placed improperly, I think... to double check, change line 31 to:

mysql_query($sql2a) or die("THIS IS MY ERROR!");

If you get that message, then you need to change your iteration, and make it happen one step earlier.

Ryan

ryantroop
Junior Poster
190 posts since Jun 2012
Reputation Points: 57
Solved Threads: 24
Skill Endorsements: 0

you are right, i'm new to php and mysql, that's why i need help, and post every modification i made to the code. if you have some time, can you help me?

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

It will come with practice...

I think youre doing it right... first, get it working the way you want. Then we can work on refactoring your code and making it "pretty." It's hard when you are plopped into advanced (heck, Im not even that advanced) code, without knowing the basics of what it is doing.

Did you find the error in the place I said? Or is it somewhere else?

ryantroop
Junior Poster
190 posts since Jun 2012
Reputation Points: 57
Solved Threads: 24
Skill Endorsements: 0

it seems to be something else...

( ! ) SCREAM: Error suppression ignored for
( ! ) Notice: Undefined index: check in C:\wamp\www\teste up\update.php on line 7
Call Stack

Time Memory Function Location

1 0.0008 149672 {main}( ) ..\update.php:0
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The first lines are nothing major. If you can configure your server to disable error reporting, they will go away. Again, it comes down to your code being "ugly" and the parser is very unhappy with you. (Unless someone else knows something I dont).

The second problem... which file is update.php?

It seems you have a bad query somewhere else...

ryantroop
Junior Poster
190 posts since Jun 2012
Reputation Points: 57
Solved Threads: 24
Skill Endorsements: 0

update.php is the 3th file. in the botom of 1st page I pasted the code of 3 files.

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

I think i need to express myself a litle better in order to get your help.

This is a form where i search for an order nunber, then i can change some data and update the rows that i've cheked. im trying a lot of possibilities, but i need to do it.

in your opinion, wat's the best way?

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

If it were me doing this, it wouldn't be too terribly different, it would just be shorter and a bit more readable. On top of that, I would sanitize my queries and control type setting from the user, so that someone couldnt pass '$10000' when theyre supposed to be putting in their address. Also, allowing $_POST to be placed directly in your query allows hackers/stupid people to send a query to either get all info from your table, or worse destroy your table. I also don't know how your SQL tables were set up, nor what permissions you allow for the user.

That said, your whole project could be done with a single PHP page. There is no need to spread it out over so many, but I understand that compartmentalizing the way you are helps to keep references and purpose to each page. That's a style issue more than a "this is the best way."

Lastly, I would probably break up all these into functions and import the repetetive connection data as necessary. For future reference, look into how .htaccess is configured with PHP (so you can create a protected directory), and how you can store passwords, database names, encryption keys, functions, and other includes that you don't want made available to the public, in a directory that is not accessable from outside the server or root FTP access.

As for the problem you are encountering --
I've personally never encountered the error you are getting. Google seems to say it's a problem with your query - in that it is asking your SQL DB to do something that it knows it shouldnt be doing, or it's overloaded due to the data it is sending back. I'm not sure where in your code that is happening, other than the SQL getting upset that you are making rapid queries, but you have more on a previous page and it causes no fault. So, Im not entirely sure..

Also, since you're on a Windows server, there may be some configurations I'm not very familliar with (I learned, and continue to use Linux for web dev) but I don't think they would be so dissimilar... hopefully someone else will chime in and help out.

ryantroop
Junior Poster
190 posts since Jun 2012
Reputation Points: 57
Solved Threads: 24
Skill Endorsements: 0

Trying do it again, this time, i chose the hard way, 1 query per row, only 6 rows are alowed to update at once, so no problem with time. as usual.... PROBLEMS
the idea was simple, the input fields with the table values. i've done that before, this time something went wrong.... agian Help needed

1st visual.php

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Ficha de entrada de material</title>
        <meta name="alex" content="sermail" />
        <link rel="stylesheet" type="text/css" href="styles/styles.css" />
        <script type="text/javascript">
 function printpage()
   {
   window.print()
   }
 </script>
    <script>
      function showMe (it, box) {
       var vis = (box.checked) ? "block" : "none";
      document.getElementById(it).style.display = vis;
      }
     </script>


<style type="text/css" media="print">
   @page land {size: landscape;}
   .printbutton {
   visibility: hidden;
   display: none;
   }
   .validado {
   visibility: hidden;
   display: none;
   }
    .background{ color: #0000;}
    .page landscape {size: landscape;}
    .page
    { bgcolor: #0000; }
    .h1{color: #000;
    font-family: Arial, sans-serif;
    font-size: 18px;
    font-weight: bold;
    text-align: left;}
    .h2{color: #000;
    font-weight: normal;
    text-align: center;
    font-family: Arial, sans-serif;
    font-size: 16px;}
.land{filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=2)}
h4 {
    color: #000;
    font-weight: normal;
    text-align: center;
    font-family: Arial, sans-serif;
    font-size: 18px;

</style>

 <script language="javascript">
             window.resizeTo(870,650, );
             </script>  


<?php
mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");

$nordem = $_POST['nordem']; 

$query="SELECT * FROM entrada where nordem_n = '" . mysql_real_escape_string($nordem) . "'";
$result=mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);

if (!$result) {
    $message  = 'N.º de Ordem inexistente ' . mysql_error() . "\n";
    die($message);
}
 while($rows=mysql_fetch_array($result))


?><body class="land" bgcolor= #00814B >
    <div class="body">
        <div class="content">

<form action="act.php" method="post">

<div class="tamanho" style="width:870px">

 <table width="870px" > <tr><td><img class="img" src="imas/sermaillogo.png"></br>
     <input type="submit" class="printbutton" value="Actualizar">
     <input type="button" class="printbutton" value="Imprimir"       onclick="printpage()" />
     <INPUT type="button" class="printbutton" value="Fechar"         onClick="window.close()">
</table>  

</div></h1></td>
<table><tr>
<td> <h1> Data:</h1></td><td> <input type="text" size="10" name="data"                  value=<?php mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");$result1="SELECT data FROM ordem where nordem = '" . mysql_real_escape_string($nordem) . "'";
$o1=mysql_query($result1)or die(mysql_error());
if ($row = mysql_fetch_array($o1)) {
    echo $row['data'];
    }
    else {
    echo 'No record found';
    }  ?>/></td>
<td><h1>N.º Ordem:</h1>  </td>  <td><input type="text" size="10" name="nordem"          value=<?php mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");$result1="SELECT nordem FROM ordem where nordem = '" . mysql_real_escape_string($nordem) . "'";
$o1=mysql_query($result1)or die(mysql_error());
if ($row = mysql_fetch_array($o1)) {
    echo $row['nordem'];
    }
    else {
    echo 'No record found';
    }   ?>/></td>
<td><h1>Fornecedor:</h1> </td> <td><input type="text" size="10" name="fornecedor"       value=<?php mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");$result1="SELECT fornecedor FROM ordem where nordem = '" . mysql_real_escape_string($nordem) . "'";
$o1=mysql_query($result1)or die(mysql_error());
if ($row = mysql_fetch_array($o1)) {
    echo $row['fornecedor'];
    }
    else {
    echo 'No record found';
    }  ?>/></td>
<td> <h1>Guia n.º:</h1>   </td>   <td><input type="text" size="10'" name="guian"        value=<?php  mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");$result1="SELECT guian FROM ordem where nordem = '" . mysql_real_escape_string($nordem) . "'";
$o1=mysql_query($result1)or die(mysql_error());
if ($row = mysql_fetch_array($o1)) {
    echo $row['guian'];
    }
    else {
    echo 'No record found';
    }  ?>/></td>
 </TR>
 <tr>
<td><h1>N.º Cliente:</h1></td><td><input type="text" size="10"name="ncliente"           value=<?php mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");$result1="SELECT ncliente FROM ordem where nordem = '" . mysql_real_escape_string($nordem) . "'";
$o1=mysql_query($result1)or die(mysql_error());
if ($row = mysql_fetch_array($o1)) {
    echo $row['ncliente'];
    }
    else {
    echo 'No record found';
    }   ?>/></td>
<td><h1>Cliente:</h1>        </td>    <td><input type="text" size="15"name="cliente"    value=<?php mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");$result1="SELECT cliente FROM ordem where nordem = '" . mysql_real_escape_string($nordem) . "'";
$o1=mysql_query($result1)or die(mysql_error());
if ($row = mysql_fetch_array($o1)) {
    echo $row['cliente'];
    }
    else {
    echo 'No record found';
    }  ?>/></td>
</tr></table>
     <table border style="1" color="000" cellspacing=0  id="produtos"  >
 <tr ><td></td>
 <td></td>
 <td ><h2>Cód. <br/>Produto </h2></td>
 <td><h2>Cód. Cliente <br>1-Lote</h2></td>
 <td><h2>Cód. Cliente<br>2-validade</h2></td>
 <td><h2>Designação</h2></td>
 <td><h2>Filtro 1 <br> Unidade de <br>negócio</h2></td>
 <td><h2>Familia/Marca</h2></td>
 <td><h2>Quant.<br> Guia</h2></td>
 <td><h2>Quant. <br>Conferida</h2></td>
 <td><h2>Atado</h2></td>
 <td><h2>Posição</h2></td>
   </tr>


<tr>
 <td><input type="hidden" name="id1" value=<?php echo $rows['id'];?></td>
 <td><input class="printbutton" type="checkbox" name="check1"  value="1"  /></td>
 <td ><input type="text" size="9"name="codprod1"          value=<?php echo $rows['codprod'];?>/></td>
 <td ><input type="text" size="6"name="lote1"             value=<?php echo $rows['lote'];?>/></td>
 <td ><input type="text" size="6"name="val1"              value=<?php echo $rows['val'];?>/></td>
 <td ><textarea cols="20" rows="2" name="designacao1"     value=<?php echo $rows['designacao'];?>>

</textarea></td>
 <td ><input type="text" size="8"name="filtro1"           value=<?php echo $rows['filtro'];?>/></td>
 <td ><input type="text" size="10"name="familiamarca1"    value=<?php echo $rows['familiamarca'];?>/></td>
 <td ><input type="text" size="6"name="quantguia1"        value=<?php echo $rows['quantguia'];?>/></td>
 <td ><input type="text" size="6"name="quantconf1"        value=<?php echo $rows['quantconf'];?>/></td>
 <td ><input type="text" size="6"name="atado1"            value=<?php echo $rows['atado'];?>/></td>
 <td ><input type="text" size="6"name="posicao1"          value=<?php echo $rows['posicao'];?>/></td>
 </tr>

<tr>
<td><input type="hidden" name="id2" value=<?php echo $rows['id'];?></td>
 <td><input class="printbutton" type="checkbox" name="check2"  value="1"  /></td>
 <td ><input type="text" size="9"name="codprod2"          value=<?php echo $rows['codprod'];?>/></td>
 <td ><input type="text" size="6"name="lote2"             value=<?php echo $rows['lote'];?>/></td>
 <td ><input type="text" size="6"name="val2"              value=<?php echo $rows['val'];?>/></td>
 <td ><textarea cols="20" rows="2" name="designacao2"     value=<?php echo $rows['designacao'];?>>

</textarea></td>
 <td ><input type="text" size="8"name="filtro2"           value=<?php echo $rows['filtro'];?>/></td>
 <td ><input type="text" size="10"name="familiamarca2"    value=<?php echo $rows['familiamarca'];?>/></td>
 <td ><input type="text" size="6"name="quantguia2"        value=<?php echo $rows['quantguia'];?>/></td>
 <td ><input type="text" size="6"name="quantconf2"        value=<?php echo $rows['quantconf'];?>/></td>
 <td ><input type="text" size="6"name="atado2"            value=<?php echo $rows['atado'];?>/></td>
 <td ><input type="text" size="6"name="posicao2"          value=<?php echo $rows['posicao'];?>/></td>

 </tr>
 <tr>
 <td><input type="hidden" name="id3" value=<?php echo $rows['id'];?></td>
 <td><input class="printbutton" type="checkbox" name="check3"  value="1"  /></td>
 <td ><input type="text" size="9"name="codprod3"          value=<?php echo $rows['codprod'];?>/></td>
 <td ><input type="text" size="6"name="lote3"             value=<?php echo $rows['lote'];?>/></td>
 <td ><input type="text" size="6"name="val3"              value=<?php echo $rows['val'];?>/></td>
 <td ><textarea cols="20" rows="2" name="designacao3"     value=<?php echo $rows['designacao'];?>>

</textarea></td>
 <td ><input type="text" size="8"name="filtro3"           value=<?php echo $rows['filtro'];?>/></td>
 <td ><input type="text" size="10"name="familiamarca3"    value=<?php echo $rows['familiamarca'];?>/></td>
 <td ><input type="text" size="6"name="quantguia3"        value=<?php echo $rows['quantguia'];?>/></td>
 <td ><input type="text" size="6"name="quantconf3"        value=<?php echo $rows['quantconf'];?>/></td>
 <td ><input type="text" size="6"name="atado3"            value=<?php echo $rows['atado'];?>/></td>
 <td ><input type="text" size="6"name="posicao3"          value=<?php echo $rows['posicao'];?>/></td>

 </tr>
 <tr>

 <td><input type="hidden" name="id4" value=<?php echo $rows['id'];?></td>
 <td><input class="printbutton" type="checkbox" name="check4"  value="1"  /></td>
 <td ><input type="text" size="9"name="codprod4"          value=<?php echo $rows['codprod'];?>/></td>
 <td ><input type="text" size="6"name="lote4"             value=<?php echo $rows['lote'];?>/></td>
 <td ><input type="text" size="6"name="val4"              value=<?php echo $rows['val'];?>/></td>
 <td ><textarea cols="20" rows="2" name="designacao4"     value=<?php echo $rows['designacao'];?>>

</textarea></td>
 <td ><input type="text" size="8"name="filtro4"           value=<?php echo $rows['filtro'];?>/></td>
 <td ><input type="text" size="10"name="familiamarca4"    value=<?php echo $rows['familiamarca'];?>/></td>
 <td ><input type="text" size="6"name="quantguia4"        value=<?php echo $rows['quantguia'];?>/></td>
 <td ><input type="text" size="6"name="quantconf4"        value=<?php echo $rows['quantconf'];?>/></td>
 <td ><input type="text" size="6"name="atado4"            value=<?php echo $rows['atado'];?>/></td>
 <td ><input type="text" size="6"name="posicao4"          value=<?php echo $rows['posicao'];?>/></td>

 </tr>
 <tr>
 <td><input type="hidden" name="id5" value=<?php echo $rows['id'];?></td>
 <td><input class="printbutton" type="checkbox" name="check5"  value="1"  /></td>
 <td ><input type="text" size="9"name="codprod5"          value=<?php echo $rows['codprod'];?>/></td>
 <td ><input type="text" size="6"name="lote5"             value=<?php echo $rows['lote'];?>/></td>
 <td ><input type="text" size="6"name="val5"              value=<?php echo $rows['val'];?>/></td>
 <td ><textarea cols="20" rows="2" name="designacao5"     value=<?php echo $rows['designacao'];?>>

</textarea></td>
 <td ><input type="text" size="8"name="filtro5"           value=<?php echo $rows['filtro'];?>/></td>
 <td ><input type="text" size="10"name="familiamarca5"    value=<?php echo $rows['familiamarca'];?>/></td>
 <td ><input type="text" size="6"name="quantguia5"        value=<?php echo $rows['quantguia'];?>/></td>
 <td ><input type="text" size="6"name="quantconf5"        value=<?php echo $rows['quantconf'];?>/></td>
 <td ><input type="text" size="6"name="atado5"            value=<?php echo $rows['atado'];?>/></td>
 <td ><input type="text" size="6"name="posicao5"          value=<?php echo $rows['posicao'];?>/></td>

 </tr>
 <tr>
 <td><input type="hidden" name="id6" value=<?php echo $rows['id'];?></td>
 <td><input class="printbutton" type="checkbox" name="check6"  value="1"  /></td>
 <td ><input type="text" size="9"name="codprod6"          value=<?php echo $rows['codprod'];?>/></td>
 <td ><input type="text" size="6"name="lote6"             value=<?php echo $rows['lote'];?>/></td>
 <td ><input type="text" size="6"name="val6"              value=<?php echo $rows['val'];?>/></td>
 <td ><textarea cols="20" rows="2" name="designacao6"     value=<?php echo $rows['designacao'];?>>

</textarea></td>
 <td ><input type="text" size="8"name="filtro6"           value=<?php echo $rows['filtro'];?>/></td>
 <td ><input type="text" size="10"name="familiamarca6"    value=<?php echo $rows['familiamarca'];?>/></td>
 <td ><input type="text" size="6"name="quantguia6"        value=<?php echo $rows['quantguia'];?>/></td>
 <td ><input type="text" size="6"name="quantconf6"        value=<?php echo $rows['quantconf'];?>/></td>
 <td ><input type="text" size="6"name="atado6"            value=<?php echo $rows['atado'];?>/></td>
 <td ><input type="text" size="6"name="posicao6"          value=<?php echo $rows['posicao'];?>/></td>
 </tr>

 </table>
  <?php
    mysql_close();

    ?>

2nd act.php

<?php
mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");

$nordem = $_POST['nordem']; 

$query="SELECT * FROM entrada where nordem_n = '" . mysql_real_escape_string($nordem) . "'";
$result=mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);

if ($_POST['check1'] == 0) {
$Sql1= " UPDATE entrada SET codprod = '$codprod1' ,lote = '$lote1' ,val = '$val1' ,designacao = '$designacao1' ,filtro = '$filtro1' ,familiamarca = '$familiamarca1' ,quantguia = '$quantguia1' ,quantconf = '$quantconf1' ,atado = '$atado1' ,posicao = '$posicao'  WHERE id = '$id1' ";
    mysql_query ($sql1) or die(mysql_error());
}
if ($_POST['check2'] == 0) {
$Sql2= " UPDATE entrada SET codprod = '$codprod2' ,lote = '$lote2' ,val = '$val2' ,designacao = '$designacao2' ,filtro = '$filtro2' ,familiamarca = '$familiamarca2' ,quantguia = '$quantguia2' ,quantconf = '$quantconf2' ,atado = '$atado2' ,posicao = '$posicao2'  WHERE id = '$id2' ";
    mysql_query ($sql2) or die(mysql_error());
}
if ($_POST['check3'] == 0) {
$Sql3= " UPDATE entrada SET codprod = '$codprod3' ,lote = '$lote3' ,val = '$val3' ,designacao = '$designacao3' ,filtro = '$filtro3' ,familiamarca = '$familiamarca3' ,quantguia = '$quantguia3' ,quantconf = '$quantconf3' ,atado = '$atado3' ,posicao = '$posicao'  WHERE id = '$id3' ";
    mysql_query ($sql3) or die(mysql_error());
}
if ($_POST['check4'] == 0) {
$Sql4= " UPDATE entrada SET codprod = '$codprod4' ,lote = '$lote4' ,val = '$val4' ,designacao = '$designacao4' ,filtro = '$filtro4' ,familiamarca = '$familiamarca4' ,quantguia = '$quantguia4' ,quantconf = '$quantconf4' ,atado = '$atado4' ,posicao = '$posicao'  WHERE id = '$id4' ";
    mysql_query ($sql4) or die(mysql_error());
}
if ($_POST['check5'] == 0) {
$Sql5= " UPDATE entrada SET codprod = '$codprod5' ,lote = '$lote5' ,val = '$val5' ,designacao = '$designacao5' ,filtro = '$filtro5' ,familiamarca = '$familiamarca5' ,quantguia = '$quantguia5' ,quantconf = '$quantconf5' ,atado = '$atado5' ,posicao = '$posicao'  WHERE id = '$id5' ";
    mysql_query ($sql5) or die(mysql_error());
}
if ($_POST['check6'] == 0) {
$Sql6= " UPDATE entrada SET codprod = '$codprod6' ,lote = '$lote6' ,val = '$val6' ,designacao = '$designacao6' ,filtro = '$filtro6' ,familiamarca = '$familiamarca6' ,quantguia = '$quantguia6' ,quantconf = '$quantconf6' ,atado = '$atado6' ,posicao = '$posicao'  WHERE id = '$id6' ";
    mysql_query ($sql6) or die(mysql_error());
}
?>
 <?php
    mysql_close();

    ?>
Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

BTw, i don´t want if after if, i want them independent. but it seems that the 2,3 4,5 and 6 depends on the first. who can i do that?

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

ok, first - no matter how many <? ?> you have on your page, you only need to connect once. Ideally in the original <?php .... ?> pair, but it persists on the page as a whole, regardless of embedded HTML.

line 68, 69, 71 - You are doing the same operation twice, and in fact you are ignoring data.
$num = mysql_num_rows($query); // this is meant to check the number of rows returned. That's it!

so if by calling that, and then checking if(!$result) {} you essentially just asked if anything was there, assign that number to $num. If something is there, then do something. And then you forget about $num.

it is usually used as:

$result = mysql_query($query);
$num = mysql_num_rows($result);
if ($num > 0) { //run my script } else { //oops! No data! }

Now, as for the rest of what you have going on.. I think you need to be a little more clear.

Are you having trouble with your data displaying properly? Or is it not updating properly with the 2nd page?

ryantroop
Junior Poster
190 posts since Jun 2012
Reputation Points: 57
Solved Threads: 24
Skill Endorsements: 0

i've trouble in bouth, file 1 don't show any records, and 2nd file don't update them...

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Ok.. so this is a basic structure that should get you going in the right direction (I hope).

<?php
$usr = 'sqlusr';
$pw = 'sqlpw';
$host = 'sqlhost';

$db = mysql_connect($host, $usr, $pw);
mysql_select_db('dbname');

$nordem = $_POST['nordem'];
$nordem = mysql_real_escape_string($nordem);

$query = "SELECT * FROM entrada WHERE nodem_n = '$nordem';";
$result = mysql_query($query);

while ($data = mysql_fetch_object($data)) {
?>
<table><form action='mypage.php' method='post'>
<tr>
 <td>Field 1</td><td>Field 2</td><td>Field 3</td>
</tr>
<tr>
 <td><? $data->col1; ?></td><td><? $data->col2; ?></td><td><? $data->col3; ?></td>
</tr>
<tr>
 <td><input type='submit' value='submit'></td><td><input type='hidden' name='id' value='<? $data->id; ?>'></td>
</tr>
</form>
</table>
<?
}
?>

Now, lets assume that there is only 1 entry, we should only get 1 form back. This is completely bare bones, and has no functionality other than sending a blank post to mypage.php.

For reference.. instead of making an array, I made an object called $data. Inside data, all the columns are stored by keyname. So, $data->id will give the value of the id column in your sql table.

Now, we can further configure our form, to make our default value of a text field.. so lets say we were doing 'Field 1' value...

on the next row, the first <td></td> we would put

<td><input type='textbox' name='whatever' value='<? data->whatever ?>'></td>

Then, when we hit submit, we now have a POST value of 'whatever' being sent to our mypage.php

Then, on mypage.php we can do something with it.

<? if(isset($_POST['whatever']){
//run some program/do sql query or update or whatever
}
?>

That's it... you will notice that even if we call back multiple results from our SQL query, since we used a while loop, we will print out multiple tables that will be identified by POST through id.. you will also note that this will limit you to only 1 update, as $_POST['id'] will be unique for each submit button. If you wanted to be able to change all the fields on all the results, you will have to either make the post a 2 dimensional array, or modify the name field for each element in the post using php. However, start with just 1 at a time, and work on making it work for that one form.

ryantroop
Junior Poster
190 posts since Jun 2012
Reputation Points: 57
Solved Threads: 24
Skill Endorsements: 0

that gave 2 erros that i will find out how to solve.. one of those, is more complicated..
...Warning:
mysql_fetch_object(): supplied argument is not a valid MySQL result resource C:\wamp\www\8.8\new.php on line 9

the other:
Notice: Undefined variable: dados in C:\wamp\www\8.8\new.php on line 9

but afterall it seams to be a good solution, the only problem is that i might need to update multiple rows...

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

supplied argument is not a valid MySQL result resource

Your query failed.

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

failed? why? righ't now i'm totaly confused....

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

If it is the query at line 12 above, probably because of the typo... I suggest you use the following when debugging:

$result = mysql_query($query) or die(mysql_error());
pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

$result = mysql_query($query) or die(mysql_error());

thanks, unfortunatly, after the modification, the same error presists, maybe this line of code isn't compatible with my mySql 5.5.24, or my php 5.4.3

Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

i was able to solve that problem, finaly|! now cames one even worst, the tables created by this files are empty, and i'm sore that i made the rigth search, and i can see that because the file repeats the line 6 times, same number of rows that i have.

<?php
mysql_connect("localhost", "root")or die("sem conecção ao servidor"); 
mysql_select_db("sermail")or die("Base de dados não foi encontrada");

$nordem = $_POST['nordem'];
$nordem = mysql_real_escape_string($nordem);
$query = "SELECT * FROM entrada WHERE nordem_n = '$nordem'";
    $result = mysql_query($query) or die(mysql_error());
while ($dados = mysql_fetch_object($result)) {
?>
<table><form action='mypage.php' method='post'>
<tr>
<td>Código Produto</td><td>Código Cliente</br>1-Lote</td><td>Código Cliente</br>2-Validade</td><td>Designação</td><td>Filtro 1</br> unidade de</br>Negocio</td><td>Familia/Marca</td><td>Quantidade Guia</td><Td>Quantidade conferida</td><td>Atado</td><td>Posição</td>
</tr>
<tr>
<td><?php $dados->codprod; ?></td><td><?php $dados->lote; ?></td><td><?php $dados->val; ?></td><td><?php $dados->designacao; ?></td><td><?php $dados->filtro; ?></td><td><?php $dados->familiamarca; ?></td><td><?php $dados->quantguia; ?></td><td><?php $dados->quantconf; ?></td><td><?php $dados->atado; ?></td>
<td><?php $dados->posicao; ?></td>
</tr>
<tr>
<td><input type='submit' value='submit'></td><td><input type='hidden' name='id' value='<?php $dados->id; ?>'></td>
</tr>
</form>
</table>
<?php
}
?>
Stardemos
Junior Poster
104 posts since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You forgot echo ...

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page generated in 0.1299 seconds using 2.8MB