Populate listbox with data from database - php

<?php
ini_set("display_errors","on");
$conn = new COM("ADODB.Connection");
try {
$myServer = "WTCPHFILESRV\WTCPHINV";
$myUser = "sa";
$myPass = "P#ssw0rd";
$myDB = "wtcphitinventory";
$connStr = "...conn string...";
$conn->open($connStr);
if (! $conn) {
throw new Exception("Could not connect!");
}
}
catch (Exception $e) {
echo "Error (File:): ".$e->getMessage()."<br>";
}
if (!$conn)
{exit("Connection Failed: " . $conn);}
$sql_exp = "select * from dbo.PC";
$rs = $conn->Execute($sql_exp);
echo "<table><tr><th>Desktop Number</th></th></tr>";
while (!$rs->EOF) {
set_time_limit(0);
echo "<td>CP # <br>".$rs->Fields("PC_Number")."</td>";
$rs->MoveNext();
}
echo "</table>";
$rs->Close();
?>
i would like to have a list box instead of just a print of all the data of PC_Number from database. All i could do is to echo it and im having a hard time inserting a select. I would like to have a form method post along with a select(listbox)

Create the list box in the while statement.
echo "<table border='1' cellpadding='1' cellspacing='0' id='rounded-corner'><tr><th>Desktop Number</th></th></tr>";
echo "<tr><td><select name='selectionField'>";
while (!$rs->EOF) {
set_time_limit(0);
echo "<option value=".$rs->Fields('PC_Number')." >".$rs->Fields('PC_Number')."</option>";
$rs->MoveNext();
}
echo "</td></tr></table>";

Related

Table row delete button

