Why I cannot update mysql database username and password?
I am newbie in php language, I trying to do a simple login page , index page, and logout page, and now i trying to do change login password page, but it does not work in mysql , my coding have no any error but in mysql data it not change to what I updated in my update php form.
here is my code ,
<?php
//including the database connection file
$host="localhost"; // Host name
$username="paapuu_user"; // Mysql username
$password="mn78k1a2"; // M开发者_运维百科ysql password
$db_name="paapuu"; // Database name
$tbl_name="admin"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if(isset($_POST['update']))
{
//here the id that we post is the same id that we get from url
//id indicates the id of this data which we are editing
//id is unique and a particular id is associated with particular data
$id1 = $_POST['id'];
$username1=$_POST['username'];
$password1=$_POST['password'];
//updating the table
$result=mysql_query("UPDATE admin SET username='$username1', password='$password1' WHERE id=$id1");
echo "<script language='javascript' type='text/javascript'>";
$error1="<p>Update successfully!</p>";
echo "alert($error1); window.location ='viewpassword.php'";
echo "</script>";
}
It would be nice to see your actual form as well.
Are you sure it takes you to inside of the isset($_POST['update']);
?
Also you could try
$result = mysql_query("UPDATE admin
SET username='$username1', password='$password1'
WHERE id=$id1")
or die(mysql_error());
To see if there's actually any problem with that query of yours.
Try surrounding the variable $id1 with single quotes:
$result = mysql_query("UPDATE admin SET username='$username1', password='$password1' WHERE id='$id1'");
It will prevent your query from having an error when no id is supplied, just like this:
UPDATE admin SET username='$username1', password='$password1' WHERE id=' ';
When no single quote is used, it will work this way:
UPDATE admin SET username='$username1', password='$password1' WHERE id=;
And I suggest you use your variable $tbl_name or just remove it from variable declarations if you won't use it in your queries. Hope this works!
It looks like you forgot to put single quotes around WHERE id=$id1 for $id1
also if that does not fix the problem try replacing the single quotes on you variables to
something like'".$username1."'
精彩评论