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);
}
?>
Related
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'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'm running a simple database search against one table in my database. The results are then displayed in a table. If no results are found, the search shows a message that says "0 results", but sometimes it will echo the table headings without displaying any results. This search is part of a class project and isn't going to be an active database, so I haven't included any protection for SQL injection. Any help would be greatly appreciated.
<h2>Customer Search</h2>
<br>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If there is a search variable try to search database
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql = "SELECT * FROM Customers WHERE Client LIKE '%$searchq%'";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo '<table class="hoverTable"><tr><th>Client</th><th>Address</th><th>City</th><th>State</th><th>Zip Code<br></th><th>Phone</th></tr>';
// We have results! Go fetch rows!
while ($row = mysqli_fetch_row($result)) {
// This loop runs until there are no more results left to echo
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row["Address"]. "</td><td> " . $row["City"]. "</td><td> " . $row["State"]. "</td><td> " . $row["ZipCode"]. "</td><td> " . $row["Phone"]. "</td></tr>";
}
echo "</table>";
}
} else {
// No results from query
$message = "0 results";
}
/* free result set */
mysqli_free_result($result);
}
}
?>
</div>
</div>
<div class="center">
<?php
if(isset($message)){ echo $message; }
?>
</div>
</body>
</html>
I find, and you may disagree, but properly indenting your code as you go and opening and closing tags before filling in content help reduce the number of problems. Having said that now it'll probably not work!
<html>
<head>
<title>db search</title>
</head>
<body>
<div>
<div>
<h2>Customer Search</h2>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
$conn = new mysqli( $servername, $username, $password, $dbname );
if ( $conn->connect_error ) die("Connection failed: " . $conn->connect_error );
if( isset( $_POST['search'] ) ) {
$searchq = $_POST['search'];
$searchq = preg_replace( "#[^0-9a-z]#i", "", $searchq );
$sql = "SELECT * FROM `Customers` WHERE `Client` LIKE '%$searchq%';";
if ( $result = mysqli_query( $conn, $sql ) ) {
if ( mysqli_num_rows( $result ) > 0 ) {
echo '
<table class="hoverTable">
<tr>
<th>Client</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Phone</th>
</tr>';
while( $row = $result->fetch_assoc() ) {
echo "
<tr>
<td>".$row["Client"]."</td>
<td>".$row["Address"]."</td>
<td>".$row["City"]."</td>
<td>".$row["State"]."</td>
<td>".$row["ZipCode"]."</td>
<td>".$row["Phone"]."</td>
</tr>";
}
echo '
</table>';
} else {
$message = "0 results";
}
}
mysqli_free_result( $result );
}
?>
</div>
</div>
<div class="center">
<?php if( isset( $message ) ){ echo $message; } ?>
</div>
</body>
</html>
Delete the redundant first while loop while ($row = mysqli_fetch_row($result)) {, if the query had just 1 result the first loop would have fetched it only while the 2nd while loop would have fetched nothing resulting in just a header and no body of the table.
<h2>Customer Search</h2>
<br>
<p class="first">Search the Customer Database</p>
<form action="searchcustomers.php" method="post">
<input type="text" name="search" placeholder="Search...." />
<input type="submit" value=">>" />
</form>
<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If there is a search variable try to search database
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
$searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
$sql = "SELECT * FROM Customers WHERE Client LIKE '%$searchq%'";
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
echo '<table class="hoverTable"><tr><th>Client</th><th>Address</th><th>City</th><th>State</th><th>Zip Code<br></th><th>Phone</th></tr>';
// We have results! Go fetch rows!
// This loop runs until there are no more results left to echo
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row["Address"]. "</td><td> " . $row["City"]. "</td><td> " . $row["State"]. "</td><td> " . $row["ZipCode"]. "</td><td> " . $row["Phone"]. "</td></tr>";
}
echo "</table>";
} else {
// No results from query
$message = "0 results";
}
/* free result set */
mysqli_free_result($result);
}
}
?>
</div>
</div>
<div class="center">
<?php
if(isset($message)){ echo $message; }
?>
</div>
</body>
</html>
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'];
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