connecting php with postgresql - php

I am a newcomer and have just started building a website.. I wrote the following code but its not working.it is not connecting to the database..If not this is there any other way to connect dreamweaver to postgresql ...please help..thnx in advance
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="span6">
<table class="table table-striped">
<thead>
<tr>
<th>Scheme-ID</th>
<th>Scheme Name</th>
<th>Scheme Type</th>
</tr>
</thead>
<tbody>
<?PHP
$conn = pg_connect("host=localhost port=5432 dbname=egram user=postgres password=password") or die("unable to connect to database");
if ($conn)
{
echo ("Connection attempt succeeded.");
}
else {
echo ("Connection attempt failed.");
}
$sql = "SELECT scheme_id,scheme_name,scheme_type FROM schemes";
$result = pg_query($conn,$sql); //order executes
if($result)
{
echo("query ran successfully");
while($data = pg_fetch_row($result))
{
echo("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td></tr>");
}
}
else
{
echo("error in running query");
}
?>
</tbody>
</table>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
#
</body>
</html>

Related

Aligning data in a table according to each characteristics in php

I get data from a mssql database that I want to display in a table.
The table has three categories:
ORDER NUMBER
PAYMENT STATUS
ORDER STATUS
The problem is as follows:
I would like to align my order numbers with the payment status and the order status in order to have a nice table that is well aligned.
Here is my code in php and the result it gives me :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/clientsS.css">
<title></title>
</head>
<body>
<div class="BLOC1">
<div class="NUMBER">
<center><table>
<tr>
<th>NUMERO COMMANDE</th>
<th>STATUT PAIEMENT</th>
<th>STATUT COMMANDE</th>
</tr>
<tr>
<td><?php
//CONNEXION ODBC SERVER//
$dsn="";
$user="";
$password="";
$conn=odbc_connect($dsn
,$user, $password);
//REQUETES
$sql = <<<EOF
SELECT top 10 [enc_cmd_num]
FROM [encaissement]
WHERE enc_date= '20221220'
EOF;
$results = odbc_exec($conn,$sql);
while($resultrow = odbc_fetch_array($results)){
echo $resultrow["enc_cmd_num"]."<br/>" ; }
?>
</td>
</div>
<div class="TITRE">
<td><?php
//CONNEXION ODBC SERVER//
$dsn="";
$user="";
$password="";
$conn=odbc_connect($dsn,$user, $password);
//REQUETES
$sql = <<<EOF
SELECT top 10 [enc_paye]
FROM [encaissement]
WHERE enc_date= '20221220'
EOF;
$results = odbc_exec($conn,$sql);
//CONDITION
while($resultrow = odbc_fetch_array($results)) {
switch($resultrow['enc_paye']){
case 0:
echo "<p>En attente paiement</p> \r\n";
break;
case 1:
echo "<p class='green'>Commande payée<p/>\r\n";
break;
}
}
?>
</td>
<td><?php
//CONNEXION ODBC SERVER//
$dsn="";
$user="";
$password="";
$conn=odbc_connect($dsn,$user, $password);
//REQUETES
$sql = <<<EOF
SELECT top 10 [enc_prepared]
FROM [encaissement]
WHERE enc_date= '20221220'
EOF;
$results = odbc_exec($conn,$sql);
//CONDITION
while($resultrow = odbc_fetch_array($results)) {
switch($resultrow['enc_prepared']){
case 0:
echo "<p>Commande en attente</p> \r\n";
break;
case 1:
echo "<p class='yellow'>Commande en cours de préparation<p/>\r\n";
break;
}
}
?>
</td>
</tr>
</table>
</div>
It is preferable that you use the same tag <p> even for the first <td>:
Test it like this and tell me if it works for you:
echo "<p>".$resultrow["enc_cmd_num"]."<p/>" ;
Instead of:
echo $resultrow["enc_cmd_num"]."<br/>" ;

Data Tables is not working in PHP file in a table with contents read from text file

