ckerr27 Posted April 5, 2012 Share Posted April 5, 2012 Hey I finally got this code working yesterday, when the user enters a name into the form and submits it the specific users details should be shown on the next page, this was working fine yesterday and when i turned my laptop on today it is not working, I am not getting any errors just not displaying the user details?? Any ideas? Heres the code Form: <table width="300" border="1" align="center" cellpadding="2" cellspacing="1" bgcolor="#9999cc"> <tr> <form name="form1" method="post" action="getdetails.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong><center>Personal Details</center> </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="submit" type="text" id="username"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Get Details"></td> </tr> </table> </td> </form> </tr> </table> getdetails.php <html> <head> <link rel="stylesheet" type ="text/css" href="anything2.css" </head> <body> <div id="container"> <div id="header"><img src="imagesb.jpg" alt="Cool Image" align="left"> <img src="images.jpg" alt="Cool Image" align="right"><center><b><font size="6.5"><br><br>User Details</b></center></font> </div> <div id="leftnav"><center> <br><br> <input type= "button" style="width:120px;" value="Home" onClick="window.location= 'home.php' "> <br><br> <input type= "button" style="width:120px;" value="Club Details" onClick="window.location= 'clubdetails.php' "> <br><br> <input type= "button" style="width:120px;" value="Future Events" onClick="window.location= 'futureevents.php' "> <br><br> <input type= "button" style="width:120px;" value="News" onClick="window.location= 'news.php' "> <br><br> <input type= "button" style="width:120px;" value="FAQ" onClick="window.location= 'faq.php' "> <br><br> <input type= "button" style="width:120px;" value="Wall" onClick="window.location= 'wall.php' "> <br><br> <input type= "button" style="width:120px;" value="About Us" onClick="window.location= 'about.php' "> </div> <div id="body"> <br><br> <?php mysql_connect ("localhost","root","") or die("Cannot connect to Database"); mysql_select_db ("test"); if (isset($_POST['Submit'])){ $username=mysql_real_escape_string(trim($_POST['Submit'])); $sql = "select * from memberdetails WHERE username='$username'"; $result = mysql_query ($sql); while ($row = mysql_fetch_array($result)) { $username= $row["username"]; $firstname= $row["firstname"]; $surname= $row["surname"]; $dob= $row["dob"]; $totalwins= $row["totalwins"]; $totalloses= $row["totalloses"]; $email= $row["email"]; $country= $row["country"]; $info= $row["info"]; echo "<b><u>Username:</b></u> $username<br>"; echo "<b><u>Firstname:</b></u> $firstname<br>"; echo "<b><u>Surname: </b> </u> $surname<br>"; echo "<b><u>Date of Birth:</b></u> $dob<br>"; echo "<b><u>Total Chess Wins:</b></u> $totalwins<br>"; echo "<b><u>Total Chess loses:</b></u> $totalloses<br>"; echo "<b><u>Email Address: </b></u> $email<br>"; echo "<b><u>Born in: </b></u> $country<br>"; echo "<b><u>Other Details:</b></u> $info<br><br><br>"; }} ?> <a href="delete.php">Delete User</a> <div id="footer">This is the footer</div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/ Share on other sites More sharing options...
AyKay47 Posted April 5, 2012 Share Posted April 5, 2012 you are using the same name attribute for the submit button and the text field. Change the text field name to something that makes sense, like "username" Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334599 Share on other sites More sharing options...
ckerr27 Posted April 5, 2012 Author Share Posted April 5, 2012 Thanks my code is now <td width="294"><input name="username" type="text" id="username"></td> <td><input type="submit" name="Submit" value="Get Details"></td> But this is still not working, it was working perfectly yesterday. Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334607 Share on other sites More sharing options...
AyKay47 Posted April 5, 2012 Share Posted April 5, 2012 post the entire updated code, using code or php tags Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334608 Share on other sites More sharing options...
ckerr27 Posted April 5, 2012 Author Share Posted April 5, 2012 getdetails.php <html> <head> <link rel="stylesheet" type ="text/css" href="anything2.css" </head> <body> <div id="container"> <div id="header"><img src="imagesb.jpg" alt="Cool Image" align="left"> <img src="images.jpg" alt="Cool Image" align="right"><center><b><font size="6.5"><br><br>User Details</b></center></font> </div> <div id="leftnav"><center> <br><br> <input type= "button" style="width:120px;" value="Home" onClick="window.location= 'home.php' "> <br><br> <input type= "button" style="width:120px;" value="Club Details" onClick="window.location= 'clubdetails.php' "> <br><br> <input type= "button" style="width:120px;" value="Future Events" onClick="window.location= 'futureevents.php' "> <br><br> <input type= "button" style="width:120px;" value="News" onClick="window.location= 'news.php' "> <br><br> <input type= "button" style="width:120px;" value="FAQ" onClick="window.location= 'faq.php' "> <br><br> <input type= "button" style="width:120px;" value="Wall" onClick="window.location= 'wall.php' "> <br><br> <input type= "button" style="width:120px;" value="About Us" onClick="window.location= 'about.php' "> </div> <div id="body"> <br><br> <?php mysql_connect ("localhost","root","") or die("Cannot connect to Database"); mysql_select_db ("test"); if (isset($_POST['Submit'])){ $username=mysql_real_escape_string(trim($_POST['Submit'])); $sql = "select * from memberdetails WHERE username='$username'"; $result = mysql_query ($sql); while ($row = mysql_fetch_array($result)) { $username= $row["username"]; $firstname= $row["firstname"]; $surname= $row["surname"]; $dob= $row["dob"]; $totalwins= $row["totalwins"]; $totalloses= $row["totalloses"]; $email= $row["email"]; $country= $row["country"]; $info= $row["info"]; echo "<b><u>Username:</b></u> $username<br>"; echo "<b><u>Firstname:</b></u> $firstname<br>"; echo "<b><u>Surname: </b> </u> $surname<br>"; echo "<b><u>Date of Birth:</b></u> $dob<br>"; echo "<b><u>Total Chess Wins:</b></u> $totalwins<br>"; echo "<b><u>Total Chess loses:</b></u> $totalloses<br>"; echo "<b><u>Email Address: </b></u> $email<br>"; echo "<b><u>Born in: </b></u> $country<br>"; echo "<b><u>Other Details:</b></u> $info<br><br><br>"; }} ?> <a href="delete.php">Delete User</a> <div id="footer">This is the footer</div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334609 Share on other sites More sharing options...
ckerr27 Posted April 5, 2012 Author Share Posted April 5, 2012 form <table width="300" border="1" align="center" cellpadding="2" cellspacing="1" bgcolor="#9999cc"> <tr> <form name="form1" method="post" action="getdetails.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td colspan="3"><strong><center>Personal Details</center> </strong></td> </tr> <tr> <td width="78">Username</td> <td width="6">:</td> <td width="294"><input name="username" type="text" id="username"></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Get Details"></td> </tr> </table> </td> </form> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334610 Share on other sites More sharing options...
AyKay47 Posted April 5, 2012 Share Posted April 5, 2012 if (isset($_POST['Submit'])) { $username=mysql_real_escape_string(trim($_POST['username'])); //this needs to match the nameattr of the input field $sql = "select * from memberdetails WHERE username='$username'"; $result = mysql_query ($sql); *snippet* Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334611 Share on other sites More sharing options...
ckerr27 Posted April 5, 2012 Author Share Posted April 5, 2012 Perfect Thanks mate Really appreciated!! Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334613 Share on other sites More sharing options...
ckerr27 Posted April 5, 2012 Author Share Posted April 5, 2012 AyKay47 just one more question.... I have tried to add a function to allow the user to delete the user form the database if required. The code I have used is: <?php mysql_connect ("localhost","root","") or die("Cannot connect to Database"); mysql_select_db ("test"); mysql_query ("DELETE FROM members WHERE username='$username'") or die (mysql_error)); echo "User has been deleted"; header("Location: personaldetails.php"); ?> This is giving me an error on the mysql_query line. I am not sure if i am using the correct format? Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334616 Share on other sites More sharing options...
AyKay47 Posted April 5, 2012 Share Posted April 5, 2012 Is $username defined elsewhere? the query syntax is fine, my guess is that $username is not defined. what is the error? Also, it's mysql_error(), not mysql_error Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334622 Share on other sites More sharing options...
ckerr27 Posted April 5, 2012 Author Share Posted April 5, 2012 When the user enters the username into the form and is redirected to getdetail.php there is a link at the bottom of the page to "delete user": <a href="delete.php">Delete User</a> Is $username declared on my getdetails.php? or do i have to declare it on my delete.php? Delete.php: <?php mysql_connect ("localhost","root","") or die("Cannot connect to Database"); mysql_select_db ("test"); mysql_query ("DELETE FROM members WHERE username='$username'") or die ("Error- user has not been deleted"); echo "User has been deleted"; //header("Location: personaldetails.php"); ?> When this function is working correctly should it delete the row completely from my database? Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334626 Share on other sites More sharing options...
AyKay47 Posted April 5, 2012 Share Posted April 5, 2012 <a href="delete.php?username=<?php echo $username; ?>">Delete User</a> Delete.php: <?php mysql_connect ("localhost","root","") or die("Cannot connect to Database"); mysql_select_db ("test"); $username = $_GET['username']; $username = mysql_real_escape_string($username); mysql_query ("DELETE FROM members WHERE username='$username'") or die ("Error- user has not been deleted"); echo "User has been deleted"; //header("Location: personaldetails.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334630 Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 When the user enters the username into the form and is redirected to getdetail.php there is a link at the bottom of the page to "delete user": <a href="delete.php">Delete User</a> Is $username declared on my getdetails.php? or do i have to declare it on my delete.php? Delete.php: <?php mysql_connect ("localhost","root","") or die("Cannot connect to Database"); mysql_select_db ("test"); mysql_query ("DELETE FROM members WHERE username='$username'") or die ("Error- user has not been deleted"); echo "User has been deleted"; //header("Location: personaldetails.php"); ?> When this function is working correctly should it delete the row completely from my database? Yup the reason it doesn't work is because you don't declare $username in delete, make sure like aykay's script that you use $_GET['username'] and when linking to the delete add ?username=$username otherwise the GET can't get it Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334637 Share on other sites More sharing options...
smerny Posted April 5, 2012 Share Posted April 5, 2012 might want to add some security... unless you want anyone to be able to delete anyone... also i'd probably delete based on user id rather than name.. and get the id from the session and require a password Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334643 Share on other sites More sharing options...
ckerr27 Posted April 5, 2012 Author Share Posted April 5, 2012 Thanks everyone!! working great. Smerny thats what I plan to do next, the user "admin" is the administrator of the site and will be the only one ablt to delete users. Any tips on this would be much appreciated. Should I have a page e.g. admindetails.php which the admin is directed to when he/she logs in, which has the delete function on this page and direct all other users to a getdetails.php where they can only view the details. Or is there a more simple way of doing this? Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334651 Share on other sites More sharing options...
smerny Posted April 5, 2012 Share Posted April 5, 2012 depends on what you want to do... if there is a lot of stuff the admin needs to do, contain those things a separate section that can only be accessed when logged in as an admin.. if it's just small things.. you could put it on the same page. so the delete button would only show up if you are logged in as an admin...and of course protect the function after the button is clicked as well. Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334655 Share on other sites More sharing options...
ckerr27 Posted April 5, 2012 Author Share Posted April 5, 2012 Super il give it a try and get back to u!! thanks mate Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334677 Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 simple temporary fix so only one person can delete <?php if($_SESSION['username'] == "theuserthatcandelete") { //delete button } else { //nodelete button } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334683 Share on other sites More sharing options...
smerny Posted April 5, 2012 Share Posted April 5, 2012 except rather than hard coding a user name in, you can have a rank session var... or check the user session var against the user db to check rank. and again, usually best to use id rather than names Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334689 Share on other sites More sharing options...
downah Posted April 5, 2012 Share Posted April 5, 2012 definitely, but I am working with that as a quick temporary solution, thought id share it Quote Link to comment https://forums.phpfreaks.com/topic/260394-code-not-working-anymore/#findComment-1334699 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.