chris_rulez001 Posted June 1, 2007 Share Posted June 1, 2007 hi, i have a users page and i wanted to create an account delete link and i came up with this: <?php session_start(); mysql_connect("localhost", "user", "pass"); //this function will delete SQL entries DELETE FROM users WHERE username = '' //This function will destroy your session session_destroy(); echo "Your account has been deleted! <a href=index.php>Index</a> or <a href=register.php>Register</a>"; ?> i have got an error, it is: Parse error: parse error, unexpected T_STRING in /www/1111mb.com/c/h/r/chrisrulez/htdocs/test1/delete.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/ Share on other sites More sharing options...
papaface Posted June 1, 2007 Share Posted June 1, 2007 Thats because youve just plonked a SQL statement in the middle of no where. Should be something like: mysql_query("DELETE FROM users WHERE username = '{$username}'"); Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-265941 Share on other sites More sharing options...
chris_rulez001 Posted June 1, 2007 Author Share Posted June 1, 2007 ok, ive done that and the code now looks like: <?php session_start(); mysql_connect("localhost", "user", "pass"); session_register("username", $username); //this function will delete SQL entries mysql_query("DELETE FROM users WHERE username = '{$username}'"); //This function will destroy your session session_destroy(); echo "Your account has been deleted! <a href=index.php>Index</a> or <a href=register.php>Register</a>"; ?> but ive got problems: Notice: Undefined variable: username in /www/1111mb.com/c/h/r/chrisrulez/htdocs/test1/delete.php on line 5 Notice: Undefined variable: username in /www/1111mb.com/c/h/r/chrisrulez/htdocs/test1/delete.php on line 8 Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-265943 Share on other sites More sharing options...
papaface Posted June 1, 2007 Share Posted June 1, 2007 session_register("username", $username); $username isnt declared anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-265945 Share on other sites More sharing options...
chris_rulez001 Posted June 1, 2007 Author Share Posted June 1, 2007 ok, but i want it so if the user that is logged in clicks delete account it deletes their account, how would i intigrate it so it does the logged in users account? Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-265951 Share on other sites More sharing options...
Caesar Posted June 1, 2007 Share Posted June 1, 2007 Where are you storing the username? Hopefully, in a session on the previous page/login. Are you posting to this page after the login? Also...you can change <?php session_start(); session_register("username", $username); ?> To... <?php session_start(); $_SESSION['username'] = $_POST['username']; //Or whatever value you're giving it ?> Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-265952 Share on other sites More sharing options...
chris_rulez001 Posted June 1, 2007 Author Share Posted June 1, 2007 im getting a: Notice: Undefined index: username in /www/1111mb.com/c/h/r/chrisrulez/htdocs/test1/delete.php on line 5 Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-265960 Share on other sites More sharing options...
Caesar Posted June 1, 2007 Share Posted June 1, 2007 <?php $username = $_SESSION['username']; mysql_query("DELETE FROM users WHERE username = '$username'"); ?> Don't assume register_globals is turned on. Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-265963 Share on other sites More sharing options...
chris_rulez001 Posted June 1, 2007 Author Share Posted June 1, 2007 right, the error has disappeared but its not actually deleting the users account, how could i do that? Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-266187 Share on other sites More sharing options...
MemphiS Posted June 1, 2007 Share Posted June 1, 2007 can you show your current code after the fixs made above.. Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-266189 Share on other sites More sharing options...
papaface Posted June 1, 2007 Share Posted June 1, 2007 We have no idea what your script looks like (the entire thing, not just this page) so we can't really say. Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-266190 Share on other sites More sharing options...
chris_rulez001 Posted June 1, 2007 Author Share Posted June 1, 2007 all my script, below: delete.php: <?php session_start(); mysql_connect("localhost", "user", "pass"); $username = isset($_SESSION['username'])?$_SESSION['username']:''; mysql_query("DELETE FROM users WHERE username = '$username'"); //This function will destroy your session session_destroy(); echo "Your account has been deleted! <a href=index.php>Index</a> or <a href=register.php>Register</a>"; ?> index.php: <?php //This will start a session session_start(); $username = isset($_SESSION['username'])?$_SESSION['username']:''; $password = isset($_SESSION['password'])?$_SESSION['password']:''; //Check do we have username and password if(!$username && !$password){ echo "Welcome Guest! <br> <a href=login.php>Login</a> | <a href=register.php>Register</a>"; }else{ echo "Welcome ".$username." (<a href=logout.php>Logout</a>)"; } ?> register.php: <?php //This function will display the registration form function register_form(){ $date = date('D, M, Y'); echo "<form action='?act=register' method='post'>" ."Username: <input type='text' name='username' size='30'><br>" ."Password: <input type='password' name='password' size='30'><br>" ."Confirm your password: <input type='password' name='password_conf' size='30'><br>" ."Email: <input type='text' name='email' size='30'><br>" ."Gender: <input type='text' name='gender' size='30'><br>" ."Age: <input type='text' name='age' size='30'><br>" ."<input type='hidden' name='date' value='$date'>" ."<input type='submit' value='Register'>" ."</form>"; } //This function will register users data function register(){ //Connecting to database $connect = mysql_connect("localhost", "user", "pass"); if(!$connect){ die(mysql_error()); } //Selecting database $select_db = mysql_select_db("mydb"); if(!$select_db){ die(mysql_error()); } //Collecting info $username = $_REQUEST['username']; $password = $_REQUEST['password']; $pass_conf = $_REQUEST['password_conf']; $email = $_REQUEST['email']; $gender = $_REQUEST['gender']; $age = $_REQUEST['age']; $date = $_REQUEST['date']; //Here we will check do we have all inputs filled if($username == ""){ die("Please enter your username!<br>"); } if($password == ""){ die("Please enter your password!<br>"); } if($pass_conf == ""){ die("Please confirm your password!<br>"); } if($email == ""){ die("Please enter your email!<br>"); } if($gender == ""){ die("Please enter your gender!<br>"); } if($age == ""){ die("Please enter your age!<br>"); } if($age < "13"){ die("You must be 13 or over to join!"); } //Let's check if this username is already in use $user_check = mysql_query("SELECT username FROM users WHERE username='$username'"); $do_user_check = mysql_num_rows($user_check); //Now if email is already in use $email_check = mysql_query("SELECT email FROM users WHERE email='$email'"); $do_email_check = mysql_num_rows($email_check); //Now display errors if($do_user_check > 0){ die("Username is already in use!<br>"); } if($do_email_check > 0){ die("Email is already in use!"); } //Now let's check does passwords match if($password != $pass_conf){ die("Passwords don't match!"); } //If everything is okay let's register this user $insert = mysql_query("INSERT INTO users (username, password, email, gender, age, date) VALUES ('$username', '$password', '$email', '$gender', '$age', '$date')"); if(!$insert){ die("There's little problem: ".mysql_error()); } echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> | <a href=index.php>Index</a>"; } $act = isset($_GET['act'])?$_GET['act']:''; switch($act){ default; register_form(); break; case "register"; register(); break; } ?> login.php: <?php session_start(); //This displays your login form function index(){ echo "<form action='?act=login' method='post'>" ."Username: <input type='text' name='username' size='30'><br>" ."Password: <input type='password' name='password' size='30'><br>" ."<input type='submit' value='Login'>" ."</form>"; } //This function will find and checks if your data is correct function login(){ //Collect your info from login form $username = $_REQUEST['username']; $password = $_REQUEST['password']; //Connecting to database $connect = mysql_connect("localhost", "user", "pass"); if(!$connect){ die(mysql_error()); } //Selecting database $select_db = mysql_select_db("mydb"); if(!$select_db){ die(mysql_error()); } //Find if entered data is correct $result = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password'"); $row = mysql_fetch_array($result); $id = $row['id']; $select_user = mysql_query("SELECT * FROM users WHERE id='$id'"); $row2 = mysql_fetch_array($select_user); $user = $row2['username']; if($username != $user){ die("Username is wrong!"); } $pass_check = mysql_query("SELECT * FROM users WHERE username='$username' AND id='$id'"); $row3 = mysql_fetch_array($pass_check); $email = $row3['email']; $select_pass = mysql_query("SELECT * FROM users WHERE username='$username' AND id='$id' AND email='$email'"); $row4 = mysql_fetch_array($select_pass); $real_password = $row4['password']; if($password != $real_password){ die("Your password is wrong!"); } //Now if everything is correct let's finish his/her/its login session_register("username", $username); session_register("password", $password); echo "<div align='center'>"; echo "<h3>"; echo "Welcome, ".$username." to Chris World"; echo "</h3>"; echo "</div>"; echo "<br>"; echo "<table>"; echo "<tr>"; echo "<td>"; echo "User Summary For ".$username.""; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "<table>"; echo "<tr>"; echo "<td>"; echo "<b>Name: </b>"; echo "</td>"; echo "<td>$username</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "<b>Date Registered: </b>"; echo "</td>"; echo "<td>"; echo "<b>Yesterday</b> at 05:11:00 PM"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "<b>Last Active: </b>"; echo "</td>"; echo "<td>"; echo "<b>Today</b> at 05:41:56 PM"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td><hr/>"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "<b>".$username."'s Email: </b>"; echo "</td>"; echo "<td>"; echo "".$email.""; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "<b>Language:</b>"; echo "</td>"; echo "<td>"; echo "English"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "<hr/>"; echo "</td>"; echo "</tr>"; echo "</table>"; echo "<a href=logout.php>Logout</a> "; echo "| <a href=delete.php>Delete Account</a>"; } $act = isset($_GET['act'])?$_GET['act']:''; switch($act){ default; index(); break; case "login"; login(); break; } ?> logout.php: <?php session_start(); //This function will destroy your session session_destroy(); echo "You are now logged out! <a href=index.php>Index</a> or <a href=login.php>Login</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-266203 Share on other sites More sharing options...
chris_rulez001 Posted June 3, 2007 Author Share Posted June 3, 2007 ive posted the whole of my code, can you help me now? Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-267392 Share on other sites More sharing options...
chigley Posted June 3, 2007 Share Posted June 3, 2007 mysql_query("DELETE FROM users WHERE username = '{$username}'") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-267409 Share on other sites More sharing options...
dough boy Posted June 3, 2007 Share Posted June 3, 2007 1. I do not see where you are selecting a database to work on. 2. You do not need to have the {} around the $username in your query. Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-267419 Share on other sites More sharing options...
chigley Posted June 3, 2007 Share Posted June 3, 2007 Not needed but it's good practice.. Quote Link to comment https://forums.phpfreaks.com/topic/53808-deletephp-help-help-needed-urgently/#findComment-267422 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.