Trying to solve this Notice: Undefined variable - php

Im trying to update my table news dynamically.
And its working fine, but I want to show in my form the current values ​​that my news has, so Im using inside my value = <?php echo $read['title'] ; ?>
But with date, Im getting two errors:
Im getting this error inside my input:
Notice: Undefined variable: spanish and Undefined variable: english
I understand erros, but I wanted to ask you, if you know how I can solve this, I know if I repeat definition of my variables "spanish and english" inside my date value, it works, but it not seems correct to repeat this variables.
Do you know how I con solve this using only one definition for $english and $spanish variables??
<?php
if(isset($_POST['sendForm']))
{
$f['title'] = $_POST['title'];
$f['date'] = $_POST['date'];
if(in_array('',$f))
{
echo 'Fill all fields';
}
else
{
$english = array(); //here I have some words
$spanish = array();
$result_date = str_ireplace ($english , $spanish, $f['date']);
$date = DateTime::createFromFormat('l, j F, Y', $result_date);
$date = $date->format('Y-m-d H:i:s');
$update = $pdo->prepare("UPDATE news set title=:title, date=:date WHERE id = :id");
$update->bindValue(':title', $f['title']);
$update->bindValue(':date', $date);
$update->bindValue(':id', '20');
$update->execute();
}
}
?>
<form action="" method="post" enctype="multipart/form-data">
<label class="line">
<span>Title:</span>
<input type="text" name="title" value="<?php echo $read['title'] ; ?>" />
</label>
<label class="line">
<span>Date:</span>
<input type="text" name="date" value="
<?php
$date = new DateTime($read['date']);
$result_date = str_ireplace($spanish , $english, $date->format('l, j F, Y'));
echo $result_date;
?>" />
</label>
<input type="submit" value="Submit" name="sendForm"/>
</form>

<?php
$english = array(); //define at first because if you post the variables are unset
$spanish = array();
if(isset($_POST['sendForm']))
{
$f['title'] = $_POST['title'];
$f['date'] = $_POST['date'];
if(in_array('',$f))
{
echo 'Fill all fields';
}
else
{
$result_date = str_ireplace ($english , $spanish, $f['date']);
$date = DateTime::createFromFormat('l, j F, Y', $result_date);
$date = $date->format('Y-m-d H:i:s');
$update = $pdo->prepare("UPDATE news set title=:title, date=:date WHERE id = :id");
$update->bindValue(':title', $f['title']);
$update->bindValue(':date', $date);
$update->bindValue(':id', '20');
$update->execute();
}
}

Notices you can off through php.ini file or just use the error_reporting(0); but make sure here this will not showing any other error.
you can enable this after the site will goes to live.
may this help you!

Related

Updating and deleting from a data table, warning undefined array key

I have been following a lesson on how to make an admin page. I got all the information out of my database to a table on the page. I have an update button and when I change the information and press the button I receive this error: Warning: undefined array key "WebID" in ..\Update.php on line 3
From my search online everyone is trying to change the code so that if array key does not exist: return null. I tried that and the error does not appear no more, but the table does not change.
Any thoughts?
This is the code:
<?php
require_once("DB/DB.php");
$SearchQueryParameter = $_GET["WebID"];
if (isset($_POST["Update"])) {
$Ename = $_POST["Ename"];
$Eid = $_POST["Eid"];
$Erank = $_POST["Erank"];
$Eemail = $_POST["Eemail"];
$Edate = $_POST["Edate"];
$Epassword = $_POST["Epassword"];
$Specialisms = $_POST["Specialisms"];
global $ConnectingDB;
$sql ="UPDATE emp_data SET Ename='$Ename', Eid='$Eid', Erank='$Erank', Eemail='$Eemail', Edate='$Edate', Epassword='$Epassword',
Specialisms='$Specialisms' WHERE WebID='$SearchQueryParameter'";
$Execute = $ConnectingDB->query($sql);
if ($Execute) {
echo '<script>window.open("adminpage.php?WebID=Recored Updated","_self")</script>';
}
}
?>
<?php
<?php
global $ConnectingDB;
$sql = "SELECT * FROM emp_data WHERE WebID='$SearchQueryParameter'";
$stmt = $ConnectingDB->query($sql);
while ($DataRows = $stmt->fetch()) {
$WebID = $DataRows["WebID"];
$Ename = $DataRows["Ename"];
$Eid = $DataRows["Eid"];
$Erank = $DataRows["Erank"];
$Eemail = $DataRows["Eemail"];
$Edate = $DataRows["Edate"];
$Epassword = $DataRows["Epassword"];
$Specialisms = $DataRows["Specialisms"];
}
?>
Html file used to update:
<form id="UpdateForm" method="post" action="Update.php?WebID<?php echo $SearchQueryParameter; ?>">
<div class="form-group">
<button type="submit" name="Update" class="form-control-submit-button">Update</button>
</div>
you have to write the form action like this.. you missed the = sign
action="Update.php?WebID=<?php echo $SearchQueryParameter; ?>"
<form id="UpdateForm" method="post" action="Update.php?WebID=<?php echo $SearchQueryParameter; ?>">
You missed the = sign, in the url

