Jump to content

Mh. Updating database?


Garloz

Recommended Posts

Hi, im so stuck in here. Like i have code like this(called update.php).

<?php 

$username = $_POST['user'];
$password = $_POST['vanaparool'];
$password2 = $_POST['uusparool'];
$con = mysql_connect("localhost","baasiksutaja","parool");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// vastus
mysql_select_db(baasinimi);
$uuendamine = mysql_query("SELECT * FROM testtable WHERE (username='$username' AND password='$password')");
while($row = mysql_fetch_array($uuendamine)) {
echo "Your " . $row['username'] . " password is updated!" ;
}
// uuendamine
mysql_select_db(baasinimi);
mysql_query("UPDATE testtable set password='$password2'");

mysql_close($con);
?>  

And my other code:

 <form method="post" action="update.php">
        <table width="80%" border="0" align="center" cellpadding="3" cellspacing="3" class="forms">
          <tr> 
            <td width="31%">Username:</td>
            <td width="69%"><input name="user" type="text" id="user"></td>
          </tr>
	  <tr> 
            <td width="31%">Old password:</td>
            <td width="69%"><input name="vanaparool" type="password" id="vanaparool"></td>
          </tr>
          <tr> 
            <td>New password:</td>
            <td width="69%"><input name="uusparool" type="password" id="uusparool"></td>
          </tr>
        </table>
        <p align="center"> 
          <input name="uuenda" type="submit" id="uuenda" value="Update">
        </p>

 

My problem is: How could i set them in one page?

* If i "update" then if it's not succesfully updated - prints me error(not blank page).

* So if I submit - then it shows above the "form" - "Successfully updated" or "Error! Check username and password".

Link to comment
https://forums.phpfreaks.com/topic/196861-mh-updating-database/
Share on other sites

Here you go...

 

<?php 

if(isset($_POST['user']) && isset($_POST['vanaparool']) && isset($_POST['uusparool'])) {

$username = $_POST['user'];
$password = $_POST['vanaparool'];
$password2 = $_POST['uusparool'];
$con = mysql_connect("localhost","baasiksutaja","parool");

if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// vastus
mysql_select_db(baasinimi);
$uuendamine = mysql_query("SELECT * FROM testtable WHERE (username='$username' AND password='$password')");
while($row = mysql_fetch_array($uuendamine)) {
echo "Your " . $row['username'] . " password is updated!" ;
}
// uuendamine
mysql_select_db(baasinimi);
mysql_query("UPDATE testtable set password='$password2'");

mysql_close($con);

}

?>

    <form method="post" action="">
        <table width="80%" border="0" align="center" cellpadding="3" cellspacing="3" class="forms">
          <tr> 
            <td width="31%">Username:</td>
            <td width="69%"><input name="user" type="text" id="user"></td>
          </tr>
	  <tr> 
            <td width="31%">Old password:</td>
            <td width="69%"><input name="vanaparool" type="password" id="vanaparool"></td>
          </tr>
          <tr> 
            <td>New password:</td>
            <td width="69%"><input name="uusparool" type="password" id="uusparool"></td>
          </tr>
        </table>
        <p align="center"> 
          <input name="uuenda" type="submit" id="uuenda" value="Update">
        </p>
</form>

 

The form now posts to itself. When the page loads again it checks for POST data and if there is some it runs the code to update the users details.

Are you sure the database login details are correct?

 

Yes. Maybe I have some bug in this code? I want like, user writes his username- password - new password to form. PHP check if the username with that password exist - if yes, then update the old password with new one for this user. And then displays above the form, like "Your password is changed" and if the username-password are incorrect then "Username/Password doesnt match".

 

Ah yeah, should I add mysql_real_escape_string too?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.