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
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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.