I have used PHP to read data from a local file, and inserted it into a table with PHP. However, the table is formatted without the Data Table features, as shown:
[HTML table without Data Tables formatting...][1]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.js"></script>
</head>
<body>
<table id = "display">
<thead>
<tr>
<th>Suburb</th>
<th>Postcode</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<?php
$file = fopen('data.txt','r');
while ($line = fgets($file)) {
list($suburb) = explode(",", $line);
list($_,$postcode) = explode(",",$line);
echo "<tr>
<td>" . $suburb . "</td>
<td>" . $postcode . "</td>
</tr>";
}
fclose($file);
?>
</tbody>
</table>
<script>
$(document).ready( function () {
$('#display').DataTable();
} );
</script>
</body>
</html>
Help would be greatly appreciated, thanks!
[1]: https://i.stack.imgur.com/TMDu9.png

Read out data and edit

i would like to read out all data records from a table and give individual data records a new value in "status" according to their "id".
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Table</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$conn = new mysqli ("localhost", "root", "Z&r*%/Nm#X4x", "cms");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, position, status FROM data";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach($data as $row) {
?>
<form method="post">
<div class="table-responsive d-table">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>POSITION</th>
<th>STATUS</th>
<th><button type="submit" name="change">CHANGE STATUS</button></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td>
<?php
if (isset($_POST['change'])) {
$update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
mysqli_query($conn, $update);
echo "Successful";
} elseif (!isset($_POST['change'])) {
echo "Not clicked";
} else {
echo "Failed";
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<?php
}
} else {
echo "No data found!";
$conn->close();
}
?>
</html>
My problem is, when I click the button, all records are changed and not just one. I hope someone can help to solve the problem.
.........................................................................................................................................................................................................
Try using {} to embed array elements into a string and enclose 'id' value
$update = "UPDATE data Set status = '0' WHERE id = {$row['id']}"
Otherwise, your query will interpret $row and [id] as separated items:
UPDATE data Set status = '0' WHERE id = [id]
and as a result, all your rows updated
Also consider to apply the status update button to each single row, there is no way to update only one row with a general "update status" button without sending a parameter of which row id will update. For example:
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Table</title>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css">
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$conn = new mysqli ("localhost", "root", "Z&r*%/Nm#X4x", "cms");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, position, status FROM data";
$stmt = $conn->prepare($sql);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_all(MYSQLI_ASSOC);
if ($data) {
foreach($data as $row) {
?>
<form method="post">
<div class="table-responsive d-table">
<table class="table table-bordered">
<thead>
<tr>
<th>ID</th>
<th>POSITION</th>
<th>STATUS</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row["position"]; ?></td>
<td><?php echo $row["status"]; ?></td>
<td>
<button type="submit" name="change" value="<?php echo $row['id']; ?>">CHANGE STATUS</button>
<?php
if (isset($_POST['change']) && $_POST["change"]==$row['id']) {
$update = $update = "UPDATE data Set status = '2' WHERE id = {$row['id']}";
mysqli_query($conn, $update);
echo "Successful";
} elseif (!isset($_POST['change'])) {
echo "Not clicked";
} else {
echo "Failed";
}
?>
</td>
</tr>
</tbody>
</table>
</div>
</form>
</body>
<?php
}
} else {
echo "No data found!";
$conn->close();
}
?>
</html>

Get specific data from database with PHP code when a user login