How to insert result of date difference into mysql database using php?

<?php
function find_days($start_date, $end_date) {
$response = new stdClass();
try {
$sdate = new DateTime($start_date);
$edate = new DateTime($end_date);
$dateInterval = $edate->diff($sdate);
$response->status = true;
$response->result = $dateInterval;
return $response;
} catch (Exception $e) {
$response->status = false;
$response->result = 'Invalid Date Format';
return $response;
}
}
?>
Start Date: <input type="date" name="sdate" placeholder="start date" />
End Date: <input type="date" name="edate" placeholder="end date" />
<input type="submit" value="Find Days" />
<?php
if (isset($_POST['sdate']) && $_POST['sdate']) {
$start_date = $_POST['sdate'];
$end_date = $_POST['edate'];
//now call the function
$days_array = find_days($start_date, $end_date);
if ($days_array->status) {
echo " <input type='text' name='day'
value='.$days_array>result>days.' />";
$day = $_POST['day'];
$query = "INSERT into cart (date,edate,days)
VALUES('$start_date','$end_date','$day')";
$success = $conn->query($query);
if (!$success) {
die("Couldn't enter data: ".$conn->error);
}
} else {
echo $days_array->result;
}
}
My code is working perfectly. But the result is displayed only on the screen.
So I've tried to store the result by placing it in a textbox and then insert into the table in a usual way. But I got an error "Catchable fatal error: Object of class DateInterval could not be converted to string in C:\xampp\htdocs\date.php on line 45"
I don't know how to rectify this.. please help me solve this.
You have to convert it to a string using format:
<?php
$now = new DateTime();
$hour = new DateTime( "now +1hours +13minutes +22seconds" );
$diff = $now->diff( $hour );
echo $now->format('Y-m-d H:i:s');
echo "\n";
echo $hour->format('Y-m-d H:i:s');
echo "\n";
echo $diff->format('%H:%I:%S');
Output
2018-07-26 20:53:42
2018-07-26 22:07:04
01:13:22

session error in php forms

This is my code:-
<html>
<body>
<center>
<h2>working time</h2>
<form action="" method="post">
<button name="starttime" class="starttime">starttime</button>
<?php
session_start();
$name = $_SESSION["name"];
if(isset($_POST["starttime"])){
$date = date('Y-m-d');
$date1 = date('H:i:s');
require "database.php";
$n = "";
$d = "";
$sql = "SELECT name, ddate FROM karthick.date WHERE name = '$name' AND ddate = '$date'";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$n = $row["name"];
$d = $row["ddate"];
}
}
if($n == $name && $d == $date){
echo'<script>
alert("Time already registered");
</script>';
}else{
$sql = "INSERT INTO karthick.date (name, ddate, starttime) VALUES ('$name', '$date', '$date1')";
if($conn->query($sql) === TRUE){
echo "starttime inserted";
}else{
echo "error";
}
}
}
?>
</form>
<form action = "" method="post">
<button name="endtime" class="endtime">Endtime</button>
<?php
session_start();
$name = $_SESSION["name"];
if(isset($_POST["endtime"])){
$date = date('Y-m-d');
$date1 = date('H:i:s');
require "database.php";
$n = "";
$d = "";
$sql = "SELECT name, ddate, endtime FROM karthick.date WHERE name = '$name' AND ddate = '$date'";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$e = $row["endtime"];
}
}
if($e != '00:00:00'){
echo'<script>
alert("Time already registered");
</script>';
}else{
$sql = "UPDATE karthick.date SET endtime='$date1' WHERE name = '$name' AND ddate = '$date'";
if($conn->query($sql) === TRUE){
echo "Endtime inserted.<br>";
}else{
echo "error";
}
$sql1 = "UPDATE karthick.date SET worked = (endtime - starttime) WHERE name = '$name' AND ddate = '$date'";
if($conn->query($sql1) == TRUE){
echo "worked time updated";
}else{
echo "error";
}
}
}
?>
</form>
<form action="user.php" method="post">
<button name= "logout" class="logout">Logout</button>
</form>
</body>
</html>
As I run this code it shows me an interrupting message.
Notice:A session had already been started - ignoring session_start()
But the code works fine. Is there any way to hide the notice message?
Only one time session_start() is needed on a page (on top). You have it two times in your page so remove the second one.
Suggestion:-
Always use some error reporting code.
example:-
error_reporting(E_ALL); // check all type of errors
ini_set('display_errors',1);// display those errors
Always add this to your php script in the beginning. It will help you a lot. Thanks.
remove session_start() from your file.
Because u started session twice.
Either the file u r included in your code in that u started session.
so please go through u r code once again
use session_start(); only once and before sending any output (at the very start of page)
or turn of error reporting(not recommended):
add error_reporting(0); at the beggining of page to hide all errors
and notices
add # character before command that causing error or notice( eg.
#session_start();)
change
<html>
<body>
<center>
<h2>working time</h2>
<form action="" method="post">
<button name="starttime" class="starttime">starttime</button>
<?php
session_start();
to
<?php
session_start();
?>
<html>
<body>
<center>
<h2>working time</h2>
<form action="" method="post">
<button name="starttime" class="starttime">starttime</button>
<?php

