I am working on a project where i have to display data from the mysql which i have done successfully but now i need to sort the result based on high views, low views, newly added and old, all this options using select dropdown menu.
Html file
<form action="index.php" method="post" >
<select name="q">
<option value="DESC" name="DESC">High views</option>
<option value="ASC" name="ASC">Low views</option>
</select>
</form>
PHP File
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "movie_db";
$q = $_Post['q'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `title`, `poster`, `descrip`, `movie` ,`Duration`, `views`,`director` FROM `movie_db` ORDER BY `views` ".$q ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<p1>";
// output data of each row
while($row = $result->fetch_assoc())
echo " <div class=\"imgc\"><img src =" . $row["poster"]. "></div></p1><p2><b>" . $row["title"]. "</b></p2><br><p3><b>Duration : </b>".$row["Duration"]." Mins</p3> <p3><b> Views : </b> ".$row["views"]."</p3><br><div><p3><b>Description :</b>".$row["descrip"]."</p3></div><br><p5> By ".$row["director"]."</p5><br>";
} else {
echo "0 results";
}
$conn->close();
?>
</div>
Php and html are in the same file that is index.php
$q = $_GET['q'];
^
Compare that with
<form action="index.php" method="post" >
^
And you will GET your answer yourself.
You are submit from by POST and get data by GET
Try this
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "movie_db";
$q = $_POST['q'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `title`, `poster`, `descrip`, `movie` ,`Duration`, `views`,`director` FROM `movie_db` ORDER BY `views` ".$q ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<p1>";
// output data of each row
while($row = $result->fetch_assoc())
echo " <div class=\"imgc\"><img src =" . $row["poster"]. "></div></p1><p2><b>" . $row["title"]. "</b></p2><br><p3><b>Duration : </b>".$row["Duration"]." Mins</p3> <p3><b> Views : </b> ".$row["views"]."</p3><br><div><p3><b>Description :</b>".$row["descrip"]."</p3></div><br><p5> By ".$row["director"]."</p5><br>";
} else {
echo "0 results";
}
$conn->close();
?>
</div>
Add a input type "submit" in the form.
Php and html are in the same file that is index.php, in that case leave the action = "" empty:
<form method="post" action = "">
<select name="q">
<option value="DESC" name="DESC">High views</option>
<option value="ASC" name="ASC">Low views</option>
</select>
<input type = "submit" value = "Submit">
</form>
Next thing, change:
$q = $_GET['q'];
to this:
$q = $_POST['q'];
Since you're using the method = "POST" in your form, see below:
<form method="post" action = "">
The problem is that the form uses POST whereas you are reading GET in php
Either change
<form method="post" action = "">
to
<form method="get" action = "">
Or change this
$q = $_POST['q'];
to this
$q = $_GET['q'];
Related
I have been trying to insert some data into a table in DB, but the error came at where one of my fields is GET variable from the anchor tag.
To get a clear understanding please take a look at my code:
index page: with a post value of id = 1
<form name="user_form" method="post" action="input.php">
<?php
$sql2 = "SELECT * FROM singleq ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$id = $row2['id'];
$cat = $row2['cat'];
?>
<tr >
<input type="hidden" name="q_id" id="q_id" value="<?php echo ($row2["id"]); ?>" />
<input type="hidden" name="tablum" id="tablum" value="singleq" />
<input type="hidden" name="test_id" id="test_id" value="<?php echo $_GET['id'] ?>" />
<td><a href='singleqbanksub.php?id=<?php echo $row2['id'];?>'><?php echo substr(($row2["question"]),0,100) ?>...</a></td>
<td><button class="button button1" type="submit" name="insert-data" id="insert-data" style="width:180px;">Add Question</button></td>
</tr>
<?php }
} else {
echo "";
}
?>
</form>
input.php :
<?php include('db.php'); ?>
<?php
$q_id = $_POST['q_id'];
$test_id = $_POST['test_id'];
$tablum = $_POST['tablum'];
$date = date_default_timezone_set('America/New_York');
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO english (cat, test_id, q_id, tablum)
VALUES ('$cat', '$test_id', '$q_id', '$tablum')";
if ($conn->query($sql) === TRUE) {
header('Location: index.php?id='echo "$test_id";'');
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Here as I tried to pass the id through the anchor tag redirection to the same page to loop the process, it shows an error about an unidentified echo.
You are using echo inside header function which is wrong , use like this:
header('Location: index.php?id='.$test_id);
When the combobox is select, i'm trying to display first n items from database
i'm trying to call the PHP function "produse($_POST)" with the value of the selected item in the select tag, the code is correct?
the limit in the SELECT SQL can be the one in the code? ("$sql = "SELECT * FROM table LIMIT $number";")
<body>
<select name="n" onchange="document.write('<?php produse($_POST); ?>')">
<option disabled selected value> -- select an option -- </option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
<br> <br> <br>
<?php
function produse($number){
$servername = "localhost:3306";
$username = "root";
$password = "";
$dbname = "produse";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$number = intval($_GET['number']);
$sql = "SELECT * FROM table LIMIT $number";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id_produs: " . $row["id_produs"]. " - Denumire: " . $row["Denumire"]. " - Pret " . $row["Pret"]. " - Descriere" . $row["Descriere"] ."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
?>
</body>
Use it separately using jQuery like this:
HTML (html_page.html) :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Your HTML Page title</title>
</head>
<body>
<div>
<select id="my_select">
<option value="" selected>Choose</option>
<option value="1">Option1</option>
<option value="2">Option2</option>
</select>
</div>
<hr />
<div class="dynamic_content"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$('#my_select').change(function() {
showResult($(this).val());
});
function showResult(value) {
if (value) {
$('.dynamic_content').load('php_page.php?number=' + value);
}
}
</script>
</body>
</html>
PHP Page (php_page.php):
<?php
//intval($_GET['item']) will return 0 and considered as FALSE if the requested parameter is not a number
if (isset($_GET['number']) && !empty($_GET['number']) && intval($_GET['number']) {
$number = intval($_GET['number']);
//I DID NOT TEST YOUR PHP CODE
$servername = "localhost:3306";
$username = "root";
$password = "";
$dbname = "produse";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM table LIMIT $number";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id_produs: " . $row["id_produs"]. " - Denumire: " . $row["Denumire"]. " - Pret " . $row["Pret"]. " - Descriere" . $row["Descriere"] ."<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
}
?>
I'm trying to create a web form which lists all of the customers and then gives you a text field with a button next to it where you can add customers. Then it should show the list of customers with delete buttons next to them where you can click to delete the customer from the database.
I'm having getting this to work. For starters it's echoing the contents of one of the PHP script. I'm not sure what I need to do.
Here's my index.php file:
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "manager";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT url from customers";
$result = $conn->query($sql);
$tempArray = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$tempArray[] = $row["url"];
}
} else {
echo "0 results";
}
$conn->close();
?>
<table>
<tr>
<td><u>URL</u></td>
<td><u>Action</u></td>
</tr>
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form></td>
</tr>
<?php endforeach; ?>
</table>
<form action="add_customer.php" method="get">
<input type="text" name="url"> <input type="submit" name="add" value="Add Customer"/>
</form>
</body>
</html>
Here's my add_customers.php file:
<html> <body>
Added <?php echo $_GET['url']; ?><br>
<?php
$servername = "localhost"; $username = "root"; $password = "test123"; $dbname = "manager";
// Create connection $conn = new mysqli($servername,$username,$password,$dbname); // Check connection if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); }
$sql = "INSERT INTO customers (url) VALUES ('$url')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"; } else {
echo "Error: " . $sql . "<br>" . $conn->error; }
$conn->close(); ?>
</body> </html>
Here's my disable_customer.php file:
<html>
<body>
<$php
session_start();
$SESSION['username']="Test";
$SESSION['authuser']=1;
$url = $_GET['url'];
echo "<br>" . $url . "<br>";
$servername = "localhost";
$username = "root";
$password = "test123";
$dbname = "manager";
// Create connection
$conn = new mysqli($servername,$username,$password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_REQUEST["btn_submit"])) {
echo "yyyyaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyy";
}
$sql = "DELETE FROM customers WHERE customers.url = " . "'$url'";
echo "---------------------\n";
echo $sql . "\n";
echo "---------------------\n";
if ($conn->query($sql) === TRUE) {
echo "Record successfully deleted.";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
</body>
</html>
try changing index.php from this:
<form action="disable_customer.php" method="get"><input type="submit" name="url" value="Disable Customer2"/></form>
to this:
<form action="disable_customer.php" method="get">
put the url in here: <input type="text" name="url"/>
<input type="submit" value="submit"/>
</form>
If that works - but you don't want the user to be entering their own urls - then you need to read those urls out of the database first:
Going back to your original code in index.php, change the foreach to output the value of the url into 'value' attribute of the button:
<?php foreach ($tempArray as $row) : ?>
<tr>
<td><?php echo $row; ?></td>
<td><form action="disable_customer.php" method="get"><input type="submit" name="url" value="<? echo $row['url'] ?>"/></form></td>
</tr>
<?php endforeach; ?>
I have generated a multiple choice question page, where it takes a random question from my qbanktable in my database. The CorrectAnswer to this question is also in the qbanktable. Here is the code for the question.php page
<form action="grades.php" method="post" id="quiz">
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "qbank";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT Question, AnswerA, AnswerB, AnswerC, AnswerD, AnswerE, CorrectAnswer FROM qbanktable ORDER BY RAND() LIMIT 1";
$result = $conn->query($sql);
?>
<h3>
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["Question"];
}
}
?>
</h3>
<p> </p>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
<label for="question-1-answers-A">A)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerA"];
}
}
?>
</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
<label for="question-1-answers-B3">B)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerB"];
}
}
?>
</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
<label for="question-1-answers-C3">C)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerC"];
}
}
?>
</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
<label for="question-1-answers-D3">D)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerD"];
}
}
?>
</label>
</div>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="E" />
<label for="question-1-answers-D3">E)
<?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["AnswerE"];
}
}
?>
</label>
</div>
<div>
<input type="hidden" name="question-1-correct_answer" id="question-1-correct-answer" value="$row["CorrectAnswer"]" >
</div>
</ol>
<input type="submit" class="hvr-grow" value="SUBMIT" />
</form>
Now after I press the submit button, it will direct me to grades.php where it will analyze the CorrectAnswer:
<?php
$correctAnswer = $_POST['question-1-correct_answer'];
$answer1 = $_POST['question-1-answer'];
if ($answer1 == $correctAnswer) {
echo "<img src=correct.svg";
}
else {
echo "<img src=wrong.svg";
}
?>
I believe now in the grades.php (code above) I am messing something simple. I was wondering what is the correct code to match the CorrectAnswer with the user answer? Thank you
Multiple ways:
Send the question id in a hidden input and requery the database to get the Correct answer, this requires you to edit the query to also get the question ID
<input type="hidden" name="question-id" id="question-id" value="$row["id"]" >
In the grades.php you can then use to requery the db to get all the answers (especially the correct one)
$questionID= $_POST['question-id'];
The second one is easier but less secure, so try the first option first
You could send it using an hidden input type, but as already commented by #khuderm, this is a bad idea, because someone with little skills could see the answer in the source. I don't think anyone would actually try it, but better safe then sorry.
<input type="hidden" name="question-1-correct_answer" id="question-1-correct-answer" value="$row["CorrectAnswer"]" >
In the grades.php you will need something like this:
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "qbank";
$questionID= $_POST['question-id'];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT CorrectAnswer FROM qbanktable where id = $questionID";
$result = $conn->query($sql);
So after a few modifications, now it works.
1) I had to include the label for the hidden input in php as follows:
<input type="hidden" name="question-id" id="question-id" value= <?php
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["id"];
}
}
?> >
Now it gets the id row from my table and send it to the grades.php
2) In grades.php the code to finally check the user input with the correct answer from question id provided from the last page:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "qbank";
$questionID= $_POST['question-id'];
$answer1 = $_POST['question-1-answers'];
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT CorrectAnswer FROM qbanktable WHERE id = $questionID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$correct = $row["CorrectAnswer"];
}
}
if ($answer1 == $correct) {
echo "<img src=correct.svg";
}
else {
echo "<img src=wrong.svg";
}
?>
Now it works fine.
Special thanks to #davejal
Why this code is not update information?
HTML Form:
<form>
<lable> ID# :</lable>
<input id= "ID" name= "ID" type= "text">
<p>
<label>Select field to Edit</label>
<select name="change">
<option value=""></option>
<option value="fname">First Name</option>
<option value="lname">Last Name</option>
<option value="email">Email</option>
<option value="city">City</option>
<option value="zip">Zip</option>
</select>
<lable> Enter the value to be replaced </label>
<input id = "replace" name = "replace" type = "text">
</p>
<input name="submit" type="submit" value="Submit">
PHP Code for updating information from database:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$conn = mysql_connect($servername,$username,$password);
if(!$conn)
{
die('Error!' . mysqli_error());
}
$sql = 'SELECT * FROM users';
mysql_select_db('mitsdatabase');
$retval = mysql_query($sql, $conn);
if(! $retval)
{
die('Could not get data:' . mysql_error());
}
echo "<table width='300' cellpadding='5' border='1'>";
echo "<tr> <td>ID#</td> <td>FirstName</td> <td>LastName</td> <td> Email </td> <td> City </td> <td> State </td> <td> Zip </td> </tr>";
while($row = mysql_fetch_array($retval,MYSQL_ASSOC))
{
echo "<tr> <td>{$row['ID']}</td> . <td>{$row['fname']}</td> . <td>{$row['lname']}</td> . <td>{$row['email']}</td> . <td>{$row['city']}</td> . <td>{$row['state']} </td>. <td>{$row['zip']}</td>";
}
echo "</table>";
$db_id = $_POST['ID'];
$db_select = $_POST['change'];
$db_replace= $_POST['replace'];
echo " Do you want to edit any entry?";
if(!_POST['submit'])
{
echo " ";
}
else{
mysqli_query("UPDATE users SET db_select='$db_replace' WHERE ID = $db_id ");
}
mysql_close($conn);
?>
I want to update informate selected from select field but somehow it is not doing any thing. Can someone help me what is wrong with this code.
Is your PHP on the same page as your HTML? If not, you are not directing to your php code within the <form> element in your HTML.
For example, if your PHP file was called 'myphpcode.php' (and in the same folder as your HTML code) then you could direct to it using the following:
<form method="post" action="myphpcode.php">
If you want to post to the same page just change <form> to <form method="post" action="#"> and get variables in php like this $nameofvar = $_POST['nameofinputfield'] . Each input field should have the name tag.
Also try to change your mysql connect to this :
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
and after you finished the query
$conn->close();
and the query to insert
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
and you can modify this $sql string to update or delete