I have written a code - It takes user input and displays sha1() string for the input.
I get the 'undefined index' notice on local host even after declaring the variables and using isset() function.
Notice: Undefined index: enc in C:\Program Files\EasyPHP-12.1\www\MySQL\localhost_drupal\encrypt.php on line 61
It works fine on the remote (production) server.
What is the fault in the following code?
<?php
//variable declaration
$enc = $len = "";
// checking with isset
if(isset($_POST['enc'])) {
//sha1()
$enc = sha1($_POST['enc']);
// strlen function
$len = strlen($_POST['enc']);
}
?>
//HTML
<!DOCTYPE html>
<html>
<head><title>Generate Password</title></head>
<body>
<form name = "encry" action = "encrypt.php" method = "post">
<input type = "text" name = "enc" >
<input type = "submit" value = "Submit">
</form>
//OUTPUT
<?php
//Line 61
echo "<p> You entered :" . $_POST['enc']."</p>";
echo "<p> Length of the string you entered :" . $len. "<p>";
echo "<p> Your Password is : ". $enc . "</p>";
?>
</body>
</html>
The reason being for the error is that your entire code is inside one page and errors out on initial load. Error reporting is set on one server, but isn't on the other.
You should be using isset() against a named submit button (which I have named) and around where you're using
echo "<p> You entered :" . $_POST['enc']."</p>"; etc..
Change your code to the following:
<?php
//variable declaration
$enc = $len = "";
// checking with isset
if(isset($_POST['enc'])) {
//sha1()
$enc = sha1($_POST['enc']);
// strlen function
$len = strlen($_POST['enc']);
}
?>
//HTML
<!DOCTYPE html>
<html>
<head><title>Generate Password</title></head>
<body>
<form name = "encry" action = "encrypt.php" method = "post">
<input type = "text" name = "enc" >
<input type = "submit" value = "Submit" name = "submit">
</form>
//OUTPUT
<?php
if(isset($_POST['submit'])) {
//Line 61
echo "<p> You entered :" . $_POST['enc']."</p>";
echo "<p> Length of the string you entered :" . $len. "<p>";
echo "<p> Your Password is : ". $enc . "</p>";
}
?>
</body>
</html>
To set error reporting on:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
It's an E_NOTICE. Likely on production you suppress error messages (or at least notices). This is fine, but it does explain why it breaks in development.
Fix the issue and the notice will go away.
Related
So I am running an error check, since my edit post function is not working, and this is the message I am getting
Fatal error: Uncaught mysqli_sql_exception: Unknown column 'trewfh' in 'field list' in C:\xampp\htdocs\Final\Forum\editpost.php:55 Stack trace: #0 C:\xampp\htdocs\Final\Forum\editpost.php(55): mysqli_query(Object(mysqli), 'UPDATE hw7_foru...') #1 C:\xampp\htdocs\Final\Forum\editpost.php(39): editpost(72, 'trewfh') #2 {main} thrown in C:\xampp\htdocs\Final\Forum\editpost.php on line 55
What a confusing error message! It is echoing out exactly what I want it to (the id =72 and the random string =trewfh) however nothing occurs in terms of updating my post. The $connectDB variable comes from including my connection file and universally works everywhere with no issues. I was hoping someone could see what I am not. Thanks for your time and assistance!!!
<?php
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(-1); mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
include 'header.php';
include 'dbconnect.php';
//GATHER MY POST THAT MATCHES THE POST THE USER CLICKS TO EDIT AND DISPLAY TO THEM!
$sql = "SELECT forumpost_ID, forumpost_Text
FROM hw7_forumpost
WHERE forumpost_ID =" . mysqli_real_escape_string($connectDB, $_GET['eid']);
$result = mysqli_query($connectDB,$sql);
if(!$result){ echo 'Something went wrong, please try again later.'; }
else{
while($row = mysqli_fetch_array($result)){
echo'<form action="" method="post">';
echo'<div>';
echo'<textarea name="contents" rows="15" cols="50">'.$row[1].'</textarea>';
echo'</div><div>';
echo'<input type="submit" value="Add Post">';
echo'</div>';
}echo '</form>';
}
if(isset($_POST['contents'])){
$contents = trim($_POST['contents']);
editpost($_GET['eid'], $contents);
echo ('Your post has been updated!');
header('index.php');
die();
}
include 'footer.php';
/********************************************
edit function
**********************************************/
function editpost($id, $contents){
global $connectDB;
$id = (int)$id;
$contents = mysqli_real_escape_string($connectDB, $contents);
$sql = "UPDATE hw7_forumpost SET forumpost_Text=".$contents." WHERE forumpost_ID= ".$id;
$result = mysqli_query($connectDB, $sql);
}
?>
My update should be saying hey database Update hw7_forumpost SET forumpost_Text = 'Trewfh' WHERE forumpost_ID = The correct id it's Getting via $_GET. Its reading everything fine like I said when it's echoing. Something random is happening along the way through
"UPDATE hw7_forumpost SET `forumpost_Text` = -->'<--".$contents."-->'<-- WHERE `forumpost_ID` = ".$id;
It was a simple case of forgetting to place a ' ' in between calling contents as David has pointed out. Thank you David!!!!
i'm currently working on a small script for my Homepage but i ran into a problem.
I Try to upload an Image, but it seems like the POST data from the form is not being received. What did i do wrong?
I already changed the post_max_size and everything in the php.ini.
These are the Errors i get:
"Notice: Undefined index: image in ...." & "Notice: Undefined index:
submit in ...."
<form method="POST" action="/eye/sites/handling/post.php" enctype="multipart/form-data">
<div class="fileUpload">
<span><i class="fa fa-folder-o" aria-hidden="true"></i> Bild wählen</span>
<input type="file" class="upload" name="image"/>
</div>
<input type="submit" value="Upload It!" name="submit"/>
</form>
<?php session_start();
error_reporting(E_ALL);
if (isset($_SESSION["login_stat"])) {
date_default_timezone_set('Europe/Berlin');
$config = "$_SERVER[DOCUMENT_ROOT]/eye/more/config.xml";
$xml = simplexml_load_file($config);
$picWidth = $xml->pic->width;
$picHeight = $xml->pic->height;
$fulldate = date('dmYHis');
if(isset($_POST["submit"])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
$typeCheck = $_FILES['image']['type'];
if ($typeCheck != "image/jpeg") {
$error = "Not a .jpg";
header('location: /eye/sites/post.php?stat=bad&error='.$error);
exit;
}
$file = $_SERVER['DOCUMENT_ROOT']."/uploads/".$fulldate.".jpg";
$type = "image/jpeg";
move_uploaded_file($_FILES['image']['tmp_name'], $file);
$file_thmb = $_SERVER['DOCUMENT_ROOT']."/uploads/!1A_thmb/".$fulldate.".jpg";
include "resize-class.php";
$resizeObj = new resize($file);
$resizeObj->resizeImage($picWidth, $picHeight, 'crop');
$resizeObj->saveImage($file_thmb, 100);
// header('location: /eye/sites/post.php?stat=good');
} else{
// header('location: /eye/sites/post.php?stat=bad&error=No File');
}
} else{
// header('location: /eye/sites/post.php?stat=bad&error=No Data');
echo $_SERVER['CONTENT_TYPE'];
echo "<br>";
echo $_FILES['image']['tmp_name'];
echo "<br>";
echo $_POST['submit'];
echo "<br>";
}
} else {
header('location: /eye/index.php?stat=in');
}
?>
Edit:
The problem is definitely about my Localhost.
This whole thing is working fine on my Webspace, but on my localhost it's not working.
BUT: I'm not getting errors anymore, when is click on Submit it goes to the php file that should save the image, but nothing is happening. I just see a white Page.
But like i said, it runs perfectly on my webspace..
If this is running on your local machine, do a quick check to make sure your "php.ini" file is configured to allow file uploads.
php.ini
file_uploads = On
The codes look fine. Check if your form action is posting to the correct path and if I may suggest using a simpler approach to test your file upload function before making it more complex. Use the following to start testing.
if (isset($_POST["submit"])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
echo "Upload is working!";
}
}
Keep us updated on your findings.
Perhaps this general information will help someone, as it helped me: a submitted form will only include fields that have defined 'name' attributes. 'id' is not enough.
The idea is that 'id' identifies an element in the DOM for use by JavaScript (either as a global variable or for use in document.getElementById(ID)), but 'name' identifies those elements whose names and values will be sent to the destination ('action') page.
So it makes sense that there are two different identifying attributes, used in two different ways.
I would like to send custom text by web to my game server throught this script, but i dunno how to define the text inside input
Any tip or suggestion or fix?
the script is:
<?php
function send_command($ip,$port,$rcon,$command) {
$fp = #fsockopen("udp://".$ip, $port, $errno, $errstr);
if ($fp){
$request = chr(1).chr(0).chr(242).chr(strlen($rcon)).$rcon.pack("S",strlen($command)).$command;
fwrite($fp, $request);
}
}
$name = "Owner";
echo "<input type='text' name='$msg'";
if (isset($_REQUEST['send'])) {
send_command("192.168.1.69",36963,"asdasd","say ©255255255".$name.": ".$msg);
}
?>
<html>
<form>
<input type="submit" value="send" name="send">
</form>
</html>
"Error "is:
Notice: Undefined variable: msg in D:\xampp\htdocs\test\index.php on line 11
Notice: Undefined variable: msg in D:\xampp\htdocs\test\index.php on line 14
Don't define your input's ID with a '$'. PHP interprets that as a variable. Name it 'msg'. You may then reference it with $_POST['msg'] or $_GET['msg'], depending on how you sent it to the PHP file.
I have the following PHP code:
<?php
//Connection to PDO Database
?>
<form method="post" action="">
<p>Busisness telephone 1</p><input id="business_telephone_01" name="business_telephone_01" tabindex="auto" value="<?php echo $result['business_telephone_01']; ?>" type="text" />
<input name="submit" type="submit" value="Save Changes"></form>
<?php
//Get from Form
if((empty($_POST['submit']) === false)){
$business_telephone_01 = $_POST['business_telephone_01'];
//Formating of telephone numbers
$message = 'This message I want to display';
echo 'This is another message';
}
echo $message;
//Code to update table through PDO
?>
Regardless of where I do the echo whether it is echo 'This is another message'; within the conditional brackets or _ echo $message;_ outside the brackets nothing is being echoed and no error is being displayed.
The html form and the PDO are working correctly and are being updated but nothing is being echoed. No error is being shown in the error log.
UPDATE:
If I use if((empty($_POST['submit']) === false)){ I get PHP
Notice: Undefined variable: hello
If I use if (isset($_POST['submit'])) { I get PHP Notice:
Undefined variable: hello
If I use if (!isset($_POST['submit']))
{ it gives me a list of undefined variables that I use e.g. from
my code above business_telephone_01
My full code
if((empty($_POST['submit']) === false)){
//Get from Form
$address_building_name = $_POST['address_building_name'];
$address_building_number = $_POST['address_building_number'];
$address_street = $_POST['address_street'];
$address_locality = $_POST['address_locality'];
$address_postcode = $_POST['address_postcode'];
$address_country = $_POST['address_country'];
//Formating of address
$address_building_number = strtoupper($address_building_number);
$address_building_number = str_replace(' ','',$address_building_number);
$address_building_name = ucwords($address_building_name);
$address_street = ucwords($address_street);
$address_locality = ucwords($address_locality);
$address_postcode = strtoupper($address_postcode);
$address_country = ucwords($address_country);
echo 'Hello';
$good = 'Good bye';
}
echo $good;
I'd be willing to bet $message isn't being echo'd because it's never initialized. (The if block, where it is supposed to be initialized, is not being executed because the conditional is failing.)
First off, you should use isset to determine if a POST variable has been submitted:
if (isset($_POST['submit'])) {
Or, to see if it has not been submitted:
if (!isset($_POST['submit'])) {
Secondly, you should enable error reporting on your program and let us know what errors (if any) you are getting:
error_reporting(E_ALL);
ini_set("display_errors", 1);
You can add these two lines to the very top of your script, right after the opening <?php line, which should help shed some light on the situation.
The problem could be that the $message variable is never set within your if statement.
If the 'if' condition is not matched, then $message is never set and you will not be able to echo the value.
To test, you could set the $message variable to some value before you start your if statement.
$message = 'condition failed';
if(isset($_POST['submit'])){
....
$message = 'This message I want to display';
}
echo $message;
If the condition is met, it will echo "This message I want to display".
If the condition fails, it will echo 'condition failed';
A quick dirty test, but it will tell you if the $message variable will be initialized inside your 'if' statement.
I am trying to learn some new stuff and always wanted to learn how to make a website with PHP and mysql...
I found this easy tutorial and sample files to play with
http://css-tricks.com/php-for-beginners-building-your-first-simple-cms/
I'm trying to add another table it works in the database but when I try to display it it don't work. Here is the code I got and using:
<?php
class simpleCMS {
var $host;
var $username;
var $password;
var $table;
public function display_public() {
$q = "SELECT * FROM laptopvoltage ORDER BY created DESC LIMIT 3";
$r = mysql_query($q);
if ( $r !== false && mysql_num_rows($r) > 0 ) {
while ( $a = mysql_fetch_assoc($r) ) {
$lvmodel = stripslashes($a['lvmodel']);
$lvmanuf = stripslashes($a['lvmanuf']);
$lvvolt = stripslashes($a['lvvolt']);
$entry_display .= <<<ENTRY_DISPLAY
<div class="post">
<h2>
$lvmodel
</h2>
<p> !!!!!!this dont show upp!!!!!! - - - - >>>>>
$lvmanuf
</p><----------- WHY?
<p>
$lvvolt
</p>
</div>
ENTRY_DISPLAY;
}
} else {
$entry_display = <<<ENTRY_DISPLAY
<h2> This Page Is Under Construction </h2>
<p>
No entries have been made on this page.
Please check back soon, or click the
link below to add an entry!
</p>
ENTRY_DISPLAY;
}
$entry_display .= <<<ADMIN_OPTION
<p class="admin_link">
Add a New Entry
</p>
ADMIN_OPTION;
return $entry_display;
}
public function display_admin() {
return <<<ADMIN_FORM
<form action="{$_SERVER['PHP_SELF']}" method="post">
<label for="lvmodel">Title:lv model</label><br />
<input name="lvmodel" id="lvmodel" type="text" maxlength="150" />
<div class="clear"></div>
<label for="lvmanuf">Title:lv manu</label><br />
<input name="lvmanuf" id="lvmanuf" type="text" maxlength="150" />
<div class="clear"></div>
<label for="lvvolt">Title:lvvolt</label><br />
<input name="lvvolt" id="lvvolt" type="text" maxlength="150" />
<div class="clear"></div>
<input type="submit" value="Create This Entry!" />
</form>
<br />
Back to Home
ADMIN_FORM;
}
public function write($p) {
if ( $_POST['lvmodel'] )
$lvmodel = mysql_real_escape_string($_POST['lvmodel']);
if ( $_POST['lvmanuf'] )
$lvmanuf = mysql_real_escape_string($_POST['lvvolt']);
if ( $_POST['lvvolt'] )
$lvvolt = mysql_real_escape_string($_POST['lvvolt']);
if ( $lvmodel && $lvmanuf && $lvvolt ) {
$created = time();
$sql = "INSERT INTO laptopvoltage VALUES('$lvmodel','$lvmanuf','$lvvolt','$created')";
return mysql_query($sql);
} else {
return false;
}
}
public function connect() {
mysql_connect($this->host,$this->username,$this->password) or die("Could not connect. " . mysql_error());
mysql_select_db($this->table) or die("Could not select database. " . mysql_error());
return $this->buildDB();
}
private function buildDB() {
$sql = <<<MySQL_QUERY
CREATE TABLE IF NOT EXISTS laptopvoltage (
lvmodel VARCHAR(150),
lvmanuf TEXT,
lvvolt VARCHAR(150),
created VARCHAR(100)
)
MySQL_QUERY;
return mysql_query($sql);
}
}
?>
it just wont show $lvmanuf. Any help on this would be great as the fields are showing up in my database.
this first file only shows results, if your not to familiar with web logic and design then ill try my best to explain, this first file is called index.php, every website and web-application has a file either call index.html or index.php the reason behind this is that the web server looks for a file named either index.html or index.php and dont misunderstand there are more than just these file types and names a server can start off of its just that these are the most common, since that is out of the way now i will explain the code behind the first file.
as you can see we have set up our basic html document inside and added a script, now the script we made will make the files that are loaded inside the id we specified disappear after a set ammount of seconds, next inside the body of the html we put this code,
<span id="messages">
<?php include "constant.php"; ?>
</span>
this code contains to main players for this script first the span tag with the id attribute tells our javascript the id of the text we want to be invisible after the set amount of seconds, next the
<php include "constant.hpp"; ?>
it includes every thing from the constant.php document we make.
file 1
index.php
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /><meta http-equiv="content-language" content="en-US" />
<title>MySQL Connection test</title>
<script type="text/javascript">
window.onload = function()
{
timedHide(document.getElementById('messages'), 10);
}
function timedHide(element, seconds)
{
if (element) {
setTimeout(function() {
element.style.display = 'none';
}, seconds*1000);
}
}
</script>
</head>
<body>
<span id="messages">
<?php include "constant.php"; ?>
</span>
</body>
</html>
this second file im not going to explain to much about it, since it would make this way to long, but this file is the connection file to the mysql database.
the only part you need to fill in on this is the
$database_ip = ""; //database ip adress goes inside quotes
$database_port = ""; //database port goes inside quotes
$database_name = ""; //database name goes inside quotes
$database_admin_user = ""; //admin username goes inside quotes
$database_admin_pass = ""; //admin password goes inside quotes
this will connect your website to the database.
file 2
constant.php
<?php
$database_ip = ""; //database ip adress goes inside quotes
$database_port = ""; //database port goes inside quotes
$database_name = ""; //database name goes inside quotes
$database_admin_user = ""; //admin username goes inside quotes
$database_admin_pass = ""; //admin password goes inside quotes
//do not modify anything past this point unless you know php well.
$database_link = null;
$database_defaults = array("127.0.0.1","3306","MySQL","root","");
$error_defaults = array("error_no_101" => "required field *IP is empty, using default parameters!",
"error_no_102" => "required field *PORT is empty, using default parameters!",
"error_no_103" => "required field *NAME is empty, using default parameters!",
"error_no_104" => "required field *USER is empty, using default parameters!",
"error_no_105" => "required field *PASS is empty, using default parameters!");
if(empty($database_ip)){
$database_ip = $database_defaults[0];
echo $error_defaults["error_no_101"] . "<br/>";
}
if(empty($database_port)){
$database_port = $database_defaults[1];
echo $error_defaults["error_no_102"] . "<br/>";
}
if(empty($database_name)){
$database_name = $database_defaults[2];
echo $error_defaults["error_no_103"] . "<br/>";
}
if(empty($database_admin_user)){
$database_admin_user = $database_defaults[3];
echo $error_defaults["error_no_104"] . "<br/>";
}
if(empty($database_admin_pass)){
$database_admin_pass = $database_defaults[4];
echo $error_defaults["error_no_105"] . "<br/>";
}
$database_link = mysqli_connect($database_ip, $database_admin_user, $database_admin_pass, $database_name);
if (!$database_link) {
die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
} else {
echo 'Success... ' . mysqli_get_host_info($database_link) . "\n";
}
mysqli_close($database_link);
?>
i put this up to help you fix your code, not to teach you the syntax of the language.
to learn the syntax of php i recommend you go here:
this is the official php website documentation that teach you the correct way to code php,
http://www.php.net/manual/en/langref.php
you could also try this place if you have the money for a subscription:
http://www.lynda.com/MySQL-tutorials/PHP-MySQL-Essential-Training/119003-2.html?srchtrk=index:1%0Alinktypeid:2%0Aq:php%0Apage:1%0As:relevance%0Asa:true%0Aproducttypeid:2
for html you could go to:
http://www.w3schools.com/html/default.asp
you could also try this place if you have the money for a subscription:
http://www.lynda.com/HTML-tutorials/HTML-Essential-Training-2012/99326-2.html