jlgray48 Posted April 23, 2007 Share Posted April 23, 2007 This is the start of a program that will prompt a user for their user ID (integer), pass the integer to a php program, use that ID to retrieve a record from a mysql database. I have created the html form as follows. <html> <form method="POST" action = "mystudent.php"> Please enter your SID <br> <br> <input type="text" name= "ID" value=""> <br> <br> <input type="submit" value="Submit" > </form> </html> This works fine, now I am starting my php file and I'm starting off slow trying to make sure I have the ID. I can't get the ID to display for some reason. I am a newbie so go easy on me. I do know I have to use $_REQUEST and not POST. Here is my PHP file <? php $test = $_REQUEST['ID']; Print "$test" ?> This doesn't work yet..... Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/ Share on other sites More sharing options...
trq Posted April 23, 2007 Share Posted April 23, 2007 $_POST should work fine. <?php $test = $_POST['ID']; Print "$test" ?> However, you may want to remove the space between = and "ID". <input type="text" name="ID" value=""> Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235646 Share on other sites More sharing options...
jlgray48 Posted April 23, 2007 Author Share Posted April 23, 2007 Ok, got that working now I'm trying to use that variable to access the database named php000 and print out the record with SID that corresponds with the variable. Here's my code. mystudent.php I'm using SID = 4 to just test I will change to $test after I get it to work. <?php $test = $_REQUEST['ID']; $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php000",$conn); $sql = "Select * from mystudent where SID=4"; mysql_query($sql,$conn); $result = mysql_query($sql,$conn); while ($array = mysql_fetch_array($result)) { print $array['name']; } ?> Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235663 Share on other sites More sharing options...
trq Posted April 23, 2007 Share Posted April 23, 2007 And whats the problem now? <?php if (isset($_POST['ID'])) { $id = mysql_real_escape_string($_POST['ID']); $conn = mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php000",$conn); $sql = "SELECT * FROM mystudent WHERE SID = '$id'"; if ($result = mysql_query($sql,$conn)) { if (mysql_num_rows($result)) { while ($array = mysql_fetch_array($result)) { print $array['name']; } } } } ?> Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235673 Share on other sites More sharing options...
jlgray48 Posted April 23, 2007 Author Share Posted April 23, 2007 Ok Thorpe, I got my code to work accept for one thing. How do I get the $array to print all of the fields? Thanks for all your help. jg Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235676 Share on other sites More sharing options...
trq Posted April 23, 2007 Share Posted April 23, 2007 You could name them all explicitly or use a foreach() loop. eg; while ($array = mysql_fetch_array($result)) { foreach($array as $val) { echo $val . "<br />"; } } Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235684 Share on other sites More sharing options...
jlgray48 Posted April 23, 2007 Author Share Posted April 23, 2007 Ok, if I use the "Select *" I get nothing in my array but if I use "Select Name" or "Select Address" it works fine. <?php $test = $_REQUEST['ID']; $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php000",$conn); $sql = "Select name from mystudent where sid=$test"; $result = mysql_query($sql,$conn) or die("Error".mysql_error()); while ($array = mysql_fetch_array($result)) { print $array['name']; } ?> Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235696 Share on other sites More sharing options...
trq Posted April 23, 2007 Share Posted April 23, 2007 Sorry, but that makes no sense. Use my code as yours has little/no error handling. Ive modified it a little to give us a hand with debugging. <?php if (isset($_POST['ID'])) { $id = mysql_real_escape_string($_POST['ID']); $conn = mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php000",$conn); $sql = "SELECT * FROM mystudent WHERE SID = '$id'"; if ($result = mysql_query($sql,$conn)) { if (mysql_num_rows($result)) { while ($array = mysql_fetch_array($result)) { foreach($array as $val) { echo $val . "<br />"; } } } else { echo "No results found"; } } else { echo "Query failed<br />$sql<br />" . mysql_error(); } } ?> What does that produce? Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235702 Share on other sites More sharing options...
jlgray48 Posted April 23, 2007 Author Share Posted April 23, 2007 That got me what I needed. Thanks again for all your help. Sorry about the duplicate post. Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235706 Share on other sites More sharing options...
jlgray48 Posted April 23, 2007 Author Share Posted April 23, 2007 Ok, here is my code, it is producing exactly what I want except it is echoing everything twice. <?php $test = $_REQUEST['ID']; $conn=mysql_connect("127.0.0.1", "odbc", "") ; mysql_select_db("php000",$conn); $sql = "Select * from mystudent where sid=$test"; $result = mysql_query($sql,$conn) or die("Error".mysql_error()); while ($array = mysql_fetch_array($result)) { foreach ($array as $val) echo $val . "<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/48205-unable-to-pass-variable-to-php/#findComment-235710 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.