PHP/MySQL Table Update Issue -
alright have mysql database setup bunch of usernames, passwords, , other details. have 1 "administrator" section on site want able edit users information when needed.
so far have page called president_admin.php (code below), displays table name, username, , password of user, link update user. have table working correctly, displays information , all. update link, takes users id in table, sends page called studentedit.php (code below) retrieves id of user, , fills 3 text fields username, password, , name fine. works.
so fill out fields test , see if let me update variables in database, , press submit goes page called studentupdate.php (code below). page connects database, , uses , if display , error if not update. goes through fine , says update.... indeed not. can @ code , me out.
i've been stuck on hours. please note code snip, secured , has more page. i'm teen trying best here... sorry if scrutiny coding world.
president_admin.php
<?php session_start(); $host="localhost"; // host name $username=""; // mysql username $password=""; // mysql password $db_name=""; // database name $tbl_name="members"; // table name // connect server , select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select db"); $sql="select * $tbl_name"; $result=mysql_query($sql); ?> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="100%" border="1" cellspacing="0" cellpadding="3"> <tr> <td align="center"><strong>name</strong></td> <td align="center"><strong>username</strong></td> <td align="center"><strong>password</strong></td> <td align="center"><strong>update</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['name']; ?></td> <td><? echo $rows['username']; ?></td> <td><? echo $rows['password']; ?></td> <td align="center"><a href="studentedit.php?id=<? echo $rows['id']; ?>">update</a></td> </tr> <?php } ?> <?php mysql_close(); ?> </table> </td> </tr> </table>
studentedit.php
<?php session_start(); $host="localhost"; // host name $username=""; // mysql username $password=""; // mysql password $db_name=""; // database name $tbl_name="members"; // table name // connect server , select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select db"); // value of id sent address bar $id=$_get['id']; // retrieve data database $sql="select * $tbl_name id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="studentupdate.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> </tr> <tr> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> <td align="center"> </td> </tr> <tr> <td align="center"> </td> <td align="center"><strong>name</strong></td> <td align="center"><strong>username</strong></td> <td align="center"><strong>password</strong></td> </tr> <tr> <td> </td> <td align="center"> <input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"> </td> <td align="center"> <input name="username" type="text" id="username" value="<? echo $rows['username']; ?>" size="15"> </td> <td> <input name="password" type="text" id="password" value="<? echo $rows['password']; ?>" size="15"> </td> </tr> <tr> <td> </td> <td> <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"> </td> <td align="center"> <input type="submit" name="submit" value="submit"> </td> <td> </td> </tr> </table> </td> </form> </tr> </table><?php // close connection mysql_close(); ?>
studentupdate.php
<?php session_start(); $host="localhost"; // host name $username=""; // mysql username $password=""; // mysql password $db_name=""; // database name $tbl_name="members"; // table name // connect server , select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select db"); // update data in mysql database $sql="update members set name='$name', username='$username', password='$password' id='$id'"; $result=mysql_query($sql); // if updated. if($result){ echo "successfully updated"; echo "<br>"; echo "<a href='president_admin.php'>return dashboard</a>"; } else { echo "error, went wrong."; } ?>
add studentupdate.php if($_post['submit'])) { $name=$_post['name']; $username=$_post['username']; $password=$_post['password']; $id=$_post['id']; //your script } , change variable name in connection script $username=""; // mysql username $password="";
Comments
Post a Comment