0

I am trying to get a variable from a form on my page 1 into a query I have made on page 2 so that when the query works it will use the variable entered on page 1

Page 1 Form

<form method="post" action="testformQ.php"> <input type="text" name="testform"> <input type="submit">enter

Page 2 Query

'$software = mysql_query("SELECT software.SoftwareID, rooms.RoomID 
    FROM softwareroom 
    INNER JOIN software 
    ON software.SoftwareID=softwareroom.SoftwareID 
    INNER JOIN rooms ON rooms.RoomID=softwareroom.RoomID 
    WHERE rooms.RoomNum=$_GET[testform]");              (the where clause is where i want the variable)
        while($rec = mysql_fetch_array($software))
        {
            echo $rec['SoftwareID'] . " " . $rec['RoomID'];
        }'

So where the GET testform is within the query is where I want the form variable to go. Any help would be appreciated thanks

  • Why avoid mysql_* functions. http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php – chanchal118 Dec 22 '13 at 14:10

2 Answers2

0

This code will work for you

$data = $_REQUEST['testform'];

$software = mysql_query("SELECT software.SoftwareID, rooms.RoomID 
    FROM softwareroom 
    INNER JOIN software 
    ON software.SoftwareID=softwareroom.SoftwareID 
    INNER JOIN rooms ON rooms.RoomID=softwareroom.RoomID 
    WHERE rooms.RoomNum='$data'");        
    while($rec = mysql_fetch_array($software))
        {
            echo $rec['SoftwareID'] . " " . $rec['RoomID'];
        }'  
Anil Meena
  • 903
  • 1
  • 12
  • 28
  • Thanks that seems to have worked but now I am having problems displaying the result of the query any idea how I might do that? – user3127167 Dec 27 '13 at 11:40
  • at the place of echo $rec['SoftwareID'] . " " . $rec['RoomID']; you can use print_r($rec); – Anil Meena Dec 27 '13 at 11:52
  • Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\testformQ.php on line 17 Getting this error 17)while($rec = mysql_fetch_array($software)) 18) { 19) print_r($rec); } – user3127167 Dec 27 '13 at 12:05
  • sorry its my mistake in coding.now you can use my code i have edit it in line $software = mysql_query("SELECT software.SoftwareID, rooms.RoomID . – Anil Meena Dec 27 '13 at 12:16
  • I still get the same errorWarning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\testformQ.php on line 17 – user3127167 Dec 27 '13 at 12:22
  • have you use $con = mysql_connect('localhost','username','password'); mysql_select_db("dbname", $con); at top of page 2? – Anil Meena Dec 27 '13 at 12:25
0

Try this for Page02 Query..

    $servername = "servername";
    $username = "username";
    $password = "password";
    $db = "db_name";

    $query = "SELECT software.SoftwareID, rooms.RoomID FROM softwareroom INNER JOIN software ON software.SoftwareID = softwareroom.SoftwareID"
            + "INNER JOIN rooms ON rooms.RoomID = softwareroom.RoomID WHERE rooms.RoomNum = '$_POST["testform"]'";

    //Create db connection
    $connection = mysqli_connect($servername, $username, $password, $db);

    //Check connection
    if(!$connection){
        echo "Failed to connect to database ".mysqli_connect_error();
    }
    $software = mysqli_query($connection, $query);
    mysqli_close($connection);