still cannot update records :(

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
eeau1973
Forum Commoner
Posts: 27
Joined: Sat Aug 19, 2006 4:49 pm

still cannot update records :(

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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. :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
eeau1973
Forum Commoner
Posts: 27
Joined: Sat Aug 19, 2006 4:49 pm

cannot use Make use of mysql_error()

Post by eeau1973 »

Sorry for the question, but

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

thanks !~
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Like this:

Code: Select all

mysql_query($query) or die(mysql_error());
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Hemlata
Forum Commoner
Posts: 35
Joined: Mon Sep 10, 2007 5:40 am
Location: India
Contact:

Post 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,
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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".
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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. ^_^
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

I guess Scottyayy simply mentioned it so that we could possibly learn something about the nature of the error at hand.
Post Reply