No data displayed by given a specific name and date - php

This is my table
and now I want to retrieve the data from MySQL to android based on name (Test) and month (01), but no data get displayed. Is there any mistake in my php ?
My php code
<?php
define('HOST','127.0.0.1:3307');
define('USER','root');
define('PASS','');
define('DB','androiddb');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('unable to connect');
$name = $_GET['name'];
$month = $_GET['month'];
$sql = "select * from information WHERE name= '". $name."' and month = '".$month."'";
$res = mysqli_query($con,$sql);
$result=array();
while($row=mysqli_fetch_array($res)){
array_push($result,array('id'=>$row[0],'name'=>$row[1],'weather'=>$row[2],'date'=>$row[3],'status'=>$row[4],
'time_in'=>$row[5], 'time_out'=>$row[6]));
}
echo (json_encode(array("result"=>$result)));
mysqli_close($con);
?>

You have a condition month = ... in your SQL statement but the table doesn't have a month column.
This should work:
$sql = "select * from information WHERE name= '". $name."' and MONTH(date) = '".$month."'";

Related

how to check and insert data if specific row data is not already inserted on same date

I want to insert specific row data into my database table but first, we have to check if row data exists in the database table. I am inserting userid, companyid and current date into my table if the record does not exists. This is the list:
My code to fetch data:
<?php
$con=mysqli_connect("localhost" , "root" , "" , "accounts");
if (!$con) {
echo "connection failed";
}
$query= "
SELECT *
FROM company
WHERE catagory = 'A'
";
/*"SELECT * FROM `company` WHERE catagory='A'";*/
$result=mysqli_query($con,$query);
while ($row=mysqli_fetch_array($result)) {
?>
<tbody>
<tr>
<td><a href='/bw/gymdetail.php?id=<?=$row['id'];?>'><?=$row['id'];?></a></td>
<td><a href='/bw/gymdetail.php?id=<?=$row['id'];?>'><?=$row['first_name']?></a></td>
<td><?=$row['u_city'];?></td>
<td><?=$row['u_address'];?></td>
<td><a href='/bw/login-system/checkin.php?id=<?=$row['id'];?>&uid=<?=$_REQUEST['id'];?>'> Check In</a></td>
</tr>
</tbody>
<?php
}
?>
checkin.php page
<?php
require 'db.php';
if (isset($_GET['uid'])) {
$id = $_GET['id'];
$uid = $_GET['uid'];
$now=new datetime();
$date = $now->format('m-d-y');
$sql = $mysqli->query("
SELECT *
FROM checkins
WHERE userid = '$id'
AND `dateis` = '$date'
");
$productcount = $sql->num_rows;
if($productcount == '1'){
$sql = "
INSERT INTO checkins (userid, companyid, dateis)
VALUES ('$uid','$id','$date')
";
$query = mysqli_query($mysqli,$sql);
}
else{
echo "already clicked";
}
}
?>
If a user clicks on checkin button.
I need to check if that row is already in table with same user and same date.
If not exists insert data else show message that record already exists.
check this out
https://chartio.com/resources/tutorials/how-to-insert-if-row-does-not-exist-upsert-in-mysql/#using-insert-ignore
<?php
require 'db.php';
if (isset($_GET['uid'])) {
$id = $_GET['id'];
$uid = $_GET['uid'];
$now=new datetime();
$date = $now->format('m-d-y');
$sql = "INSERT IGNORE INTO checkins (userid, companyid, dateis) "
. "VALUES ('$uid','$id','$date')";
$query = mysqli_query($mysqli,$sql);
if(!mysqli_affected_rows($mysqli)){
echo "already clicked";
}
}
?>

trying to UPDATE SQL after one SQL

I am calculating AGE by DATE from DOB field, then I want to push it into AGE with correct age based on DOB . So As I debug The DOB calculating to AGE is works, but it cannot update AGE the code:
<?php
$servername = "localhost";
$username = "usernameexmaple";
$password = "passworking";
$dbname = "dbnameworking";
// Create connection
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id as ID, YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5)) as age
FROM regio_users";
$sql2 = ("UPDATE regio_users SET age = '$newage' WHERE id ='$newid' ");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$newage = $row['age'];
$newid = $row['ID'];
$sql2 = ("UPDATE regio_users SET age = '$newage' WHERE id ='$newid' ");
$result2 = $conn->query($sql);
if ($result2){
echo "done"."<br>";
}
}
}
else {
echo "0 results";
}
$conn->close();
?>
It echos DONE for every ID but not updating anything at all.
You have used $result2 = $conn->query($sql); which is incorrect. You have to use $result2 = $conn->query($sql2); as $sql2 is the new query you formed.
This can be done with a single line SQL, rather than using PHP to loop through all the rows to only update the age:
UPDATE `regio_users` SET `age` = YEAR(CURRENT_TIMESTAMP) - YEAR(`dob`) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(`dob`, 5));
As saty pointed, use correct variable name.
Check if autocommit is on. If not, make sure you commit the data. Check for its syntax in PHP.

