1 Question: Passing data from `HTML` Login form to `PHP` for data retrivel from `MySQL' database

Question created on Sunday February 26, 2017

The below is the code of the PHP file that I creaed.

<?php
$db = mysqli_connect("127.0.0.1","root","toor","mylib")
or die('Error connecting to MySQL server.');
?>
<html>
<head>
</head>
<h1>PHP connect to MySQL</h1>
</body>
</html>


<?php
$query = "SELECT * FROM book WHERE bookid IN  (SELECT bookid FROM studentbook  WHERE studid =
$_POST['stuid'])";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_row($result);
foreach($row as $a)
    print($a." ");
?>

Now, I am looking to get studid from HTML login page and passing it to the PHP file using POST method to retrieve data corresponding to the value(studid) entered, from MySQL database - I mean using mysqli_query() function.

But when I try to run the code which I have shown above, I came up with below Error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /opt/lampp/htdocs/connectivity.php on line 13

Please guide me on the following:

1) How to get values from Login page and pass it to the PHP file using post method?

2) How to correct the above mentioned error?

3
1 answer                              3                         

Hope the below code must work and your error might be solved,

<?php
$db = mysqli_connect("127.0.0.1","root","toor","mylib")
or die('Error connecting to MySQL server.');

/* get values from form */
$sid = $_REQUEST['stuid'];

/* execute query */
$query = "SELECT * FROM book WHERE bookid IN  (SELECT bookid FROM studentbook  WHERE studid = '$sid')";
mysqli_query($db, $query) or die('Error querying database.');
$result = mysqli_query($db, $query);
$row = mysqli_fetch_row($result);
foreach($row as $a)
    print($a." ");
?>

Now your data from Login Page would get stored in $sid variable and will be interpolated in the query. So, if your database is connected and everything works fine, you could see the output stored in $row array.

1
Sunday February 26, 2017
source posted here