I have an authorization page, it works everything is ok, but when I log in I want to see additional data from the database for this user.
code
knocks out only one user and everything, when I exit the session and switch on as a new user, nothing is knocked out .... connection to the database works
session_start();
require ('vendor/connect.php');
$FIRSTNAME=$_SESSION['FIRSTNAME'];
$sql = "SELECT BIRTHDAY from users WHERE FIRSTNAME='$FIRSTNAME'";
$result = ibase_query($db, $sql);
if (ibase_fetch_row($result) > 0) {
while($row = ibase_fetch_assoc($result)) {
echo "You BIRTHDAY: " . $row["BIRTHDAY"]. " ";
}
} else {
echo "0 results";
}
Your code should be like below :
$sql = "SELECT BIRTHDAY from users WHERE FIRSTNAME='$FIRSTNAME'";
$result = ibase_query($db, $sql);
$row = ibase_fetch_row($result);
while ($row) {
echo $row[0] . "\t";
}
Related
I have this php code where it checks if the client_number or client_email present in the database. If present it'll disable the submit button. But when one of the field data is not present in database it enables the button which it shouldn't.
function checkDataExistence($connection){
if(!empty($_POST["clientEmailID"])) {
$query = "SELECT * FROM clients_table WHERE client_email='" . $_POST["clientEmailID"] . "'";
$result = mysqli_query($connection, $query);
$count = mysqli_num_rows($result);
if($count>0) {
echo "<span style='color:red'> This Email is already registered!.</span>";
echo "<script>$('#submit').prop('disabled',true);</script>";
} else{
echo "<script>$('#submit').prop('disabled',false);</script>";
}
}
if(!empty($_POST["clientPhoneNumber"])) {
$query = "SELECT * FROM clients_table WHERE client_phone_number='" . $_POST["clientPhoneNumber"] . "'";
$result = mysqli_query($connection, $query);
$count = mysqli_num_rows($result);
if($count>0) {
echo "<span style='color:red'> This Phone number is already registered.</span>";
echo "<script>$('#submit').prop('disabled',true);</script>";
}else{
echo "<script>$('#submit').prop('disabled',false);</script>";
}
}
}
How to make it work for both input fields?
I'm trying to query MySQL DB and print a single result on the page using PHP.
It's always going to be a single result, so I'm not sure if I need to loop
In any case, would anyone mind advising why the below doesn't work?
Thank you!!
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else {
echo "success";
}
$sql = "SELECT sum(Discounted_Value) as id FROM Orders WHERE Year = 2017";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Total 2017 " $row["id"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
If there will be only one result you can simplify code to:
$sql = "SELECT sum(Discounted_Value) as id FROM Orders WHERE Year = 2017";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo "Total 2017 " . $row["id"] . "<br>"; // Don't forget dots for concatenation
Hi I am trying to do a Registration that the users will put their name password and their answers to some questions and then an admin will manually answer to it if it's accepted.I did the system that loads their name password and answers in the database,and I also ran the things that will show the answers to the admin,but I can't figure a way to change a value just for one user not for all of them,I will leave you my codes and everything over here.
Here is my admin.viewapplications.php code
(Here,it shows everything fine,but I can't figure a way that the button to act just for one id not for all)
<?php
//include(__DIR__ . "/signup.php");
include("../resources/config.php");
//$name = $_POST['Name'];
//$mg = $_POST['MG'];
//$pg = $_POST['PG'];
//$rk = $_POST['RK'];
$sql = "SELECT id, name, tutorial, MG, PG, RK FROM rp_users WHERE tutorial = 2";
//$tutorial = "SELECT tutorial FROM rp_users";
$result = mysql_query($sql);
//$result2 = mysql_query($tutorial);
//$value = mysql_fetch_object($result2)
/*if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}*/
//if($value > 1)
//
while($row = mysql_fetch_array($result))
{
//$tutorial = row["tutorial"];
//f($tutorial == 2)
//}
$id = $row["id"];
$name = $row["name"];
$mg = $row["MG"];
$pg = $row["PG"];
$rk = $row["RK"];
echo "ID: " . $id."<br> <br>";
echo "Nume: " . $name."<br> <br>";
echo "MG: " . $mg."<br> <br>";
echo "PG: " . $pg."<br> <br>";
echo "RK: " . $rk."<br> <br>";
echo '<form action="./?p=applicationaccept" method="POST">';
echo '<input type="submit" name="accept" value="Accepta">';
echo '</form><br>';
echo '<form action="./?p=applicationdeny" method="POST">';
echo '<input type="submit" name="deny" value="Respinge">';
echo '</form><br> <br> <br>';
}
//}
//
?>
And here is my applicationaccept.php
<?php
include("../admin/admin.viewapplications.php");
include("../resources/config.php");
$iduser = $id;
$sql = "UPDATE rp_users SET tutorial=0";
$result = mysql_query($sql);
if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}
/*while($row = mysql_fetch_array($result))
{
}*/
?>
I think what you want to do is a simple UPDATE to your MySQL database..
but make sure you format the PHP code you're using otherwise it'll give you an ERROR!
Also you have to use 'mysqli' now in PHP!
<?php
$someID = '1';
$sql = "UPDATE `rp_users` SET `tutorial`= '0' WHERE `id` = $someID";
$result = mysqli_query($link, $sql);
if($result)
{
echo "Success";
}
else
{
echo ("Error");
}
?>
BTW I forgot to mntion the '$link' is the connection to your database!
As of my understanding of your question if your form action is applicationaccept.php and you are trying to update for one user in applicationaccept.php file, try this:
<?php
include("../admin/admin.viewapplications.php");
include("../resources/config.php");
$iduser = $_POST["id"]; // pass id as parameter in form
$sql = "UPDATE rp_users SET tutorial=0";// change this line to following line
$sql = "UPDATE rp_users SET tutorial=0 where id=$iduser";
$result = mysql_query($sql);
if($result)
{
echo "Succes";
}
else
{
die(mysql_error());
}
?>
Be aware your code is vulnerable
I have this part of code in php . when player press button in client (using ajax) I want my database show next record. but I won't.
if(isset($_POST['req'])){
$counter++;
$sql = "SELECT question FROM mytable WHERE id = $counter";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["id"]." ". $row["question"]. " " . "<br>";
}
} else {
echo "0 results";
}
}
I would suggest storing your counter in a session. Then each time a player does this action you can give them the next row like so :-
session_start();
if(isset($_POST['req'])){
if ( ! isset($_SESSION['counter']) ) {
$_SESSION['counter'] = 1;
} else {
$_SESSION['counter'] = $_SESSION['counter'] + 1;
}
$sql = "SELECT question FROM mytable WHERE id = {$_SESSION['counter']}";
$result = $conn->query($sql);
if ( ! $result ) {
// log error to error log
error_log(print_r($conn->errorinfo(),true), 3, 'app_error.log');
echo "Temporary database issues, please try again later";
header('Location: error_page.php');
exit;
}
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
echo $row["id"]." ". $row["question"]. " " . "<br>";
} else {
echo "0 results";
}
}
The easy way is to send the current id along in the Ajax request. Increment it then use it to pull the next question from your database
I am looking for a way to display an error message if there is nothing listed in the table.
I have a photos table.
If this tables is empty, id like to echo something.
else, show the pictures.
inside of that table I have
id, name, url
id = id
name = name of image
url = url of image.
If there are no rows, we have an error.
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
mysql_fetch_array($query1);
if(empty($query1)) {
echo "nothing";
} else {
echo "good";
}
Try this,
$query = "SELECT * FROM photos";
$result= mysql_query($query);
$length= mysql_num_rows($result);
if($length>0)
{
while($rows = mysql_fetch_array($result))
{
echo $rows['name'];
echo "<img src='$rows[url]' />";
}
}
else
{
echo "Nothing to display";
}
Hope this will work
What about something like...
$sql = "SELECT COUNT(*) AS amountPhotos FROM photos";
$result = mysql_query($sql);
$row = mysql_fetch_assoc($result);
if ($row["amountPhotos"] == 0) {
echo "There are no photos in the photo table.";
}
or
$sql = "SELECT * FROM photos LIMIT 1";
$result = mysql_query($sql);
if (mysql_num_rows($result) == 0) {
echo "There are no photos in the photo table.";
}
Try this
$query1 = mysql_query("SELECT COUNT(*) FROM photos;");
$result = mysql_fetch_array($query1);
if(empty($result)) {
echo "nothing";
} else {
echo "good";
}
This pretty much sums up the answer for this question: http://www.w3schools.com/php/php_mysql_select.asp
They even provided a sample code:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) { //<--- here they check if number of rows returned is greater than 0 (so there is data to display)
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results"; //<----- nothing found
}
$conn->close();
?>
Just modify this and you'll be good to go.