Page 1 of 1

still cannot update records :(

Posted: Sat Sep 15, 2007 4:58 pm
by eeau1973
Hi folks ... Would somebody check my code to tell me what's wrong ... :( I cannot update records ....

this is the first file :

Code: Select all

<html>
<head>
<title>Actualiza Status de Guias</title>
</head>
<body bgcolor="white">
<form method="POST" action="modifica1.php">
  <table>
    <col span="1" align="right">
    <tr> 
      <td><font color="blue" size="1" face="Verdana, Arial, Helvetica, sans-serif">N&uacute;mero 
        de Guia a Actualizar:</font></td>
      <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
        <input name="numguia" type="text" id="numguia" size=100>
        </font></td>
    </tr>
    <tr> 
      <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
        <input type="submit" value="Consulta Guia">
        </font></td>
    </tr>
  </table>
</form>
</body>
</html>

And then here is modifica1.php ...

Code: Select all

<?php
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;
$db1=mysql_connect(localhost, user, passwd);
mysql_select_db("mydatabase");
$query="SELECT * FROM datos WHERE numguia = \"".$formVars["numguia"]."\"";
$result=mysql_query($query);
$row=mysql_fetch_array($result);
$formVars = array();
$formVars["numguia"]=$row["numguia"];
$formVars["origenpais"]=$row["origenpais"];
$formVars["origenestado"]=$row["origenestado"];
$formVars["origenciudad"]=$row["origenciudad"];
mysql_close($db1);
?>
<html>
<head>
<title>Actualización de Guias</title>
</head>
<body bgcolor="white">
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"><form method="post" action="modifica2.php"> <table> 
</font> 
<col span="1" align="right">
<tr> 
  <td><font color="blue" size="1" face="Verdana, Arial, Helvetica, sans-serif">N&uacute;mero 
    de Guia:</font></td>
  <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
    <input type="text" name="numguia" 
value="<? echo $formVars["numguia"]; ?>" size=100>
    </font></td>
</tr>
<tr> 
  <td><font color="blue" size="1" face="Verdana, Arial, Helvetica, sans-serif">Pais 
    de Origen:</font></td>
  <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
    <input type="text" name="model" 
value="<? echo $formVars["origenpais"]; ?>" size=100>
    </font></td>
</tr>
<tr> 
  <td><font color="blue" size="1" face="Verdana, Arial, Helvetica, sans-serif">Estado 
    de Origen:</font></td>
  <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
    <input type="text" name="addr" 
value="<? echo $formVars["origenestado"]; ?>" size=100>
    </font></td>
</tr>
<tr> 
  <td><font color="blue" size="1" face="Verdana, Arial, Helvetica, sans-serif">Ciudad 
    de Origen:</font></td>
  <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
    <input type="text" name="zip" 
value="<? echo $formVars["origenciudad"]; ?>" size=100>
    </font></td>
</tr>
<tr> 
  <td>&nbsp;</td>
  <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp; </font><font size="1" face="Verdana, Arial, Helvetica, sans-serif">&nbsp; 
    </font></td>
</tr>
<tr>
  <td>&nbsp;</td>
</tr>
<tr> 
  <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
    <input type="submit" value="Submit">
    </font></td>
</tr>
</body>
</html>

And finally here is modifica2.php

Code: Select all

<html>
<head>
<title>Actualización de Guias</title>
</head>
<body bgcolor="white">
<?php
foreach($HTTP_POST_VARS as $varname => $value)
$formVars[$varname]=$value;
$db1=mysql_connect(localhost, user, password);
mysql_select_db("mydatabase");
echo "Record updated<br><a href=\"modifica.php\">click here</a> to update another record<br>";
$query="UPDATE tablename set ".
"numguia= \"".$formVars["numguia"]."\",".
"origenpais= \"".$formVars["origenpais"]."\",".
"origenestado= \"".$formVars["origenestado"]."\",".
"origenciudad= \"".$formVars["origenciudad"]."\",".
"\" WHERE numguia = \"".$formVars["numguia"]."\"";
mysql_query($query);
mysql_close($db1);
?>
</body>
</html>
when i check the record that i modify to update there is no record affected :(

Thanks a lot !!

greetings

Posted: Sat Sep 15, 2007 5:05 pm
by s.dot
Make use of mysql_error() and turn on error reporting, if it isn't on. If that doesn't work echo or var_dump() some of your variables before your queries.

Some standard debugging stuff. :)

cannot use Make use of mysql_error()

Posted: Sat Sep 15, 2007 5:41 pm
by eeau1973
Sorry for the question, but

.. how can i use Make use of mysql_error() ?

thanks !~

Posted: Sat Sep 15, 2007 5:46 pm
by s.dot
Like this:

Code: Select all

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

Posted: Mon Sep 17, 2007 4:41 am
by Hemlata
Hello eeau1973,


While updating records, you are using query in modifica2.php as..

Code: Select all

$query="UPDATE tablename set ".
"numguia= \"".$formVars["numguia"]."\",".
"origenpais= \"".$formVars["origenpais"]."\",".
"origenestado= \"".$formVars["origenestado"]."\",".
"origenciudad= \"".$formVars["origenciudad"]."\",".
"\" WHERE numguia = \"".$formVars["numguia"]."\"";
Is the table to be updated having name as 'tablename' or you missed to correct it to be 'datos'.

If the above is not the reason then what error you are getting while updating records?

Regards,

Posted: Mon Sep 17, 2007 5:20 am
by CoderGoblin
Ok this is where I get flamed but never mind...
scottayy wrote:Like this:

Code: Select all

mysql_query($query) or die(mysql_error());
Whilst this is a recognised and common practice, it is (In my opinion) not a good idea to have mysql errors output directly to everybody on a live system. Geeks could possibly use it to figure out your database structure, non geeks find it meaningless and annoying. Generally users should see a message such as 'A database error has occured. This error has been logged and will be enacted upon as soon as possible'. If possible you should only output errors if a the user is a specific user level (if applicable for the application) or another possible solution is to check a $_SESSION['debug'] value..

You can use the fact that most mysql commands where you could get an error return false if they fail so a simple

Code: Select all

if ($result=mysql_query($query)) {
  if ($_SESSION['debug']) {
     echo mysql_error();
  } else {
     echo 'A database error has occured. This error has been logged and will be enacted upon as soon as possible';
  }
}
should work.

The question is then what do you do afterwards, For a live system recovery may be an desired option rather than a simple "die".

Posted: Mon Sep 17, 2007 6:27 am
by superdezign
CoderGoblin wrote:Ok this is where I get flamed but never mind...
It is. :P
There's little to no reason for us to over complicate things for programmers that seem to be new to PHP. However, I do agree with the idea of having a debug mode that is natively programmed into the live site as well. I think that I just may implement a debug mode for administrative users. ^_^

Posted: Mon Sep 17, 2007 7:12 am
by CoderGoblin
superdezign wrote:There's little to no reason for us to over complicate things for programmers that seem to be new to PHP.
Surely better to teach good style at the outset... What you start learning is hard to unlearn especially if it is "quick" and normally hidden. Same principle as using mysql_real_escape_string with $_GET.

Posted: Mon Sep 17, 2007 7:27 am
by volka
I'd rather continue using or die(mysql_error()) in the "PHP - Code" forum and have an article with the working title "Why we sometimes use or die(mysql_error()) but discourage you from doing the same" (written by someone who's not me of course ;)) that I can link to.

Posted: Mon Sep 17, 2007 7:33 am
by superdezign
CoderGoblin wrote:Surely better to teach good style at the outset... What you start learning is hard to unlearn especially if it is "quick" and normally hidden.
Well, for the same reason we don't tell every programmer to use templates or object-oriented programming, it's simply that the best way to learn is to take your time. I'm sure all of us have used "or die(mysql_error())" and it just simplified the learning process for us.

Posted: Mon Sep 17, 2007 7:56 am
by aceconcepts
I guess Scottyayy simply mentioned it so that we could possibly learn something about the nature of the error at hand.