Undefined Index in vars - php

I'm getting this error in my variables on lines 26 and 27. I have been searching for the problem itself, some people say that the variables are not initialized. Althought I think they are. Also I saw people saying to use isset() / !empty() but I don't understand that, and what it does.
<?php
$nome = $_POST['nome']; //26
$preco = $_POST['preco']; //27
if(count($_FILES) > 0) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
mysql_connect("localhost", "crc", "root");
mysql_select_db ("crc");
$imgData =addslashes(file_get_contents($_FILES['userImage'['tmp_name']));
$sql = "INSERT INTO fios (nome,preco,imagem)VALUES('$nome','$preco','{$imgData}')";
$current_id = mysql_query($sql) or die("<b>Erro:</b> Problema na imagem inserida!<br/>" . mysql_error());
if(isset($current_id)) {
header("Location: veradmin.php");
}}}
?>
<!DOCTYPE html>
<html>
<title>Inserir</title>
</head>
<body>
<form name="frmImage" enctype="multipart/form-data" action="" method="post" class="frmImageUpload">
<div align="center">
</p><tr>
<td width="321"><strong>Nome/Descricao:</strong></td>
<td width="102" align="left">
<input type="text" name="nome" value="" size="40" />
</td>
</tr><p>
</p><tr>
<td width="321"><strong>Preco:</strong></td>
<td width="102" align="left">
<input type="text" maxlength="9" name="preco" value="" size="20" />
</td><p>
</p></tr>
<input name="userImage" type="file" class="inputFile" /><p>
</p><input type="submit" value="Inserir Registo" class="btnSubmit" />
</form>
</div>
</body>
</html>

This issue happened because in the first load $_POST['nome'] and $_POST['preco'] is empty and these indexes does not exists.
In this cases you should check with !empty to run this lines:
if(!empty($_POST['nome']) && !empty($_POST['preco']))
{
if(count($_FILES) > 0) {
...
}
}
At this time if you post your form or not these codes will run which is root cause of showing these notices

Related

PHP/MySql Update query Is Not Working