When I try the delete button I get no errors and the page refreshes but the data won't be deleted, so I think the problem is in passing along the id from a row.
None of the other solutions have worked for me so far. This is the table body.
<tbody>
<?php
$server = "localhost";
$user = "Website";
$pass = "pass";
$db = "db";
$conn = new mysqli($server, $user, $pass, $db);
if ($conn->connect_error)
{
die("connection to database failed");
}
$query = "SELECT * FROM koppeling";
$result = mysqli_query($conn, $query);
while($row = $result-> fetch_assoc())
{
echo "<tr>";
echo "<td>".$row['Wagon_ID']."</td>";
echo "<td name='wagon'>".$row['EPC']."
<a href='delete.php' class='table-button'
id='".$row['id']."'>Delete</a>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
?>
</tbody>
delete.php file:
<?php
$id = $_GET['id'];
$server = "localhost";
$user = "Website";
$pass = "pass";
$db = "db";
$conn = new mysqli($server, $user, $pass, $db);
if ($conn->connect_error)
{
echo "Conn db failed";``
}
$query = "DELETE FROM koppeling WHERE id='$id';";
try
{
mysqli_query($conn, $query);
mysqli_close($conn);
header('Location: koppelen.php');
}
catch (Exception $e)
{
echo "$e";
}
?>
Can anyone help me figure out what is wrong, I've been stuck on this quite a while now.
Instead if this
while($row = $result-> fetch_assoc())
{
echo "<tr>";
echo "<td>".$row['Wagon_ID']."</td>";
echo "<td name='wagon'>".$row['EPC']."
<a href='delete.php' class='table-button'
id='".$row['id']."'>Delete</a>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
Write this
while($row = $result-> fetch_assoc())
{
echo "<tr>";
echo "<td>".$row['Wagon_ID']."</td>";
echo "<td name='wagon'>".$row['EPC']."
<a href='delete.php?id=".$row['id']."' class='table-button'
id='".$row['id']."'>Delete</a>";
echo "<td>";
echo "</td>";
echo "</tr>";
}
It would be better if you used form with POST request or ajax instead of links.
If you really want to do it this way you can add query string to your href attribute.
$url = "delete.php?id=" . $row['id'];
<a href="<?php echo $url ?>" class='table-button'
id='".."'>Delete</a>";
You have to change the below line
$query = "DELETE FROM koppeling WHERE id='$id';";
to
$query = "DELETE FROM koppeling WHERE id='".$id."';";
so that id value will be properly populated in the query

Show list of users in my database [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 2 years ago.
Hey guys i am trying to display a list off all users in my database. not sure how to get along with it. Getting confused with mysqli and sql..
my dadtabase connection file is :
class Dbconnect extends PDO
{
private $dbengine = 'mysql';
private $dbhost = 'localhost';
private $dbuser = 'root';
private $dbpassword = '';
private $dbname = 'test';
public $dbh = null;
function __construct()
{
try{
$this->dbh = new PDO("".$this->dbengine.":host=$this->dbhost; dbname=$this->dbname", $this->dbuser, $this->dbpassword);
$this->dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
catch (PDOException $e){
$e->getMessage();
}
}
}
?>
I tired this on it but getting an error.
$sqlget = "SELECT * FROM user";
$sqldata = mysqli_query($dbh, $sqlget) or die('error users');
echo "<table>";
echo " <th>User Name </th> <th>Email</th> <th> Some Name</th>";
while($row = mysql_fetch_array($sqldata, MYSQL_ASSOC)) {
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['email'];
echo "</td><td>";
echo $row['some'];
echo "</td>";
}
echo "</table>"
?>
I have also created a table to insert the data on them..
User Name . Email . lastName
not sure how to proceed. especially as i just got into php.. help will be much appreciaited to come up with a php handling file for me.
and any further tips to delete or edit users...Thanks in Advance people..!!!!!!
You must use PDO functions:
$db = new PDO("".$dbengine.":host=$dbhost; dbname=$dbname", $dbuser, $dbpassword);
$q = "SELECT * FROM users";
$result = $db->query($q)->fetchall();
foreach ($result as $user) {
echo $user['name'];
}
Try this:
$dbh = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4', 'root', '');
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$sqlget = "SELECT * FROM user";
$sqldata = $dbh->query($sqlget) or die('error users');
echo "<table>";
echo " <th>User Name </th> <th>Email</th> <th> Some Name</th>";
while($row = $sqldata->fetch(PDO::FETCH_ASSOC)) {
echo "<tr><td>";
echo $row['name'];
echo "</td><td>";
echo $row['email'];
echo "</td><td>";
echo $row['some'];
echo "</td>";
}
echo "</table>"

How to save a select option loaded from database in PHP

[sample][1]
Good day,
I successfully fetched the values from the database **(db) for the select option using mysqli
but
the problem is whenever I try save it in the database it doesn't retrieve the value selected in select the option .
Can you please give any suggestions on how to deal with this.
<?php
$conn = new mysqli('localhost', 'root', '', 'db') ;
if (mysqli_connect_error()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
} else {
echo"db_connection.php RUNNING<br>";
}
/* CONNECTION IN DATABASE*/
/* WHILE LOOP FOR SELECT OPTION IN DATABASE*/
$result = $conn->query("select b_fname from tbl_client");
echo "<html>";
echo "<body>";
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
unset($b_id, $b_fname);
$b_id = $row['b_id'];
$b_fname = $row['b_fname'];
echo '<form action ="dropdown_demo.php" method="POST" enctype="multipart/form-data" >';
echo '<option name="b_fname" value="/'.$b_fname.'/">'.$b_fname.'.'.$b_fname.'</option>';
echo " </form>";
}
echo '</select><input type="submit" name="add_drop" />';
echo "</body>";
echo "</html>";
/* SQL INSERT THE VALUE TO THE */
$sql = "INSERT INTO tbl_client (b_fname)VALUES ( '$b_fname' )";
if (mysqli_query($conn, $sql)) {
echo('<script>alert("Record Added Successfully!");</script>');
// header('Refresh: 1; URL= import_addnew-Copy.php');
error_reporting(0);
} else {
error_reporting(0);
}
mysqli_close($conn);
?>
I have made some changes in your code. pu the tag out side while loop etc...
Try the below code:
$conn = new mysqli('localhost', 'root', '', 'db') ;
if (mysqli_connect_error())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}else
{
echo"db_connection.php RUNNING<br>";
}
/* CONNECTION IN DATABASE*/
/* WHILE LOOP FOR SELECT OPTION IN DATABASE*/
$result = $conn->query("select b_fname from tbl_client");
echo "<html>";
echo "<body>";
echo '<form action ="dropdown_demo.php" method="POST" enctype="multipart/form-data" >';
echo '<select name="b_fname" >';
while ($row = $result->fetch_assoc()) {
unset($b_id, $b_fname);
$b_id = $row['b_id'];
$b_fname = $row['b_fname'];
echo '<option value="'.$b_fname.'">'.$b_fname.'.'.$b_fname.'</option>';
}
echo '</select><input type="submit" name="add_drop" />';
echo " </form>";
echo "</body>";
echo "</html>";
/* SQL INSERT THE VALUE TO THE */
if(isset($_POST['add_drop'])){
$b_fname = $_POST["b_fname"];
$sql = "INSERT INTO tbl_client (b_fname) VALUES ( '".$b_fname."' )";
if (mysqli_query($conn, $sql)) {
echo('<script>alert("Record Added Successfully!");</script>');
// header('Refresh: 1; URL= import_addnew-Copy.php');
error_reporting(0);
} else {
error_reporting(0);
}
}
mysqli_close($conn);

Mysqli query returns no rows

For whatever reason, I keep getting the echo "No News."; even though I clearly have information put into the table which is named news.
<?php
session_start();
define('DB_SERVER', "localhost");
define('DB_USER', "USERNAME");
define('DB_PASSWORD', "PASSWORD");
define('DB_DATABASE', "DATABASE");
$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE);
// Check connection
if(mysqli_connect_errno()) {
echo "Email Owner#OtherTXT.com";
exit();
}
/* create a prepared statement */
$query = "SELECT `title`, `message`, `date` FROM `news`";
$result = $mysqli->query($query);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h2>";
echo ($row["title"]);
echo "</h2>";
echo "<h3>";
echo ($row["date"]);
echo "</h3>";
echo "<br />";
echo "<p>";
echo ($row["message"]);
echo "</p>";
}
} else {
echo "No News.";
}
$mysqli->close();
?>
This is a picture of my table
Hope this will help ;)
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h2>";
echo ($row["title"]);
echo "</h2>";
echo "<h3>";
echo ($row["date"]);
echo "</h3>";
echo "<br />";
echo "<p>";
echo ($row["message"]);
echo "</p>";
}
} else {
echo "No News.";
}
Maybe adding a store_result before calling the num rows do the trick:
$result = $mysqli->query($query);
$result->store_result();
if ($result->num_rows > 0) {
...
Or you can put it into the query:
$result = $mysqli->query($query,MYSQLI_STORE_RESULT);

trouble retrieving data from sql database with php

I'm pretty new to php, and I'm teaching myself. I've looked at a few different resources, and the php script I have now doesn't return any critical errors when executed, but its not returning the data from the table.
<?php
$connect = mysqli_connect("localhost","*","*","*");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$comments = "SELECT * FROM commentstable";
$rs = mysqli_query($connect,$comments);
$fetch = mysqli_fetch_array($rs);
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
echo $fetch;
mysqli_close($connect);
echo "hello";
?>
you have double entry:
$fetch = mysqli_fetch_array($rs); //<--- remove this as you are calling it again in the while loop
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
Check this
$connect = mysqli_connect("localhost","turlough","samus1","comments");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
$comments = "SELECT * FROM commentstable";
$rs = mysqli_query($connect,$comments);
if($rs)
{
while($fetch = mysqli_fetch_array($rs)) {
echo $fetch['comments'];
}
}
else
{
// no results from query
}
mysqli_close($connect);
}

Categories