adamjones Posted October 5, 2008 Share Posted October 5, 2008 OK. So I have a PHP form, to update my users information. It goes like so; editadmin.php (The first page an admin goes to, to update another users info) :- <?php $host="localhost"; $username="wowdream_domaine"; $password="mypassword"; $db_name="wowdream_domaine"; $tbl_name="members"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="400" border="1" cellspacing="0" cellpadding="3"> <tr> </tr> <tr> <td align="center"><strong>Username</strong></td> <td align="center"><strong>Password</strong></td> <td align="center"><strong>Rank</strong></td> <td align="center"><strong>Update</strong></td> <td align="center"><strong>Delete</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['username']; ?></td> <td>********</td> <td><? echo $rows['access']; ?></td> <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td> <td align="center"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete</a></td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> update.php (This displays an update form, depending on the user selected and their ID) :- <?php $host="localhost"; $username="wowdream_domaine"; $password="mypassword"; $db_name="wowdream_domaine"; $tbl_name="members"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $id=$_GET['id']; $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>Update data in mysql</strong> </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>Username</strong></td> <td align="center"><strong>Password</strong></td> <td align="center"><strong>Access</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="username" type="text" id="username" value="<? echo $rows['username']; ?>" size="15"></td> <td align="center"><input name="password" type="text" id="password" value="<? echo $rows['password']; ?>" size="15"></td> <td align="center"><input name="access" type="text" id="access" value="<? echo $rows['access']; ?>" 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> <? mysql_close(); ?> admin_ac.php (The information is posted here, and is updated in the database) :- <?php $host="localhost"; $username="wowdream_domaine"; $password="mypassword"; $db_name="wowdream_domaine"; $tbl_name="members"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="UPDATE $tbl_name SET username='$username', password='$password', access='$access' WHERE id='$id'"; $result=mysql_query($sql); if($result){ echo "Successful"; echo "<BR>"; echo "<a href='editadmin.php'>View result</a>"; } else { echo "ERROR"; } ?> OK. The problem is, it does everything fine, except when I change the info in the form, instead of updating, it echo's my error - Could anyone work out what's wrong with it? Cheers, Adam. Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/ Share on other sites More sharing options...
unkwntech Posted October 5, 2008 Share Posted October 5, 2008 1st - When you post code please place it between [ code] and [/ code] 2nd - change: else { echo "ERROR"; } with else { echo "ERROR:" . mysql_error(); } Then tell us what it says. Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657440 Share on other sites More sharing options...
budimir Posted October 5, 2008 Share Posted October 5, 2008 OK, another thing. Where are your variables declared???? $sql="UPDATE $tbl_name SET username='$username', password='$password', access='$access' WHERE id='$id'"; '$username' ???? '$password' ???? '$access' ???? '$id' ???? You variables are emtpy so nothing gets updated!!!! That's why you get error!!!! Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657455 Share on other sites More sharing options...
Acs Posted October 5, 2008 Share Posted October 5, 2008 And another thing! Try separating your presentation code from you php code! Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657457 Share on other sites More sharing options...
BloodyMind Posted October 5, 2008 Share Posted October 5, 2008 I'm with u ACS your code is totally unreadable Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657465 Share on other sites More sharing options...
adamjones Posted October 5, 2008 Author Share Posted October 5, 2008 Ok. Sorry guys- editadmin.php - <?php $host="localhost"; $username="wowdream_domaine"; $password="mypassword"; $db_name="wowdream_domaine"; $tbl_name="members"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td> <table width="400" border="1" cellspacing="0" cellpadding="3"> <tr> </tr> <tr> <td align="center"><strong>Username</strong></td> <td align="center"><strong>Password</strong></td> <td align="center"><strong>Rank</strong></td> <td align="center"><strong>Update</strong></td> <td align="center"><strong>Delete</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ ?> <tr> <td><? echo $rows['username']; ?></td> <td>********</td> <td><? echo $rows['access']; ?></td> <td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update[/url]</td> <td align="center"><a href="delete_ac.php?id=<? echo $rows['id']; ?>">delete[/url]</td> </tr> <?php } ?> </table> </td> </tr> </table> <?php mysql_close(); ?> Update.php - <?php $host="localhost"; $username="wowdream_domaine"; $password="mypassword"; $db_name="wowdream_domaine"; $tbl_name="members"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $id=$_GET['id']; $sql="SELECT * FROM $tbl_name WHERE id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <form name="form1" method="post" action="update_ac.php"> <td> <table width="100%" border="0" cellspacing="1" cellpadding="0"> <tr> <td> </td> <td colspan="3"><strong>Update data in mysql</strong> </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>Username</strong></td> <td align="center"><strong>Password</strong></td> <td align="center"><strong>Access</strong></td> </tr> <tr> <td> </td> <td align="center"><input name="username" type="text" id="username" value="<? echo $rows['username']; ?>" size="15"></td> <td align="center"><input name="password" type="text" id="password" value="<? echo $rows['password']; ?>" size="15"></td> <td align="center"><input name="access" type="text" id="access" value="<? echo $rows['access']; ?>" 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> <? mysql_close(); ?> Update_ac.php - <?php $host="localhost"; $username="wowdream_domaine"; $password="mypassword"; $db_name="wowdream_domaine"; $tbl_name="members"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="UPDATE $tbl_name SET username='$username', password='$password', access='$access' WHERE id='$id'"; $result=mysql_query($sql); if($result){ echo "Successful"; echo "<BR>"; echo "<a href='editadmin.php'>View result[/url]"; } else { echo "ERROR"; } ?> And in regards to 1st - When you post code please place it between [ code] and [/ code] 2nd - change: else { echo "ERROR"; } with else { echo "ERROR:" . mysql_error(); } Then tell us what it says. - I have done this now, but it's not echoing an error. Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657515 Share on other sites More sharing options...
junelis Posted October 5, 2008 Share Posted October 5, 2008 try this <?php $sql="UPDATE $tbl_name SET username='".$_POST['username']."', password='".$_POST['password']."', access='".$_POST['access']."' WHERE id='".$_POST['id']."'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657528 Share on other sites More sharing options...
adamjones Posted October 5, 2008 Author Share Posted October 5, 2008 Thanks, but nope - Still not working. try this <?php $sql="UPDATE $tbl_name SET username='".$_POST['username']."', password='".$_POST['password']."', access='".$_POST['access']."' WHERE id='".$_POST['id']."'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657549 Share on other sites More sharing options...
AndyB Posted October 5, 2008 Share Posted October 5, 2008 <a href="update.php?id=<? echo $rows['id']; ?>">update</a> Note that you're using short tags there. Check the generated HTML source to verify that id actually has a value. If not, edit that code to: <a href="update.php?id=<?php echo $rows['id']; ?>">update</a> Other than that, it's likely one or more of your SQL queries is failing. You can check that easily enough bu using rational error trapping/display. For example, change instances of: $result=mysql_query($sql); to: $result=mysql_query($sql) or die("Error: ". mysql_error(). " with query ". $sql); Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657561 Share on other sites More sharing options...
budimir Posted October 5, 2008 Share Posted October 5, 2008 Also on your Update_ac.php try this $id = $_GET["id"]; echo "$id"; Maybe you are not getting an ID so the query can not be updated. Also try to echo you're query to see if you are getting all the values. Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657563 Share on other sites More sharing options...
adamjones Posted October 5, 2008 Author Share Posted October 5, 2008 Nope- Still not working! Aghh! Could someone please post a link to a tutorial on how to update database info like this, please? Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657590 Share on other sites More sharing options...
AndyB Posted October 5, 2008 Share Posted October 5, 2008 Nope- Still not working! Can you confirm that your HTML form does actually include a value for the id in the clickable links? Please also identify each/any error message you got after making the changes I suggested. 'not working' isn't helping us help you. Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657593 Share on other sites More sharing options...
budimir Posted October 5, 2008 Share Posted October 5, 2008 You're file "Update_ac.php" is not complete you are missing couple of things. This is wrong: <?php $host="localhost"; $username="wowdream_domaine"; $password="mypassword"; $db_name="wowdream_domaine"; $tbl_name="members"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql="UPDATE $tbl_name SET username='$username', password='$password', access='$access' WHERE id='$id'"; $result=mysql_query($sql); if($result){ echo "Successful"; echo "<BR>"; echo "<a href='editadmin.php'>View result[/url]"; } else { echo "ERROR"; } ?> This is right: <?php $host="localhost"; $username="wowdream_domaine"; $password="mypassword"; $db_name="wowdream_domaine"; $tbl_name="members"; mysql_connect("$host", "$username", "$password") or die ("cannot connect"); mysql_select_db("$db_name") or die ("cannot select DB"); $id = $_POST["id"]; $username1 = $_POST["username"]; $password1 = $_POST["password"]; $access1 = $_POST["access"]; $sql="UPDATE $tbl_name SET username='$username1', password='$password1', access='$access1' WHERE id='$id'"; $result=mysql_query($sql) or die (mysql_error()); $num=mysql_num_rows($result); if($num == 1){ echo "Successful"; echo "<br />"; echo "<a href='editadmin.php'>View result[/url]"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127101-php-form-wont-work/#findComment-657597 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.