i'm trying to make a web app with php and postgres but i have problems showing the data.
This is my view, it's just a simple login.
<form method="post" action="../Controlador/logueo.php" role="form" class="form-inline">
<div class="form-group">
<input name="nombre" type="text" class="form-control" placeholder="Introduce tu usuario">
</div>
<br>
<div class="form-group">
<br>
<input name="contraseña" type="password" class="form-control"
placeholder="Contraseña">
</div>
<br>
<br>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
This is ../Controlador/logueo.php and here is where i have problems. I can't show the info in $alumno (the variable in foreach), i tried to make an echo and nothing happens...
<?php
include("../Modelo/alumno.php");
$al = new alumno();
$alumnos = $al->obtenerAlumnos();
echo $alumnos;
foreach ($alumnos as $alumno) {
echo $alumno[0];
if ($alumno[0] == $_POST[nombre] && $alumno[5] == $_POST[contraseña] ){
//Do something
}
}
?>
And this is Modelo/alumno.php
<?php
class alumno
{
function _construct()
{
}
function obtenerAlumnos()
{
include("conexion.php");
$query = "SELECT * FROM alumno";
$result = pg_query($squery);
$alumnos = array();
while ($row = pg_fetch_row($result)) {
array_push($alumnos, $row);
}
return $alumnos;
}
}
?>
finally i have Conexion.php
<?php
$db = pg_connect("host=localhost port=5432 dbname=Tarea3BD user=postgres password=motorola13")
or die('Could not connect: ' . pg_last_error());
?>
Hope someone can help me, i'm new with php and postgres.
Many thanks.
Related
How to display SQL result into text input?
When I press the generate button it should appear in the input text.
<?php
$random_name = "";
$conn = mysqli_connect("localhost","root","","wordlist");
if(!$conn)
{
die("Connection Error!");
}
$query_random = "SELECT kata FROM list ORDER BY RAND() LIMIT 1";
$result = mysqli_query($conn,$query_random);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$random_name = $row["kata"];
}
}
mysqli_close($conn);
?>
</head>
<body>
<div class="container" >
<form action="#">
<h4>Word Generator : </h4>
<div class="form-group">
<input type="text" class="form-control">
</div><br>
<button id="generate-btn" type="submit" title="Generate Word">Generate</button>
</div>
</form>
You just need to add this in place of your input, your code is correct but you missed to print the value, that's all
<input type="text" class="form-control" value="<?php echo #$random_name ?>">
I am trying to learn select query with class.
Below is index.php, which have a form to display results and I in PHP I am calling class operations, which is in operations.php
<?php
session_start();
include('operations.php');
$userprofileobj = new operations();
$userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $userprofileobj->fetch_assoc();
?>
<form class="form-horizontal" method="post" action="#" name="number_validate" id="number_validate" >
<div class="control-group">
<label class="control-label">Admin Name</label>
<div class="controls">
<input type="text" name="min" id="min" value="<?php echo $row['user_name']; ?>" required />
</div>
</div>
<div class="control-group">
<label class="control-label">User Email</label>
<div class="controls">
<input type="text" name="max" id="max" value="<?php echo $row['user_email']; ?>" required />
</div>
<div class="form-actions">
<input type="submit" value="Update" class="btn btn-success">
</div>
</form>
operations.php is:
class operations{
public $user_email;
public $error;
public $con;
public function __construct()
{
$this->con = new mysqli("localhost","root","","admin_with_oops");
}
public function showuserprofile($user_email)
{
$result = $this->con->query("select * from user_table where user_email='$user_email'");
if($result->num_rows > 0)
{
return $result;
}
else
{
$this->error = "User not exist";
}
}
}
Am I on right way? If yes teh what is the problem on above code?
Error is: Fatal error: Call to undefined method operations::fetch_assoc() in F:\xampp\htdocs\admin_with_oops\HTML\index.php on line 16
$userprofileobj is an instance of the operations class. It doesn't have fetch_assoc() method. You should replace
$userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $userprofileobj->fetch_assoc();
with
$row = $userprofileobj->showuserprofile($_SESSION['user_email'])->fetch_assoc();
Or you could assign the result to a variable and then call the fetch_assoc() function on it:
$result = $userprofileobj->showuserprofile($_SESSION['user_email']);
$row = $result->fetch_assoc();
I want to create a simple script for login in php using PDO and a PostgreSql database. I have created the database in Postgre and these scripts. The first script is called cbconnection.php:
<?php session_start(); $conn=pg_connect( "dbname=gestionelavori user=gestionelavori password=gestionelavori port=5432 "); $stat=pg_connection_status($conn); if ($stat===P GSQL_CONNECTION_OK) { echo 'Connection status ok'; } else { echo
'Connection status bad'; } include_once 'utente.php'; $utente=new Utente($conn); ?>
The other script is utente.php:
<!DOCTYPE HTML>
<html>
<title>Sistema di Gestione Rapporti Lavoro
</title>
<head>
</head>
<body>
<form method="post">
<div class="form-group">
<input type="text" class="form-control" name="nome_utente" placeholder="Username or E mail ID" required />
</div>
<div class="form-group">
<input type="password" class="form-control" name="password" placeholder="Your Password" required />
</div>
<div class="clearfix"></div>
<hr />
<div class="form-group">
<button type="submit" name="btn-login" class="btn btn-block btn-primary">
<i class="glyphicon glyphicon-log-in"></i> SIGN IN
</button>
</div>
</form>
<?php include_once 'dbconnection.php'; class Utente{ private $db; function __construct($conn) { $this->db = $conn; } public function login($nome_utente,$password) { try { $stmt = $this->db->prepare("SELECT * FROM utente WHERE nome_utente=nome_utente "); $stmt->execute(array('nome_utente'=>$nome_utente)); $utenteRow=$stmt->fetch(PDO::FETCH_ASSOC); if($stmt->rowCount()
> 0) { if($password, $utenteRow['password']) { $_SESSION['utente_session'] = $utenteRow['id_utente']; return true; } else { return false; } } } catch(PDOException $e) { echo $e->getMessage(); } } public function is_loggedin() { if(isset($_SESSION['utente_session']))
{ return true; } } } ?>
</body>
</html>
The problem is that I can't see anything in my page. Is there any error I have made or something I should fix? I'm new at PDO. Thanks!
I tried to make a code which will add an entry to my MySQL table (called "rechnungen") via php. So I made some inputs in html and finaly I tried to insert the informations into my table (using the INSERT INTO... command). So this is what i made:
<?php
Session_Start();
$username=$_SESSION['username'];
$password=$_SESSION['password'];
$dbname=$_SESSION['dbname'];
$servername=$_SESSION['hostname'];
/*conn dev*/
$conn = mysql_connect($servername, $username, $password);
if($conn === false){
header("Location: LogIn.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title></title>
</head>
<body>
<main>
<form method="POST" action="">
<div class="form_neueRechnung">
<!-- part 1 -->
<input type="text" name="suche_Vname_Patienten" placeholder="Vorname" required="">
<input type="text" name="suche_Nname_Patienten" placeholder="Nachname" required="">
<input type="number" id="id_Patient" name="id_patient" placeholder="Pat. Nr." Value="
<?php echo $KID_output; ?>" required="">
</td>
<input type="radio" name="Behandlung" value="Osteopathie" onclick="andere()" required="">
<input type="radio" name="Behandlung" value="Krankengymnastik" onclick="andere()" required="">
<input type="radio" name="Behandlung" id="andere_Behandlung" value="andere" onclick="andere()" required="">
<input type="text" name="andereBehandlung_text" id="andereBehandlung_text" placeholder="andere" style="visibility:hidden">
<!-- part 2 -->
<input type="radio" name="rezept_rechnung" id="mit_rezept" value="mit_Rezept" onclick="rezept()" required="">
<input type="radio" name="rezept_rechnung" id="ohne_rezept" value="ohne_Rezept" onclick="rezept()" required="">
<input type="text" id="ohne_rezept_text" name="ohne_rezept_text" placeholder="freier Text">
<!-- part 3 -->
<input type="time" name="termin1_von" required="">
<input type="time" name="termin1_bis" required="">
<input type="date" name="termin1_date" required="">
<!-- submit -->
<input type="submit" class="submit" value="Rechnug erstellen" name="submit" id="submit">
</div>
<div class="form_fieldset" id="rezept_einstellungen" style="visibility:hidden">
<input type="date" id="rezept_datum" name="rezept_datum">
<input type="text" id="rezept_verordnung" name="rezept_verordnung">
<input type="text" id="rezept_diagnose" name="rezept_diagnose">
</div>
</form>
<script type="text/javascript">
function andere() {
if (document.getElementById('andere_Behandlung').checked) {
document.getElementById('andere_BehandlungArt').style.visibility = 'visible';
} else {
document.getElementById('andere_BehandlungArt').style.visibility = 'hidden';
}
}
function rezept() {
if (document.getElementById('mit_rezept').checked) {
document.getElementById('rezept_einstellungen').style.visibility = 'visible';
} else {
document.getElementById('rezept_einstellungen').style.visibility = 'hidden';
}
if (document.getElementById('ohne_rezept').checked) {
document.getElementById('ohne_rezept_text').style.visibility = 'visible';
} else {
document.getElementById('ohne_rezept_text').style.visibility = 'hidden';
}
}
</script>
<?php
mysql_connect("$servername","$username","$password") or die("connection failed!");
mysql_select_db($dbname) or die ("no database found");
$query = mysql_query("SELECT * FROM `rechnungen`");
while($row = mysql_fetch_array($query)){
$RID = $row['RechnungsID'];
}
$RechnungsID = max($RID ,$RID)+1;
echo $RechnungsID;
$mit_ohne_Rezept = "";
if(isset($_POST['submit'])) {
if($_POST['rezept_rechnung'] == "mit_Rezept") {
$mit_ohne_Rezept = "1";
}
else {
$mit_ohne_Rezept = "0";
}
}
if(isset($_POST['submit'])){
$KundenID=$_POST['id_patient'];
$Behandlung=$_POST['Behandlung'];
$Rezept_datum=$_POST['rezept_datum'];
$Rezept_Verordnung=$_POST['rezept_verordnung'];
$Rezept_Diagnose=$_POST['rezept_diagnose'];
$ohneRezept_text=$_POST['ohne_rezept_text'];
mysql_select_db($dbname,$conn);
$result = "INSERT INTO rechnungen (`RechnungsID`, `KundenID`, `Behandlung`, `mit_ohne_Rezept`, `Rezept_datum`, `Rezept_Verordnung`, `Rezept_Diagnose`, `ohneRezept_text`)
VALUES ('$RechnungsID','$KundenID','$Behandlung','$mit_ohne_Rezept','$Rezept_datum','$Rezept_Verordnung','$Rezept_Diagnose','$ohneRezept_text)";
if (mysql_query($result)) {
echo ("finished!");
} else {
echo "error". mysql_error();
}
}
mysql_close($conn);
?>
</main>
</body>
</html>
I know it's a pretty long code, but i don't know where the problem could be. I'm getting this error:
errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''sdfas)' at line 2
please help me. I'm despairing.
','$ohneRezept_text)";
looks like the issue is here.
missing a quotation mark?
that's what the error is saying
also you don't need to wrap variables in quotations, well you can but its still a pain. if your input contains quotation marks it skips right out. Use addslashes()
In following code, how can I add a button or a link that sorts the data by 'name'?
The following code is just an example, a modified version of code that I found from http://fwebde.com/php/sqlite-php/ . The structure of the code is so enjoyable that I would like to improve it with a sorting-element.
Thanks!
<?php
$db = new PDO('sqlite:db.db');
if (isset($_POST['name']) && isset($_POST['message'])) {
try {
$stmt = $db->prepare("INSERT INTO messages (name, message) VALUES (:name, :message);");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':message', $message);
$title = $_POST['name'];
$content = $_POST['message'];
$stmt->execute();
} catch (Exception $e) {
die ($e);
}
}try {
$posts = $db->prepare('SELECT * FROM messages;');
$posts->execute();
} catch (Exception $e) {
die ($e);
}
?>
<?php while ($post = $messages->fetchObject()): ?>
<?php echo $post->name ?>
<?php echo $post->message ?>
<?php endwhile; ?>
<form action="" method="post">
<label for="name">Name:</label>
<input type="text" name="name" />
<textarea name="message" rows="8" cols="50"></textarea>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
$db = new PDO('sqlite:db.db');
if (isset($_POST['submit']) && isset($_POST['name']) && isset($_POST['message'])) {
try {
//Insert data
} catch (Exception $e) {
die ($e);
}
}
try {
$order = "";
if(isset($_POST['sort'])){
$order = " ORDER BY `name`"; // Order list by name
$orderFlag = 1;
if(isset($_POST['order']) && $_POST['order'] == 1){
$order .= " DESC";
$orderFlag = 0;
}
}
$posts = $db->prepare("SELECT * FROM `messages`" . $order . ";");
$posts->execute();
} catch (Exception $e) {
die ($e);
}
?>
<?php while ($post = $posts ->fetchObject()): ?>
<?php echo $post->name ?><?php echo $post->message ?><br/>
<?php endwhile; ?>
<form action="" method="post">
<!--pushing this button sort by name -->
<input type="submit" name="sort" value="Sort by name" />
<input type="hidden" name="order" value="<?=$orderFlag?>" />
<label for="name">Name:</label>
<input type="text" name="name" />
<textarea name="message" rows="8" cols="50"></textarea>
<!--pushing this button insert new data -->
<input type="submit" name="submit" value="Submit" />
</form>