I'm creating a simple PHP to do list. I want to turn the date red if it's not the current date. Here's part of my code, with the HTML removed:
<?php include "connection.php";?>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$task = isset($_POST['task']) ? $_POST['task'] : null;
$importance = isset($_POST['importance']) ? $_POST['importance'] : null;
$due_date = isset($_POST['due_date']) ? $_POST['due_date'] : null;
$order = isset($_POST['order']) ? $_POST['order'] : null;
if(isset($task,$importance,$due_date)){
$sql = "INSERT INTO tasks (task, importance, due_date) VALUES ('$task', '$importance', '$due_date')";
$result = mysqli_query($connection, $sql);
if(!$result){
die("Database query failed.");
}
}
}
if(isset($order)){
$sql = "SELECT * FROM tasks ORDER BY {$order}";
} else {
$sql = "SELECT * FROM tasks";
}
$result = mysqli_query($connection, $sql);
if(!$result){
die("Database query failed.");
}
$current_date = date("Y-m-d");
if(isset($due_date)!=$current_date){
$due_date_class="overdue";
} else {
$due_date_class="not_overdue";
}
?>
<table>
<?php
while($column = mysqli_fetch_assoc($result)){
?>
<tr><td><?php echo $column["task"]?></td><td class="<?php echo $column["importance"] ?>"><?php echo $column["importance"]?></td><td class="<?php echo $due_date_class ?>"><?php echo $column["due_date"]?></td><td><?php echo "<a href='delete_one.php?id=".$column['id']."'>Delete</a>" ?></td></tr>
<?php
}
?>
</table>
<?php include "footer.php";?>
This is the part that isn't working. It's either turning all the rows red or black, depending on what date I enter:
$current_date = date("Y-m-d");
if(isset($due_date)!=$current_date){
$due_date_class="overdue";
} else {
$due_date_class="not_overdue";
}
isset($due_date)
should be changed to
$due_date
Because, isset will return value 1 or 0, which will be compared with date, which is going to be false all the time.
Instead, you could do following:
if(isset($due_date))
{
$current_date = date("Y-m-d");
if($due_date!=$current_date){
$due_date_class="overdue";
} else {
$due_date_class="not_overdue";
}
}
else
{
echo "Not a valid Due Date";
}
>>>Demo<<<
Regarding variable scoping.
You need to declare $due_date_class at very top, to increase its scope, otherwise you will get error. Since, scope of this variable will be limited to if and else.
Related
I'm creating a simple PHP and SQL to do list application. My problem is, every time I edit a row, it creates another row instead of just editing the existing row. Here is some of the code from index.php:
<?php
$current_date = date("Y-m-d");
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$task = isset($_POST['task']) ? $_POST['task'] : null;
$importance = isset($_POST['importance']) ? $_POST['importance'] : null;
$due_date = isset($_POST['due_date']) ? $_POST['due_date'] : null;
$order = isset($_POST['order']) ? $_POST['order'] : null;
if(isset($task,$importance,$due_date,$user_name)){
$sql = "INSERT INTO tasks (task, importance, due_date, user_name) VALUES ('$task', '$importance', '$due_date', '$user_name')";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
}
}
?>
<?php
while($column = mysqli_fetch_assoc($result)){
?>
<tr><td><?php echo $column["task"]?></td><td class="<?php echo $column["importance"] ?>"><?php echo $column["importance"]?></td><?php if($column["due_date"]<$current_date){echo '<td class="overdue">';}else{echo '<td class="not_overdue">';} ?><?php echo $column["due_date"]?></td><td><?php echo "Delete" ?></td></tr>
<?php
}
?>
Here is some of the code from edit.php:
<?php
if($_GET['id'] != ""){
$task_id = $_GET['id'];
$task_id = base64_decode($task_id);
$sql = "SELECT * FROM tasks WHERE task_id='$task_id'";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
}
?>
<?php
}
?>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$task = isset($_POST['task']) ? $_POST['task'] : null;
$importance = isset($_POST['importance']) ? $_POST['importance'] : null;
$due_date = isset($_POST['due_date']) ? $_POST['due_date'] : null;
if(isset($task,$importance,$due_date)){
$sql = "UPDATE tasks SET task='$task', importance='$importance', due_date='$due_date' WHERE task_id='$task_id'";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
}
}
?>
Can someone tell me what I'm doing wrong?
On your edit.php file the Form tag should have an Action pointing to edit.php to update.
I fixed this issue by removing the form action on edit.php. Original:
<form method="post" action="index.php">
New:
<form method="post">
I then added a redirect to the bottom of this code block on edit.php:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$task = isset($_POST['task']) ? $_POST['task'] : null;
$importance = isset($_POST['importance']) ? $_POST['importance'] : null;
$due_date = isset($_POST['due_date']) ? $_POST['due_date'] : null;
if(isset($task,$importance,$due_date)){
$sql = "UPDATE tasks SET task='$task', importance='$importance', due_date='$due_date' WHERE task_id='$task_id'";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
header('Location: index.php');
}
}
?>
I suppose that on passing the task_id to edit.php you have a form for editing values and in that form, the action part should be updated to edit.php.
I have a very simple use case where I am checking if a certain value is present in the table and it always seems to fail.This is my php code.
<?php
include "config.php";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);
if(!$con)
{
echo "Connection Error".mysqli_connect_error();
}
else{
//echo "";
}
$device_id = $_POST["device_id"];
$check = "SELECT magazine_id FROM registered_buyers WHERE device_id = $device_id";
$rs = mysqli_query($con,$check);
if(mysqli_num_rows($con,$rs) == 0)
{
$jsonarray = $_POST["jsonarray"];
echo "This will be inserted".$jsonarray;
}else
{
echo "User already registered";
}
?>
Can anyone please point out my mistake.Any help or suggestion is welcome.Thank you.
You can try to follow this code.
<?php
include "config.php";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);
if(!$con){
echo "Connection Error".mysqli_connect_error();
}
$device_id = $_POST["device_id"];
$check = "SELECT magazine_id FROM registered_buyers WHERE device_id = ".$device_id;
$rs = mysqli_query($con, $check);
if(mysqli_num_rows($rs) == 0){
$jsonarray = $_POST["jsonarray"];
echo "This will be inserted".$jsonarray;
}else{
echo "User already registered";
}
?>
since i dont have enough rep to add a comment, i will consider the device_id is string, if so try something like this:
"SELECT magazine_id FROM registered_buyers WHERE device_id = '$device_id'";
add '
I have a column called favid. I am trying to pull and compare the data in that column to an existing value:
<?php $query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid=$favid");
while ($row = mysql_fetch_assoc($query)) {
echo $row['favid']; };?>
I also have an existing value:
$x
But when I do something like this it doesn't work:
<?php if($row['favid'] == $x){?>
Do this...
<?php } else { ?>
Do nothing...
<?php}?>
I realize the data in the column somehow isn't pulled out. What should be done for this to work?
Try this, I assume you already connected to DB.
<?php
$x = 1;
$query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid='$favid'") or die(mysql_error());
if (mysql_num_rows($query) > 0)
{
while ($row = mysql_fetch_assoc($query))
{
if ($row["existing_column_name"] == $x)
{
echo "Yes";
} else
{
echo "No";
}
}
} else
{
echo "Nothing was found";
}
?>
<?php
$x = 100500; // integer for example
$CID = mysql_connect("host","user","pass") or die(mysql_error());
mysql_select_db("db_name");
$query = mysql_query("SELECT * FROM ajaxfavourites WHERE favid='{$favid}'", $CID);
while ($row = mysql_fetch_assoc($query)) {
if (intval($row["some_existing_column_name"])==$x){
print "Is equals!";
} else {
print "Is different!";
}
}
?>
Please be informed that mysql_connect and other functions with the prefix of mysql_ is deprecated and can be removed in the next versions of PHP.
I have the following code to check if a row exists in MySQL:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT 1 FROM files WHERE id='$code' LIMIT 1");
if (mysql_fetch_row($result)) {
echo 'Exists';
} else {
echo 'Does not exist';
}
}
?>
This works fine. But I need to change it a bit. I have the following fields:
id, title, url, type. When someone uses the code above ^ to check if a row exists, I need a variable to get the url from the same row, so I can redirect the user to there.
Do you have any idea how I can do that?
Thanks in advance! :)
Try this:
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT * FROM files WHERE id=" . $code . " LIMIT 1");
if (mysql_num_rows($result) > 0) {
while($rows = mysql_fetch_array($result)) {
echo 'Exists';
$url = $rows['url'];
}
} else {
echo 'Does not exist';
}
}
?>
It is quite simple. I think you don't show any effort to find the solution by yourself.
<?php
if (!empty($_POST)) {
$code = $_POST['code'];
mysql_connect("$dbhost","$dbuser","$dbpass");
mysql_select_db("$dbname");
$result = mysql_query("SELECT url FROM files WHERE id='$code' LIMIT 1");
if ($result) {
$url = mysql_fetch_row($resultado);
} else {
echo 'Does not exist';
}
}
<?php
$sql_query = "SELECT * FROM test WHERE userid ='$userid'";
$result1 =mysql_query($sql_query);
if(mysql_num_rows($result1)>0){
while($post = mysql_fetch_array($result1))
{
$url = $post['url'];
}
}
?>
If mysql_num_rows($result1)>0 it means row is existed fir the given user id
In my example below the for loop is not executed and / or my data is not being inserted into the database. What can I change?
<?php
include('connection.php');
{
if(isset($_POST['Submit']))
{
date_default_timezone_set('Asia/Calcutta');
$date = date('Y-m-d H:i:s', time());
for ($i=1; $i<=$_POST["NUM_STUDENTS"]; $i++) {
$STD = "STUDENT_ID".$i;
$DS = "DISCOUNT".$i;
$LV = "LEAVE".$i;
$FN = "FINE".$i;
$sql = "INSERT INTO ATTENDANCE";
$sql .= "(SESSION_ID,ORG_ID,GRADE_ID,MONTH,STUDENT_ID,DISCOUNT,LEAVE,FINE,SOURCE,CREATEDTTM,UPDDTTM,DELETE_FLAG)";
$sql .= "VALUES ";
$sql .= "('".$_POST["SESSION_ID"]."','".$_POST["ORG_ID"]."','".$_POST["GRADE_ID"]."','".$_POST["MONTH"]."','".$_POST[$STD]."','".$_POST[$DS]."','".$_POST[$LV]."','".$_POST[$FN]."' ";
$sql .= ",'".$_SESSION['login_name']."','".$date."','".$date."','N')";
$objQuery_2 = mysql_query($sql);
if($objQuery_2)
{
echo"<script>alert('Attendance Fine Added Successfully')</script>";
header("refresh:0;url=attendance_srch.php");
exit();
}
else
{
echo"<script>alert('Please Check Data')</script>";
header("refresh:0;url=attendance_srch.php");
exit();
}
}
}
mysql_close($bd);
ob_flush();
}
?>
You have left a lot space here between ? and > must be ?> . [This is one of the errors]
<?=$objResult["OPERATOR_ID"];? >">
^^^
must be
<?=$objResult["OPERATOR_ID"];?>">
Array keys are case sensitive. If the actual names of the input is OPERATOR_ID, then you can't access it with $_GET['operator_id'], you have to use $_GET['OPERATOR_ID'].
Another problem is that you have an extra set of braces. So you're doing all the database code even if the if (isset($_GET['OPERATOR_ID']) is false.