As all have commented I have changed my code. Now the thing is when I am running my below php code as separate file its running like charm:
<?php
require("phpsqlajax_dbinfo.php");
$conn = new mysqli($hostname, $username, $password, $database);
$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] . "'>" . $row['username'] . "</option>";
}
echo "</select>";
?>
Like this:
But when I am trying to include this into html code its not working:
<!DOCTYPE html>
<html>
<head>
<title>FusionCharts Column 2D Sample</title>
</head>
<body>
<div>
<?php
require("phpsqlajax_dbinfo.php");
$conn = new mysqli($hostname, $username, $password, $database);
$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select name='username'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] . "'>" . $row['username'] . "</option>";
}
echo "</select>";
?>
</div>
<div id="chart-container">LOADING....</div>
<script src="js/jquery-2.2.4.js"></script>
<script src="js/fusioncharts.js"></script>
<script src="js/fusioncharts.charts.js"></script>
<script src="js/themes/fusioncharts.theme.zune.js"></script>
<script src="js/userChart.js"></script>
</body>
</html>
Its giving empty drop box:
Remove select inside select and don't mix mysqli_* with mysql_*.Do like below:-
<div>
<select>
<?php
require("phpsqlajax_dbinfo.php");
$conn = new mysqli($hostname, $username, $password, $database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT username FROM users";
$result = $conn->query($query);
?>
<?php
while ($line = $result->fetch_assoc()) {
?>
<option value="<?php echo $line['username'];?>"> <?php echo $line['username'];?> </option>
<?php
}
?>
</select>
</div>
Note:-
file extension must be .php not .html.
Don't use (deprecated + removed) mysql_* library. Use mysqli_* or PDO
Related
I'm trying to get the selected value from a dropdown list.
The list has name 'select_employee', when I press the button with name 'save' I hope to get the value. I'm using a POST to to get the value.
I get the error 'Undefined index: select_employee'.
<div class="form-group">
<h2>Enter Certified Course Details</h2>
<?php
// start of connect db
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "TrainingDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//end of connect db
?>
<form id="form" action="" method="post">
<?php
// Drop down list employee
$sql = "SELECT * FROM employees";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "Select Employee<br>";
echo "<select name='select_employee' id='select_employee'>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row['id'] . "'>" . $row['fname'] . " " . $row['sname'] . " </option>";
}
echo "</select>";
echo "<br>";
}
?>
</form>
<?php
if(isset($_POST['save'])){
echo "save<br>";
$employee=$_POST['select_employee']; // error here
echo "Selected Employee" . $employee . "<br>";
}
?>
<form Employee="/employee_page.php" method="post">
<button type="submit" class="btn btn-primary" name="save" value="save">Save</button>
</form>
</div>
You are using two different form so make sure to use only one form.
Im just starting out with MySQL and PHP. Im trying to create a drop-down menu, to pick a certain competence within a company.
Later on, i want a total of 3 dropdowns, so that i can combine user/competence/avaliable date, to display sort of a calendar to show which users are avaliable a certain date, with information about their competence.
However, the code below just returns this:
Result
The same query to the database returns 12 values.
What am i doing wrong? I get no errors, just a blank drop-down.
<!DOCTYPE html>
<html>
<head>
<title> Greetings. </title>
<meta charset="UTF-8"/>
</head>
<body>
<?php
session_start();
$con = mysqli_connect("127.0.0.1", "root", "", "service");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
echo "<br />";
$query_kompetens = "SELECT kompetens FROM kompetens";
$kompetens = mysqli_query($con, $query_kompetens);
echo "<select name='Kompetens'>";
echo "<option size =30 ></option>";
while ($row = mysqli_fetch_array($kompetens)) {
echo "<option value='" . $row['Kompetens'] . "'>" . $row['Kompetens'] . "</option>";
}
echo "</select>";
;
?>
</body>
</html>
Try This code, actually you used $row['Kompetens'] instead of $row['kompetens'].
<!DOCTYPE html>
<html>
<head>
<title> Greetings. </title>
<meta charset="UTF-8"/>
</head>
<body>
<?php
session_start();
$con = mysqli_connect("127.0.0.1", "root", "", "servicedesk");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
echo "Connected successfully";
echo "<br />";
$query_kompetens = "SELECT kompetens FROM kompetens";
$kompetens = mysqli_query($con, $query_kompetens);
echo "<select name='Kompetens'>";
echo "<option size =30 ></option>";
while ($row = mysqli_fetch_array($kompetens)) {
echo "<option value='" . $row['kompetens'] . "'>" . $row['kompetens'] . "</option>";
}
echo "</select>";
;
?>
</body>
</html>
I had inserted the data from html and it is stored in mysql database, and the data is retrieved from database to html,now I had done how to delete the data that are displayed in html,my task is how to update the displayed.And my deleted code is:
Below is my HTML code:
<html>
<head>
<title>STUDENT_DATA</title>
</head>
<body>
<form action="tab.php" method="post" >
<center>
sname: <input type="text" name="sname" required><br></br>
sno:<input type="text" name="sno"><br></br>
marks:<input type="text" name="marks"><br></br>
class:<input type="text" name="class"><br></br>
phno:<input type="text" name="phno" onkeypress='return event.charCode >
= 48 && event.charCode <= 57'><br></br>
DOB:<input type="date" placeholder="DD-MM-YYYY"
required pattern="(0[1-9]|1[0-9]|2[0-9]|3[01]).(0[1-9]|1[012]).[0-9]{4}"
name="DOB"/><br></br>
<button>submit</button></br>
</center>
</form>
</body>
</html>
Below is my PHP code for displaying the data:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "student",$connection);
if (!$select_db)
{
die("Database Selection Failed" . mysql_error());
}
$sql = "SELECT * FROM hello1 ";
$result = mysql_query($sql) or die(mysql_error());
?>
<table border="2" style= " margin: 0 auto;" id="myTable">
<thead>
<tr>
<th>sname</th>
<th>sno</th>
<th>marks</th>
<th>class</th>
<th>phno</th>
<th>DOB</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['sname'] . "</td>";
echo "<td>" . $row['sno'] . "</td>";
echo "<td>" . $row['marks'] . "</td>";
echo "<td>" . $row['class'] . "</td>";
echo "<td>" . $row['phno'] . "</td>";
echo "<td>" . $row['DOB'] . "</td>";
echo "<td><a href='delete.php?did=".$row['sname']."'>Delete</a></td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
</html>
Below is my PHP code for deleting:
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection)
{
die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "student",$connection);
if (!$select_db)
{
die("Database Selection Failed" . mysql_error());
}
?>
<?php
if(isset($_GET['did'])) {
$delete_id = $_GET['did'];
$sql = mysql_query("DELETE FROM hello1 WHERE sname = '".$delete_id."'");
if($sql) {
echo "<br/><br/><span>deleted successfully...!!</span>";
}
else
{
echo "ERROR";
}
}
?>
Below is what I have tried in update.php
<?php
$connection = mysql_connect('localhost', 'root','');
if (!$connection) { die("Database Connection Failed" . mysql_error());
}
$select_db = mysql_select_db( "emp",$connection);
if (!$select_db) { die("Database Selection Failed" . mysql_error()); } ?>
<?php if(isset($_GET['did']))
{
$update_id = $_GET['did']; $sql = mysql_query("UPDATE FROM venu WHERE id = '".$update_id."'");
if($sql) {
echo "<br/><br/><span>updated successfully...!!</span>";
} else {
echo "ERROR";
}
} ?>
I filled my dropdown menu with data from the database. I can select here the name of the product. I have a second textbox on my page. Here i want to show the price of the selected product. I tried different ways but I o net get it working.
First of all this is my dropdown menu where the product name is showed:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM form";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option onchange='OnChange()' value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
I want to show the price of the selected value in this textbox:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbsi";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT price FROM form WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<input type='text' class='form-control' name='price1' id='price1' onkeyup='getValues()' value='" . $row["price1"]. "'>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Using this script i only get "0 results" in price1. It doesnt update when i change the selected value of the dropdown menu. How can I ensure that price1 gets updated when i select another value in the dropdown menu?
Update 1:
I tried to add the the following script
<script>
function OnChange(){
UpdatePoints(<?php echo $price1; ?>);
}
echo "<script type='text/javascript'> window.onchange=load; </script>";
</script>
When I use this script i still get "0 results"
Update 2:
The following is still not working:
//filename: test.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<select class='form-control select2' id='product1' name='product1' onChange='getstate(this.value);' style='width: 100%;'>";
echo "<option selected disabled hidden value=''></option>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<option value='" . $row["id"]. "'>" . $row["name"]. "</option>";
}
echo "</select>";
} else {
echo "0 results";
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM forms WHERE id='". $product1 ."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div id='price-list'>";
echo "<input type='text' class='form-control' name='price1' id='price1' value='" . $row["price"]. "'>";
echo "</div>";
}
} else {
echo "0 results";
}
$conn->close();
?>
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "test.php",
data:'id='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<?php
$product1=$_POST['price1'];
?>
make a ajax call and get the value of using post
<script>
function getprice(val) {
$.ajax({
type: "POST",
url: "index.php",
data:'priceid='+val,
success: function(data){
$("#price-list").html(data);
}
});
}
</script>
<select class='form-control select2' id='product1' name='product1' style='width: 100%;' onChange="getstate(this.value);">
//yourcode
</select>
<div id="price-list">
</div>
index.php
<?php
$product1=$_POST['priceid'];
?>
I am just unable to understand why the rows are not getting deleted!
please note that i am getting the login values of corrected check boxes in the php page.
from my point of view, most probably error should be in php page where i am using
'DELETE FROM' query.
<?php
session_start();
?>
<html>
<head>
<form id="delete_customers" action="deletecustomers.php" method="post">
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("car_rental_system", $con);
$result = mysql_query("SELECT * FROM customer");
echo "<table border='1' align='center'>
<tr>
<th>first_name</th>
<th>Last_name</th>
<th>login</th>
</tr>";
while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['login'] . "</td>";
echo "<td>"."<input type='checkbox' name='deletingcustomers[]'
value=$row['login']}"."</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
<p class='submit'>
<button type='submit' name='dscustomer'>Delete selected</button>
</p>
</head>
</html>
//NOW deletecustomers.php
<?php
session_start();
$_SESSION['deletingcustomers'] = $_POST['deletingcustomers'];
$N = count($_SESSION['deletingcustomers']);
$con = mysql_connect("localhost","root","");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("car_rental_system", $con);
if(empty($_SESSION['deletingcustomers'])) {
echo("No customers selected");
} else {
for ($i=0; $i<$N; $i++) {
$sql1="delete from `customer`
where login='{$_SESSION[deletingcustomers][$i]}'";
if(mysql_query($sql1,$con))
echo 'executed';
}
}
?>
NO! No! Why do people keep using mysql_query().....(head desk)
Please look up PDO. http://php.net/manual/en/book.pdo.php it helps prevent sql injections and gives you a better understanding of how to harness oop's power.
Your $_SESSION[deletingcustomers][$i] needs to be $_SESSION['deletingcustomers'][$i]
Example on its way
$tempVar = $_SESSION['deletingcustomers'][$i];
$dbConnection = new PDO("mysql:host=".$hostName.";dbname=".$dbName, $username, $password);
$sql = "delete from `customer` where login='$tempVar'";
$stmt = $newObj->prepare($sql);
$stmt->execute();
Replace
echo "<td>"."<input type='checkbox' name='deletingcustomers[]' value=$row['login']}"."</td>";
To
echo "<td><input type='checkbox' name='deletingcustomers[]' value='".$row['login']."'</td>";
and try