Clear SQL table before importing data (php)

Hi guys as the title says i'm trying to clear My SQL table before importing new data , but the problem only the last two rows appears not all of them .
I tried this using "TRUNCATE"
// clear table
$sqli = "TRUNCATE TABLE MyTable";
mysql_query($sqli);
$sql = 'INSERT INTO MyTable VALUES("'.$Data1.'","'.$Data2.'") ';
mysql_query($sql);
mysql_close();
$sql = 'SELECT * FROM MyTable';
$req = mysql_query($sql);
while($data = mysql_fetch_array($req)){
$D1 = $data['data1'];
$D2 = $data['data2'];
echo "$D1<br />
$D2<br />";
}
i can see only the last two rows.
this is full code
part 1 : adding data into MySQL table
<?php
include( "............" );
$table = mta::getInput();
$Kills = $table[0];
$Deaths = $table[1];
// Send infos in MySQL
$base = mysql_connect ('........', '.........', '........');
mysql_select_db ('.........', $base);
$sql = 'INSERT INTO AccountsData VALUES("'.$Kills.'","'.$Deaths.'") ';
mysql_query($sql);
mysql_close();
// Return true if is added
// an other code here
?>
part 2 : get data from Mysql Table
<?php
$base = mysql_connect ('.........', '.........', '.........');
mysql_select_db ('..........', $base);
$sql = 'SELECT * FROM AccountsData';
$req = mysql_query($sql);
while($data = mysql_fetch_array($req)){
$Kills = $data['Kills'];
$Deaths = $data['Deaths'];
echo "$Kills<br />
$Deaths<br />";
}
mysql_close();
?>
before using "TRUNCATE TABLE" it's working fine and i can see all rows
like this:
example
player 1
player 2
player 3
player 4
after using "TRUNCATE TABLE" i can see only the last row
player 4.
All what i need is i want to clear the table before adding new rows.
Sorry it's the first time that i use https://stackoverflow.com/

Select a string from mysql table in php doesn't show output

I'm trying to extract a string field, under a column called "whenadded", of a mysql table (called "values") with a php script. I'm using this code:
<?php
include("common.php");
$link=dbConnect();
$name = safe($_POST['name']);
$mysqldate = mysql_query("SELECT `whenadded` FROM `values` WHERE `name`= `$name`");
?>
In this table I have only two columns: name and whenadded. Both are strings.
If I try to display the result of $mysqldate, I don't see anything (white field).
[SOLVED] At the end I solved using this:
include("common.php");
$link=dbConnect();
$name = safe($_POST['name']);
$query = mysql_query("SELECT * FROM $dbName . `valorigps` WHERE name = '$name'");
while($row = mysql_fetch_array($query))
{
echo $row['whenadded'];
}
your query string shoud be this:
$mysqldate = mysql_query("SELECT whenadded FROM values WHERE name= '$name'");
or
$mysqldate = mysql_query("SELECT whenadded FROM values WHERE name= '{$name}'");
no need of quote for table columns!

Field not being inserted into mysql database from php

I have a little problem where a field is not being inserted into my users table. I have two tables as follows:
users - id, gamerid, email, password, country, country_code
countries - country_id, country, country_code
Now i have a signup form that comprises of gamerid,email,password and a country select (pulls from the countries table using php)
My problem is that when i submit the form, i want to run a query to pull the country code from the table which matches what was selected by the user and insert these fields into the users table. All my data is inserting correctly except for the country_code.
Here is my code for the html select section:
<select name = "country_create" style = "height: 25px; width: 180px;">
<option value="0" selected="selected" class = "signup_form_country_select_class">Select your country</option>
<?php
include "config.php";
$connection = mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($dbname, $connection) or die(mysql_error());
$result = mysql_query('SELECT country FROM countries');
while($row = mysql_fetch_array($result))
{
echo '<option value="'.$row['country'].'">'.$row['country'].'</option>';
}
?>
</select>
And here is the php from the register script:
$connection = mysql_connect($host, $username, $password) or die(mysql_error());
mysql_select_db($dbname, $connection) or die(mysql_error());
// INPUT CLEANING FUNCTION
function clean($str)
{
$cleaned = mysql_real_escape_string(strip_tags($str));
return $cleaned;
}
$gamerid = clean($_POST['gamerid_create']);
$email = clean($_POST['email_create']);
$password = clean($_POST['password_create']);
$country = ($_POST['country_create']);
$cc_qry = "SELECT country_code FROM countries WHERE country = '$country'";
$country_code = mysql_query($cc_qry);
$insert = "insert into users(gamerid,email,password,country,country_code) values('$gamerid','$email','$password','$country','$country_code')";
mysql_query($insert, $connection);
Thanks in advance guys!
First - use PDO or mysqli functions, but secondly - you must also fetch data from query result:
$res = mysql_query($cc_qry);
$res_cc = mysql_fetch_assoc($res);
$country_code = $res_cc['country_code'];

Categories