When I update anything in my admin panel and click on update button then nothing happens — i.e it neither updates it nor goes to the page from where we click edit. I am using free hosting. The same code is working on the local host, but it is not working on free hosting. Can anyone help me?
$sqlN="UPDATE selectplan SET plan_name='$planname',Plandetails='$Plandetails', Plantime='$Plantime', Plandate='$Plandate', plancost='$plancost', img='$image' WHERE id='$id__' ;";
$resultN=mysqli_query($conn,$sqlN);
Here is the complete code
<?php
require "conn.php";
session_start();
if(!isset($_SESSION['id']))
{
header('Location:index.php');
}
else
{
$id__=$_GET['id'];
$sql="SELECT * from selectplan where id='$id__' ;";
$result=mysqli_query($conn,$sql);
if((mysqli_num_rows($result))>0)
{
$row=mysqli_fetch_row($result);
$id_P=$row[0]; //id
$planname=$row[1];//name
$Plandetails=$row[2];
$Plantime=$row[3];
$Plandate=$row[4];
$plancost=$row[5];
$img__=$row[6];
}
}
if (isset($_POST['submit']))
{
$planname=$_POST["planname"];//name
$Plandetails=$_POST["Plandetails"];
$Plantime=$_POST["Plantime"];
$Plandate=$_POST["Plandate"];
$plancost=$_POST["plancost"];
if($planname==""||$Plandetails==""||$Plantime==""||$Plandate==""||$plancost=="")
{
echo "Please fill in all the fields";
}
else
{
$target="images/".basename($_FILES['image']['name'] );
//get all the submitted data from the form
$image=$_FILES['image']['name'];
//now move upload image to folder
if (move_uploaded_file($_FILES['image']['tmp_name'], $target))
{
echo "image upload";
}
$sqlN="UPDATE selectplan SET plan_name='$planname',Plandetails='$Plandetails', Plantime='$Plantime', Plandate='$Plandate', plancost='$plancost', img='$image' WHERE id='$id__' ;";
$resultN=mysqli_query($conn,$sqlN);
if ($resultN) {
header('Location:profile.php');
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Edit Plan</title>
</head>
<body class="t">
<p>Back to Profile
||
Logout Now</p>
<br> <br><br>
<form action="profile.php" method="POST" enctype="multipart/form-data" >
Plan Name:<br><textarea name="planname" id="" value="" cols="80" ><?php echo $planname; ?></textarea><br>
Plan Details:<br><textarea name="Plandetails" id="" value="" cols="80" rows="6"><?php echo $Plandetails; ?></textarea><br>
Plan Time:<br><input type="time" name="Plantime" id="" value="<?php echo $Plantime; ?>" /><br>
Plan Date:<br><input type="date" name="Plandate" id="" value="<?php echo $Plandate; ?>" /><br><br>
Plan Cost:<br><input type="numbers" name="plancost" id="" value="<?php echo $plancost; ?>" /><br><br>
Image:<br><?php echo "<img src='images/$img__'"; ?> alt="Tourism" height="150" width="300" > <br><input type="hidden" name="size" value="1000000"><input type="file" name="image" /><br></br> <input type="submit" name="submit" value="Update now ">
</form>
</body>
</html>
i think your problem is in conn.php
your auth values is incorrect
in conn.php yku should change the value that is currently localhost and change the user and pass

error with fill in a mysql table via php

I tried to make a code which will add an entry to my MySQL table (called "rechnungen") via php. So I made some inputs in html and finaly I tried to insert the informations into my table (using the INSERT INTO... command). So this is what i made:
<?php
Session_Start();
$username=$_SESSION['username'];
$password=$_SESSION['password'];
$dbname=$_SESSION['dbname'];
$servername=$_SESSION['hostname'];
/*conn dev*/
$conn = mysql_connect($servername, $username, $password);
if($conn === false){
header("Location: LogIn.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<title></title>
</head>
<body>
<main>
<form method="POST" action="">
<div class="form_neueRechnung">
<!-- part 1 -->
<input type="text" name="suche_Vname_Patienten" placeholder="Vorname" required="">
<input type="text" name="suche_Nname_Patienten" placeholder="Nachname" required="">
<input type="number" id="id_Patient" name="id_patient" placeholder="Pat. Nr." Value="
<?php echo $KID_output; ?>" required="">
</td>
<input type="radio" name="Behandlung" value="Osteopathie" onclick="andere()" required="">
<input type="radio" name="Behandlung" value="Krankengymnastik" onclick="andere()" required="">
<input type="radio" name="Behandlung" id="andere_Behandlung" value="andere" onclick="andere()" required="">
<input type="text" name="andereBehandlung_text" id="andereBehandlung_text" placeholder="andere" style="visibility:hidden">
<!-- part 2 -->
<input type="radio" name="rezept_rechnung" id="mit_rezept" value="mit_Rezept" onclick="rezept()" required="">
<input type="radio" name="rezept_rechnung" id="ohne_rezept" value="ohne_Rezept" onclick="rezept()" required="">
<input type="text" id="ohne_rezept_text" name="ohne_rezept_text" placeholder="freier Text">
<!-- part 3 -->
<input type="time" name="termin1_von" required="">
<input type="time" name="termin1_bis" required="">
<input type="date" name="termin1_date" required="">
<!-- submit -->
<input type="submit" class="submit" value="Rechnug erstellen" name="submit" id="submit">
</div>
<div class="form_fieldset" id="rezept_einstellungen" style="visibility:hidden">
<input type="date" id="rezept_datum" name="rezept_datum">
<input type="text" id="rezept_verordnung" name="rezept_verordnung">
<input type="text" id="rezept_diagnose" name="rezept_diagnose">
</div>
</form>
<script type="text/javascript">
function andere() {
if (document.getElementById('andere_Behandlung').checked) {
document.getElementById('andere_BehandlungArt').style.visibility = 'visible';
} else {
document.getElementById('andere_BehandlungArt').style.visibility = 'hidden';
}
}
function rezept() {
if (document.getElementById('mit_rezept').checked) {
document.getElementById('rezept_einstellungen').style.visibility = 'visible';
} else {
document.getElementById('rezept_einstellungen').style.visibility = 'hidden';
}
if (document.getElementById('ohne_rezept').checked) {
document.getElementById('ohne_rezept_text').style.visibility = 'visible';
} else {
document.getElementById('ohne_rezept_text').style.visibility = 'hidden';
}
}
</script>
<?php
mysql_connect("$servername","$username","$password") or die("connection failed!");
mysql_select_db($dbname) or die ("no database found");
$query = mysql_query("SELECT * FROM `rechnungen`");
while($row = mysql_fetch_array($query)){
$RID = $row['RechnungsID'];
}
$RechnungsID = max($RID ,$RID)+1;
echo $RechnungsID;
$mit_ohne_Rezept = "";
if(isset($_POST['submit'])) {
if($_POST['rezept_rechnung'] == "mit_Rezept") {
$mit_ohne_Rezept = "1";
}
else {
$mit_ohne_Rezept = "0";
}
}
if(isset($_POST['submit'])){
$KundenID=$_POST['id_patient'];
$Behandlung=$_POST['Behandlung'];
$Rezept_datum=$_POST['rezept_datum'];
$Rezept_Verordnung=$_POST['rezept_verordnung'];
$Rezept_Diagnose=$_POST['rezept_diagnose'];
$ohneRezept_text=$_POST['ohne_rezept_text'];
mysql_select_db($dbname,$conn);
$result = "INSERT INTO rechnungen (`RechnungsID`, `KundenID`, `Behandlung`, `mit_ohne_Rezept`, `Rezept_datum`, `Rezept_Verordnung`, `Rezept_Diagnose`, `ohneRezept_text`)
VALUES ('$RechnungsID','$KundenID','$Behandlung','$mit_ohne_Rezept','$Rezept_datum','$Rezept_Verordnung','$Rezept_Diagnose','$ohneRezept_text)";
if (mysql_query($result)) {
echo ("finished!");
} else {
echo "error". mysql_error();
}
}
mysql_close($conn);
?>
</main>
</body>
</html>
I know it's a pretty long code, but i don't know where the problem could be. I'm getting this error:
errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''sdfas)' at line 2
please help me. I'm despairing.
','$ohneRezept_text)";
looks like the issue is here.
missing a quotation mark?
that's what the error is saying
also you don't need to wrap variables in quotations, well you can but its still a pain. if your input contains quotation marks it skips right out. Use addslashes()

Did'n do nothing using if statement to go to another page PHP

I want go to another page if the condition allowed. But when i submit the button it just like refresh the page. it doesn't direct to another page.
I have already read another question remain same but did not change.
Here is my code :
<?php
session_start();
if(isset($_POST['login']))
{
$nik = $_POST['nik'];
$pswd = $_POST['pswd'];
if ((empty($nik)) or (empty($pswd)))
echo "Data masih ada yang kosong<br/><br/>";
else
{
include("sss_connection.php");
$connection = mysql_connect($server,$user,$pass);
$db = mysql_select_db("skripsi");
if(!$connection)
{
echo "Database belum terkoneksi<br/><br/>";}
elseif(!$db)
{
echo "Database Tidak Ada<br/><br/>";}
else
{
$sql1 = "select count(*) as cek,kd_jabatan from datakaryawan where nik='".$nik."' and password='".$pswd."'";
//$exec = mysql_query($sql1);
$ambil_data = mysql_query($sql1);
if ($data = mysql_fetch_array($ambil_data))
{
$cek = $data["cek"];
$jabatan = $data["kd_jabatan"];
}
if(($jabatan) == 0)
{
echo "Anda gagal Login, NIK / Password salah<br/><br/>";
}
else
{
header("Location: sss_header.php");
}
}
}
}?>
And here is my html code :
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="post">
<div style="float:left;margin-left:35%;margin-top:5%;">
<center><strong><font size="6">Login</font></strong></center>
</br></br>
<table style="float:left;margin-left:5%;">
<tr>
<td width=84>NIK</td>
<td width=10>:</td>
<td width=30><input type="text" name="nik" size=30 onkeypress="return isNumber(event)" style="width: 217px" autofocus></td>
</tr>
<tr>
<td width=70>Password</td>
<td width=10>:</td>
<td width=30><input type="password" name="pswd" size=30 style="width: 217px"></td>
</tr>
</table><blockquote><blockquote><br/><br/>
<input type="submit" value="Login" name="login" style="float:left; margin-left:45px; margin-top:12px;">
<input type="submit" value="Daftar" name="daftar" style="float:left; margin-left:26px; margin-top:12px;">
<input type="reset" value="Clear" style="float:left; margin-left:26px; margin-top:12px;">
</form>
</div>
<div style="float:left;margin-left:35%;margin-top:5%;">
</div>
</body>
</html>
you have to specified the action attribute on your form tag, should be like this:
<form method="post" action="anotherplace.php">

could not inserting data to mysql table using php

I have following form in html
<form method="post" enctype="multipart/form-data" />
<fieldset>
<legend>Activate Scheme</legend>
<p>Date Of Draw</p>
<p><input type="text" name="dateOfDraw" class="textBox" style="width:150px" /></p>
<p>Time Of Draw</p>
<p><input type="text" class="textBox" name="timeOfDraw" style="width:150px" /></p>
<p>Enter scheme name</p>
<p><input type="text" class="textBox" name="schemeName" style="width:150px" />
</p>
<p>Upload Image</p>
<p><input type="file" name="image" id="image" /></p>
<p><input name="scheme_button" type="submit" class="button1" value="Submit"></p>
</fieldset>
</form>
Problem is that their is no error when I execute the following query
<?php
if(isset($_POST['submit_button']) && count($_POST)>0) {
print_r($_POST);
$dateOfDraw = $_POST['dateOfDraw'];
$timeOfDraw = $_POST['timeOfDraw'];
$schemeName = $_POST['schemeName'];
$imageName = $_FILES['image']['name'];
$destination = '../images/'.$imageName;
$source = $_FILES['image']['tmp_name'];
if(move_uploaded_file($source, $destination)) {
echo 'file uploaded';
} else {
echo ' file not uploaded';
}
$sSQL = "INSERT INTO landing_page(dateOfDraw, timeOfDraw, schemeName, image) VALUES('$dateOfDraw','$timeOfDraw','$schemeName','$imageName')";
echo $sSQL;
if(!$sSQL){
die(mysqli_query($con));
}
mysqli_query($con,$sSQL);
//header("location: list_products.php");
}
?>
but when I check my data in a mysql database the rows are still empty. Please check it am I missing something or the php code is wrong.
Note: I am using php version 5.5.16
The name of your submit button is scheme_button and that's exactly the thing that you need to $_POST.
if(isset($_POST['scheme_button']) && count($_POST) > 0)
You form doesn't have an action and it doesn't know where to go. So add one.
<form method="post" action="index.php" enctype="multipart/form-data" />
firstly you have to name of submit button
if(isset($_POST['scheme_button']) && count($_POST) > 0)
secondly if insert code in same page then
if insert code in another page then
action = "filename"

Not inserting data into database

I'm trying to get my PHP/HTML back up to scratch, and I've started by designing my own little news/whatever system. What I'm trying to do for efficiency is to run the Add/Edit/Delete all from the one process.php file via a switch($x), but for some reason it won't insert any data, and it won't give me any errors. I'm completely lost on what to do here. If anyone could help me out the code for both files is as below.
process.php
<?php
include("config.php");
if (!isset($_GET['x'])) {
$x = $_GET[x];
switch($x) {
case "add":
$title = $_POST['title'];
$text = $_POST['text'];
$date = $_POST['date'];
$author = $_POST['author'];
mysql_query("INSERT INTO posts(id, title, text, date, author) VALUES(null, '$title', '$text', '$date', '$author')") or die(mysql_error());
echo("Article inserted. Click <a href=\"index.php\" />here</a> to return.");
break;
case "gohome":
echo("Looks like you've taken a wrong turn. Click here to return.");
default:
echo("Go home.");
break;
}
} else {
$x = 'gohome';
}
?>
index.php (adding data)
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/style.css" />
</head>
<body>
<div align="center" /><font size="20px;" />test</font></div>
<?php include("includes/navigation.php"); ?>
<div align="center" />
<fieldset><legend>Submit an article</legend>
<form action="includes/process.php?x=add" method="post" />
<input name="title" type="text" value="Title" onfocus="if(this.value=='Title') this.value='';" onblur="if(this.value=='') this.value='Title';"/><br />
<input name="date" type="text" value="Date" onfocus="if(this.value=='Date') this.value='';" onblur="if(this.value=='') this.value='Date';"/><br />
<textarea rows="4" cols="50" name="text" /></textarea><br />
<input name="author" type="text" value="Author" onfocus="if(this.value=='Author') this.value='';" onblur="if(this.value=='') this.value='Author';"/><br />
<input type="submit" />
</form>
</fieldset>
</body>
</html>
This code:
if (!isset($_GET['x'])) {
$x = $_GET[x];
should be:
if (isset($_GET['x'])) {
$x = $_GET['x'];
You had the test backwards, so when the parameter was set you weren't going into the switch.

Categories