Bl4ckMaj1k Posted March 22, 2011 Share Posted March 22, 2011 Good evening my PHP experts!!! OK so heres the problem...I have been at this for about 2 DAYS now and for the life of me I just don't understand what I could possibly be doing wrong. I have 3 of 4 variables outputting correctly but without my id being pulled from the URL NOTHING will get written to my database. I have ran some tests and I came to learn that my $_POST['id']; function is not working properly for some strange reason. As always, any help would be greatly appreciated. Please see below PHP code. I also have posted the HTML code for your reference purposes...thanks in advance. Please helpppp!!!! <?php session_start(); $id = ''; if (isset($_POST['password'])) { include_once "scripts/connect_to_mysql.php"; $id = $_POST['id']; $id = mysql_real_escape_string($id ); $id = eregi_replace("`", "", $id); $cust_password = $_POST['password']; $hashedPass = md5($cust_password); $sql = mysql_query("UPDATE customers SET password='$hashedPass' WHERE id='$id'"); $update_success = 'You have successfully updated your Password. Please click <a href="customer_login.php">HERE</a> to log into your account.'; //Output to test whether or not my variables have anything in them. //$update_success, $hashedPass, $cust_password all Output just fine. $id displays nothing at all. echo $update_success; echo $hashedPass; echo $cust_password; echo $id; exit(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <link rel="stylesheet" type="text/css" href="style.css"/> <script type="text/javascript"> <!-- Form Validation --> function validate_form ( ) { valid = true; if ( document.form.password.value == "" ) { alert ( "Password field must not be blank." ); valid = false; } return valid; } <!-- Form Validation --> </script> </head> <body> <div id="pg_container"> <!--HEADER SECTION--> <div id="header"> <div id="header_logo"></div> <div id="header_right">Welcome to Built 2 Manage.</div> </div> <!--HEADER SECTION--> <!--PAGE CONTAINER--> <div id="pgbdy_container"> <form action="cust_acc_conf.php" method="post" id="form" name="form" enctype="multipart/form-data" onsubmit="return validate_form ( );"> <div id="login_header"> <p> Please log into your account below. </p> </div> <div id="cust_choose_pass_form"> <div id="cust_choose_pass_text"> <p> Password: </p> </div> <div id="cust_choose_pass_field"> <p> <input name="password" type="text" id="password" /><br /><br /> <input name="submit_password" type="submit" id="submit_password" value="Submit Password" /><br /> </p> </div> </div> </form> </div> <!--PAGE CONTAINER--> <div id="footer"></div> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/ Share on other sites More sharing options...
Pikachu2000 Posted March 22, 2011 Share Posted March 22, 2011 If id is appended to the URL string, it will be in the $_GET array, not the $_POST array . . . Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/#findComment-1190902 Share on other sites More sharing options...
Bl4ckMaj1k Posted March 22, 2011 Author Share Posted March 22, 2011 I have tried $_GET, $_POST, and $_REQUEST....still nothing. My $id variable will not echo anything neither will any data get written to the table. P.S. if I am using method="POST"; in my HTML form, will PHP still allow me to gather data using $_GET? Someone told me yesterday that its not possible. P.S.S. I am literally smiling ear to ear seeing the speedy reply.....WOW!!! YAY!!! :-) Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/#findComment-1190904 Share on other sites More sharing options...
Pikachu2000 Posted March 22, 2011 Share Posted March 22, 2011 Yeah, even if the form method is post, you can still send a 4_GET var by tacking it onto the form's action= attribute. To see what you're actually getting, put this at the top of the script and look at its output after the form has been submitted. echo '$_POST array:<pre>'; print_r($_POST); echo '</pre>$_GET array:<pre>'; print_r($_GET); echo '</pre>'; Edit: fixed bbcode tags. Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/#findComment-1190911 Share on other sites More sharing options...
Bl4ckMaj1k Posted March 22, 2011 Author Share Posted March 22, 2011 Interesting. Here is the PHP with the function you advised I add to top of code: <?php session_start(); echo '$_POST array:<pre>'; print_r($_POST); echo '</pre>$_GET array:<pre>'; print_r($_GET); echo '</pre>'; $id = ''; if (isset($_POST['password'])) { include_once "scripts/connect_to_mysql.php"; $id = $_GET['id']; $id = mysql_real_escape_string($id ); $id = eregi_replace("`", "", $id); $cust_password = $_POST['password']; $hashedPass = md5($cust_password); //$sql = mysql_query("UPDATE customers SET password='$hashedPass' WHERE id='$id'"); $update_success = 'You have successfully updated your Password. Please click <a href="customer_login.php">HERE</a> to log into your account.'; echo $update_success; echo $hashedPass; echo $cust_password; echo $id; exit(); } ?> And here is what was output to page: $_POST array: Array ( [password] => christopher [submit_password] => Submit Password ) $_GET array: Array ( ) You have successfully updated your Password. Please click HERE to log into your account.0a909ffe7be1ffe2ec130aa243a64c26christopher Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/#findComment-1190921 Share on other sites More sharing options...
Pikachu2000 Posted March 22, 2011 Share Posted March 22, 2011 And at that point, is there still a key=value pair appended to the URL string in the address bar of your browser? Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/#findComment-1190927 Share on other sites More sharing options...
kenrbnsn Posted March 22, 2011 Share Posted March 22, 2011 Please post your form. Ken Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/#findComment-1190928 Share on other sites More sharing options...
Bl4ckMaj1k Posted March 22, 2011 Author Share Posted March 22, 2011 EDIT I DID IT!!!!!! I FIXED IT!!!!! Finally WOWWWW!!! I am SOOOOO proud of me right now!! Here is what I did. First, I changed the following: if (isset($_POST['password'])) { include_once "scripts/connect_to_mysql.php"; $id = $_GET['id']; $id = mysql_real_escape_string($id ); $id = eregi_replace("`", "", $id); to: include_once "scripts/connect_to_mysql.php"; $id = $_GET['id']; $id = mysql_real_escape_string($id ); $id = eregi_replace("`", "", $id); if (isset($_POST['password'])) { Then I went into the HTML of the code and changed the following: <form action="cust_acc_conf.php" method="post" id="form" name="form" enctype="multipart/form-data" onsubmit="return validate_form ( );"> <div id="login_header"> to: <form action="cust_acc_conf.php?id=<?php echo $id ?>" method="post" id="form" name="form" enctype="multipart/form-data" onsubmit="return validate_form ( );"> This wouldn't work for me before because the $id variable wasn't being returned UNLESS I hit the submit button. Now that its outside of the if condition, it stores it in the $id variable without any necessary action required. This makes it accessible whenever I need it, not just when a button is pressed. Wow sometimes all you need is a push in the right direction. Thank youuuuuuuuuu!!!!!!! Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/#findComment-1190930 Share on other sites More sharing options...
Bl4ckMaj1k Posted March 22, 2011 Author Share Posted March 22, 2011 And at that point, is there still a key=value pair appended to the URL string in the address bar of your browser? P.S. The answer to this was NOOOO!!!! And I totally get it!! My form wasn't returning the variable to the URL.....so how would it POSSIBLY have anything to capture....wow PHP makes SOOOO much sense!!!! :-) I <3 this place already!!!!!! Link to comment https://forums.phpfreaks.com/topic/231411-please-help-cant-get-id-from-urlwhats-the-problem/#findComment-1190932 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.