populate text box from MYSQL query

I need to Query a database and then populate a textbox input with the result.
I'm trying
Date Called:
<td>
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK)
{
$Call1query = "SELECT Call1 FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Call1result = mysql_query($Call1query);
while( $row = mysql_fetch_array($Call1result) ){
$Call1 = $row["$Call1Result"];
}
}
?>
<input type="text" name="Call1" id="Call1" value="<?php echo( htmlspecialchars( $Call1) ); ?>"/></td>
But getting nothing out, where am I going wrong, Text inputs seem tricky to populate!
Thankyou!
use instead
$Call1 = $row["Call1"];
Replace
$Call1 = $row["$Call1"];
with
$Call1 = $row["Call1"];
the problem is here change
$Call1 = $row["$Call1Result"];
to
$Call1 = $row['Call1']; //here column name comes not variable name
try like this
<td>
<?php
$selectedSPK=$_POST['SPKSelect'];
$assigned = $_POST['Sales_Exec'];
$date = $_POST['DateSelect'];
if ($selectedSPK)
{
$Call1query = "SELECT Call1 FROM Data WHERE SPKCustNo = '$selectedSPK' ";
$Call1result = mysql_query($Call1query);
while( $row = mysql_fetch_array($Call1result) ){
$Call1 = $row["Call1"];
}
}?>
<input type="text" name="Call1" id="Call1" value="<?php echo( htmlspecialchars( $Call1) ); ?>"/></td>

Correct php syntax

// query
$sql = "INSERT INTO tool (title,details) VALUES (:title,:details) ";
$q = $conn->prepare($sql);
$q->execute(array(':details'=>$details,
':title'=>$title));
Been having trouble with this all day, I've finally got it down to this. If I use the above code, it will just add a new post to the database. This is supposed to be used for editing a post, so obviously I need to edit existing information:
// query
$post = htmlspecialchars($_GET['story']);
$sql = "UPDATE tool SET (title,details) VALUES (:title,:details) WHERE id = $post";
$q = $conn->prepare($sql);
$q->execute(array(':details'=>$details,
':title'=>$title));
'id' is a column in the database. I need it to update the title, and details, for that specific post. I'm just not sure what syntax I'm supposed to be using here.
Thanks for any answers!
===== Second question:
Now I'm back to my old error. Whenever I edit a post, it will lose the title and details, only once. The first time I edit the post, I lose all information, but the rest of the times it will work just fine. Any idea why? Heres the code:
Form from the edit page(may or may not be important, I don't know):
$name = $_SESSION['Username'];
if (in_array($name, $allowedposters)) {
$results = mysql_query("SELECT * FROM tool WHERE id = $post");
while($row = mysql_fetch_array($results)){
$title= $row['title'];
$details= $row['details'];
$date= $row['date'];
$author= $row['author'];
$id= $row['id'];
echo "<a href=story.php?id=";
echo $post;
echo ">Cancel edit</a> <br><br><b>";
echo $title;
echo "</b> <br><br>";
echo '
<form action="edit-new.php?story=';
echo $id;
echo '" method="post" enctype="multipart/form-data">
<textarea rows="1" cols="60" name="title" wrap="physical" maxlength="100">';
echo $title;
echo '</textarea><br>';
?>
<textarea rows="30" cols="60" name="details" wrap="physical" maxlength="10000">
<?php
echo $details;
echo '</textarea><br>';
echo '<label for="file">Upload featured image:</label><br>
<input type="file" name="file" id="file" />';
echo'<br><input type="submit" />';
}
} else {
echo "Not enough permissions.";
}
?>
Here is the SQL, inserting into DB:
<?php
$post = htmlspecialchars($_GET['story']);
$title = mysql_real_escape_string($_POST['title']);
$details = mysql_real_escape_string($_POST['details']);
echo "B<br>";
echo $_POST['title'];
echo '<br>';
echo $_POST['details'];
echo $post;
echo "<br><br>";
// configuration
$dbtype = "mysql";
$dbhost = "localhost";
$dbname = "zzzz";
$dbuser = "zzzzz";
$dbpass = "zzzzzz";
// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data
// query
$sql = "UPDATE tool SET title=:title, details=:details WHERE id = :postid";
$q = $conn->prepare($sql);
$q->execute(array(
':details'=>$details,
':title'=>$title,
':postid' => $post
));
?>
Fix your sql UPDATE syntax
$sql = "UPDATE tool SET title=:title, details=:details WHERE id = :postid";
$q = $conn->prepare($sql);
$q->execute(array(
':details'=>$details,
':title'=>$title,
':postid' => $post
));

Categories