I want to get data from my database with PHP code. In brief when a user log in with id & password i want to show his data only in user panel, but i got some error: Here is my user admin panel code:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>User Panel</title>
<link rel="stylesheet" href="assets/demo.css">
<link rel="stylesheet" href="assets/form-labels-on-top.css">
</head>
<body>
<header>
<h1 align="center"><b>User Information Details</b></h1></br>
</header>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "xyz";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
include('user_login_check.php');
$result= mysql_query("SELECT * FROM `user_information` WHERE `user_id` = '".$_SESSION['id']."' ")or die(mysql_error());
// $result = mysql_query("SELECT * FROM user_information WHERE user_name='" . $_POST["user_name"] . "' and user_password = '". $_POST["user_password"]."'");
?>
<form>
<table border="5" style= "background-color: #333333; color: #FFF; margin: 0 auto; padding:100px" >
<thead>
<tr>
<th>User ID</th>
<th>User Type</th>
<th>User Name</th>
<th>User Country</th>
<th>User Email</th>
<th>User Phone</th>
<td>User Address</td>
<td>User Password</td>
<td>User Status</td>
<td>Edit</td>
</tr>
</thead>
<tbody>
<?php
while( $row = mysqli_fetch_assoc( $result ) ){
echo
"<tr>
<td>{$row['user_id']}</td>
<td>{$row['user_type']}</td>
<td>{$row['user_name']}</td>
<td>{$row['user_country']}</td>
<td>{$row['user_email']}</td>
<td>{$row['user_phone']}</td>
<td>{$row['user_address']}</td>
<td>{$row['user_password']}</td>
<td>{$row['user_status']}</td>
<td><img src='image/editicon.jpeg'/></td>;
</tr>\n";
}
?>
</tbody>
<div align="left" style="background:#333333">
<h1><input type="button" value="Log Out" onClick="window.location.href='logout_user.php'" /> </h1>
</div>
</table>
</form>
</body>
</html>
problem is in specially this section
$result= mysql_query("SELECT * FROM `user_information` WHERE `user_id` = '".$_SESSION['id']."' ")or die(mysql_error());
here is my login check code:
<?php
//connect the database
$hostName="localhost";
$dbUsername="root";
$dbPassword="";
$dbName="xyz";
mysql_connect($hostName,$dbUsername,$dbPassword) or die("Connection failed");
mysql_select_db($dbName) or die("Database name doesn't exist");
//start session
#session_start();
if(isset($_POST["user_name"] , $_POST["user_password"]))
{
$User_name = $_POST["user_name"];
// echo "$User_name";
$User_password = $_POST["user_password"];
// echo "$User_password";
$sql = "SELECT * FROM `user_information` WHERE `user_name`='".$User_name."' AND `user_password`='".$User_password."' ";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
// $result = mysql_query("SELECT * FROM `user_info` WHERE `User_name`='".$username."' AND `User_password``='".$password."'");
$my_arary = array();
while($row = mysql_fetch_assoc($result))
{
$_SESSION['id']= $row["Id"];
// echo $_SESSION['login_user_id'];
$_SESSION['user_password']= $row["user_password"];
//echo $_SESSION['login_user_password'];
$_SESSION['user_name']= $row["user_name"];
//echo $_SESSION['login_user_name'];
$my_arary[] = $row;
print_r($row);
if($User_name == $_SESSION['user_name'] && $User_password == $_SESSION['user_password']){
header("Location: userpanel.php");
//echo "successfully logged in";
}
else{
echo "no matches";
header("Location: user_login_basic.php");
}
}
}
?>
You set your $_SESSION['id']= $row["Id"] . Are you sure there column Id in your table? Because you using user_id instead of Id in your query in user admin panel code

Importing MySQL Rows into a table

So I was importing some MySQL Rows into a form but there seems to be an issue, the database doesnot connects, it says no database selected, I have my Database connection parameters in constants.php and they are working fine in the other pages of the website, and there is no syntax error in the commands either, Help me please.Thanks!
<?php
require_once 'classes/Membership.php';
require_once 'includes/constants.php';
$membership = New Membership();
$membership->confirm_Member();
$con = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or
die('Error connecting Database');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<title>Post Scores | Admin Panel D2C</title>
<style type="text/css">
</style>
</head>
<body>
<div class="main_body">
<table width="1029" border="0" cellpadding="0" class="score_table" >
<td width="131">
<form align="center" action="update_team_database.php" method="post">
<tr>
<td> Team: </td>
<td width="831">
<?php
$result = mysql_query("select DISTINCT TeamName from team") or die(mysql_error());
echo '<select name="teamname1"><OPTION>';
echo "Select a team</OPTION>";
while ($row = mysql_fetch_array($result)){
$team1 = $row["TeamName"];
echo "<OPTION value=\"$team1\">$team1</OPTION>";
}
echo '</SELECT>';
?></td>
</tr>
</form>
</table>
</body>
</html>
I would recommend you make use of Object Oriented way of performing queries over MySQL using MySQLi
You may check the example in here
http://in2.php.net/manual/en/mysqli.query.php
Since MySQL is deprecated as of PHP 5.5

Categories