Jump to content

Unable to pass Variable to PHP


jlgray48

Recommended Posts

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

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'];

}

 

 

 

?>

 

 

 

 

 

 

 

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'];
        }
      }
    }
  }

?>

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'];

 

 

}

?>

 

 

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?

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>";

 

}

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.