Crud application wont instert into database - php

my application wont insert into database, and its not giving me any errors.
If my question is a duplicate, please redirect me to the original question, thanks.
MY PHP CODE:
<?php
include_once("dbconfig.php");
if(isset($_POST['submit'])){
/*$targetDir = "slike/";
$allowTypes = array('jpg','png','jpeg','gif');
$statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';*/
$image = $_FILES['files']['tmp_name'];
$file = addslashes(file_get_contents($image));
$name = mysqli_real_escape_string($mysqli, $_POST['ime']);
$group=mysqli_real_escape_string($mysqli,$_POST['grupa']);
$semi=mysqli_real_escape_string($mysqli,$_POST['podgrupa']);
$price=mysqli_real_escape_string($mysqli,$_POST['cena']);
if(empty($name) || empty($group) || empty($semi)||empty($price)) {
echo "<font color='red'>Namesteno je da sve moraju da budu popunjene.</font><br/>";
if(empty($name)) {
echo "<font color='red'>Ime je prazno.</font><br/>";
}
if(empty($group)) {
echo "<font color='red'>Grupa je prazna.</font><br/>";
}
if(empty($semi)) {
echo "<font color='red'>Podgrupa je prazna.</font><br/>";
}
if(empty($price)){
echo "<font color='red'>Cena je prazna.</font><br/>";
}
}
else{
$result = mysqli_query($mysqli, "INSERT INTO slike(ime,grupa,podgrupa,cena,slika) VALUES('$name','$group','$semi','$price','$file')");
echo "<font color='green'>Data added successfully.";
}
}
?>
My html:
</div>
<div class="content">
<form action="" method="post" enctype="multipart/form-data">
Slike biras ovde:
<input type="file" name="files">
<p>IME</p>
<input type="text" name="ime">
<p>GRUPA</p>
<input type="text" name="grupa">
<p>PODGRUPA</p>
<input type="text" name="podgrupa">
<p>CENA</p>
<input type="text" name="cena">
<input type="submit" name="submit" value="UPISI">
</form>
If you need some more codes i will post it in replies.
Problem is that i get the message "Data added successfully"
But when i go to the database, its empty.
THANKS to anyone who helps me.

<form action="/your_php_file.php" method="post" enctype="multipart/form-data">
Please set action to your form which should go to your php file.

Related

issues with multiple forms on php page

i am trying to make a simple login system, with password recovery option,, so i made a password reset link.
hwoever, it is not working, meaning that form2's button just leads back to form1 (leads back to username and email form && i have three different forms), so i separated it into three different if statements, for each button clicked, but the same issue keeps on happening.
please tell me what is happening and ho to fix it
thank you.
(code is below)
//not actually js, but is php
session_start();
if(isset($_SESSION['username']) && isset($_SESSION['password'])){
header("Location: changepass.php");
}
if(($_SERVER["REQUEST_METHOD"] == "POST")) {
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "test";
$con = new mysqli($dbhost, $dbuser, $dbpass, $db) or die("Connection failed: %s\n". $con -> error);
$GLOBALS['email'] = $_POST['email'];
$GLOBALS['username'] = $_POST['username'];
$result = mysqli_query($con,"SELECT * FROM login WHERE email='" . htmlspecialchars($GLOBALS['email']) . "' and username = '". htmlspecialchars($GLOBALS['username'])."'");
$count = mysqli_num_rows($result);
//Part 1
if($_POST['submit1']) {
if($count==0) {
echo "<script>
document.getElementById('error').innerHTML += 'Invalid Username or Email.';
</script>";
} else {
echo "<script>
document.getElementById('main').style.display = 'none';
</script>";
echo "<script>
document.getElementById('next').style.display = 'inline-block';
</script>";
echo "<script>
document.getElementById('verify').innerHTML += 'A verification email has been sent to you. Copy the verification code and paste it above.';
</script>";
$GLOBALS['token'] = bin2hex(random_bytes(3));
echo $GLOBALS['token'];
$to = $GLOBALS['email'];
$subject = "Password Reset";
$msg = "Hello. Your token is <strong>" . $GLOBALS['token'] . "</strong>. <br>Good day.";
$msg = wordwrap($msg,70);
$headers = "From: email#example.com";
mail($to, $subject, $msg, $headers);
}
}
//Part 2
if($_POST['submit2']) {
if($_POST['code'] != $GLOBALS['token']) {
echo "<script>
document.getElementById('error2').innerHTML += 'Invalid verification code.';
</script>";
} else {
echo "<script>
document.getElementById('next').style.display = 'none';
</script>";
echo "<script>
document.getElementById('final').style.display = 'inline-block';
</script>";
}
}
//Part 3
if($_POST['submit3']) {
$np = $_POST['np'];
$cnp = $_POST['cnp'];
if($np != $cnp) {
echo "<script>
document.getElementById('error3').innerHTML += 'Passwords do not match.';
</script>";
} else {
$sql = "UPDATE login SET password='$cnp' WHERE email=" . $GLOBALS['email'];
$rs = mysqli_query($con, $sql);
if($rs) {
echo "Changed password successfully! Click <a href='login.php'>here</a> to sign in.";
} else {
echo "An unknown error occurred. Please try again.";
}
}
}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Reset Password</title>
</head>
<body>
<fieldset>
<legend>Reset Password</legend>
<form name="frmContact" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div id="main">
<label for="email">Email</label>
<input type="email" style="display:inline-block" name="email" id="email" required autofocus />
<br>
<br>
<label for="username">Username</label>
<input type="text" style="display:inline-block" name="username" id="username" required />
<br>
<p id="error" style="color:red"></p>
<p> </p>
<p>
<input type="submit" name="submit1" id="submit1" value="Reset Password" /> Create an Account Sign in
</p>
</div>
</form>
<form name="frmContact2" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div id="next" style="display:none;">
<p id="verify" style="color:green"></p>
<label for="code">Verification Code</label>
<input type="text" style="display:inline-block" maxlength="6" name="code" id="code" required autofocus /> <p style="color:red;display:inline-block" id="validatecode"></p>
<br>
<p id="error2" style="color:red"></p>
<p> </p>
<p>
<input type="submit" name="submit2" id="submit2" value="Reset Password" /> Create an Account Sign in
</p>
</div>
</form>
<form name="frmContact3" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div id="final" style="display:none;">
<label for="np">New Password</label>
<input type="text" style="display:inline-block" name="np" id="np" required autofocus /> <p style="color:red;display:inline-block" id="validatenp"></p>
<br>
<label for="cnp">Confirm New Password</label>
<input type="text" style="display:inline-block" name="cnp" id="cnp" required autofocus /> <p style="color:red;display:inline-block" id="validatecnp"></p>
<br>
<p id="error3" style="color:red"></p>
<p> </p>
<p>
<input type="submit" name="submit3" id="submit3" value="Reset Password" />
</p>
</div>
</form>
</fieldset>
</body>
</html>
Just for sake of debugging remove all extra stuff from that file and focus on 3 if statements
Also try using
if(isset($_POST['submit1']))
Always try to close in onto the problem at hand and remove extra stuff that is in there. It helps make simpler but better decisions.

What's wrong with this POST method?

i have tried to fix my this upload code in my php :
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
$nama=$_SESSION["username"];
if (($nama)!='Admin'){
echo "<script>
window.location = '/home'
</script>";
}
?>
<h2>Upload Item Panel</h2>
<p><span class="error">*Semua harus diisi</span></p>
<form method="post" action="listingupload.php/" enctype="multipart/form-data">
Nama Barang: <input type="text" name="name" value="">
<br><br>
Stock :<input type="text" name="stock" value="">
<br><br>
Warna: <input type="text" name="warna" value="">
<br><br>
RAM: <input type="text" name="ram" value="">
<br><br>
kondisi: <input type="text" name="kondisi" value="">
<br><br>
Harga: <input type="text" name="harga" value="">
<br>
<b>Harap mengisi kolom harga tanpa titik (Dua juta rupiah = 2000000)</b>
<br><br>
Harga Sebelum diskon: <input type="text" name="hargabefore" value="">
<br><br>
Deskripsi: <textarea name="deskripsi" rows="5" cols="40"></textarea>
<br><br>
Gambar Listingan:<br><br>
<input type="file" name="pic"><br><br>
Gambar Detail 1:<br><br>
<input type="file" name="pic2"><br><br>
Gambar Detail 2:<br><br>
<input type="file" name="pic3"><br><br>
Gambar Detail 3:<br><br>
<input type="file" name="pic4"><br><br>
Gambar Detail 4:<br><br>
<input type="file" name="pic5"><br><br>
Gambar Detail 5:<br><br>
<input type="file" name="pic6"><br><br>
<input type="submit" value="POST" name="submit">
</form>
</body>
</html>
so that is the code for the form page
and here is the code for the POST method :
<?php
$namafile = $_FILES['pic']['name'];
$namafile2 = $_FILES['pic2']['name'];
$namafile3 = $_FILES['pic3']['name'];
$namafile4 = $_FILES['pic4']['name'];
$namafile5 = $_FILES['pic5']['name'];
$namafile6 = $_FILES['pic6']['name'];
$ukuran = $_FILES['pic']['size'];
$error = $_FILES['pic']['error'];
$ukuran2 = $_FILES['pic2']['size'];
$error2 = $_FILES['pic2']['error'];
$ukuran3 = $_FILES['pic3']['size'];
$error3 = $_FILES['pic3']['error'];
$ukuran4 = $_FILES['pic4']['size'];
$error4 = $_FILES['pic4']['error'];
$ukuran5 = $_FILES['pic5']['size'];
$error5 = $_FILES['pic5']['error'];
$ukuran6 = $_FILES['pic6']['size'];
$error6 = $_FILES['pic6']['error'];
include 'mysqldata.php';
$item=$_POST['name'];
$deskripsi=$_POST['deskripsi'];
$price=$_POST['harga'];
$stock=$_POST['stock'];
$ram=$_POST['ram'];
$warna=$_POST['warna'];
$kondisi=$_POST['kondisi'];
$hargabefore=$_POST['hargabefore'];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn= new mysqli($host,$dbUsername,$dbPassword,$dbname);
if (mysqli_connect_error()){
echo mysqli_errno($this->db_link);
die('Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
} else{
echo "Redirecting....\n";
if (move_uploaded_file($_FILES['pic']['tmp_name'],'listing/'.$namafile)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic2']['tmp_name'],'listing/'.$namafile2)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic3']['tmp_name'],'listing/'.$namafile3)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic4']['tmp_name'],'listing/'.$namafile4)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic5']['tmp_name'],'listing/'.$namafile5)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic6']['tmp_name'],'listing/'.$namafile6)){
echo "Success";
}else{
echo "Failed";
}
$sql = "INSERT INTO $dbname.itemlist(Itemname, Description,Harga,Image,stock,picture,picture2,picture3,picture4,picture5,warna,ram,kondisi,pseudoprice) VALUES ('$item','$deskripsi','$price','$namafile','$stock','$namafile2','$namafile3','$namafile4','$namafile5','$namafile6','$warna','$ram','$kondisi','$hargabefore')";
$conn->query($sql);
}
?>
sometimes this works normally, so i got all of my data and my files uploaded to the mysql, BUT sometimes it just give me the PHP Notice of PHP Undefined Index for ALL OF my data including Price, Image, Stock etc.
I have no idea at all why could this happen, can anyone help me with this one?
EDIT :i did fill in all of the form data, but when i POST it, sometimes it still shows undefined index , the problem is that, if it shows UNDEFINED INDEX doesn't it mean that all of my data is not sent to the mysql? Because is did fill all of the data in my form, the problem is that in the POST script it told me that all of the data is NULL (Undefined index)
if i only uploaded 2-3 pictures the code is working, but if i uploaded 5 pictures then the undefined index error shows up
I am supposing that you have to check if form is submiited or not.
if(!empty($_POST) && !empty($_FILES))
{
$namafile = $_FILES['pic']['name'];
$namafile2 = $_FILES['pic2']['name'];
$namafile3 = $_FILES['pic3']['name'];
$namafile4 = $_FILES['pic4']['name'];
$namafile5 = $_FILES['pic5']['name'];
$namafile6 = $_FILES['pic6']['name'];
$ukuran = $_FILES['pic']['size'];
$error = $_FILES['pic']['error'];
$ukuran2 = $_FILES['pic2']['size'];
$error2 = $_FILES['pic2']['error'];
$ukuran3 = $_FILES['pic3']['size'];
$error3 = $_FILES['pic3']['error'];
$ukuran4 = $_FILES['pic4']['size'];
$error4 = $_FILES['pic4']['error'];
$ukuran5 = $_FILES['pic5']['size'];
$error5 = $_FILES['pic5']['error'];
$ukuran6 = $_FILES['pic6']['size'];
$error6 = $_FILES['pic6']['error'];
include 'mysqldata.php';
$item=$_POST['name'];
$deskripsi=$_POST['deskripsi'];
$price=$_POST['harga'];
$stock=$_POST['stock'];
$ram=$_POST['ram'];
$warna=$_POST['warna'];
$kondisi=$_POST['kondisi'];
$hargabefore=$_POST['hargabefore'];
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn= new mysqli($host,$dbUsername,$dbPassword,$dbname);
if (mysqli_connect_error()){
echo mysqli_errno($this->db_link);
die('Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
} else{
echo "Redirecting....\n";
if (move_uploaded_file($_FILES['pic']['tmp_name'],'listing/'.$namafile)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic2']['tmp_name'],'listing/'.$namafile2)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic3']['tmp_name'],'listing/'.$namafile3)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic4']['tmp_name'],'listing/'.$namafile4)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic5']['tmp_name'],'listing/'.$namafile5)){
echo "Success";
}else{
echo "Failed";
}
if (move_uploaded_file($_FILES['pic6']['tmp_name'],'listing/'.$namafile6)){
echo "Success";
}else{
echo "Failed";
}
$sql = "INSERT INTO $dbname.itemlist(Itemname, Description,Harga,Image,stock,picture,picture2,picture3,picture4,picture5,warna,ram,kondisi,pseudoprice) VALUES ('$item','$deskripsi','$price','$namafile','$stock','$namafile2','$namafile3','$namafile4','$namafile5','$namafile6','$warna','$ram','$kondisi','$hargabefore')";
$conn->query($sql);
}
}
Do this format to check if the variables have been set.
if (isset($_FILES['pic']['name']))
{
$namafile = $_FILES['pic']['name'];
}
if (isset($_POST['name']))
{
$item = $_POST['name'])
}
// do the rest

Issue connecting to database - php

I am trying to upload an image of a plane and then the user needs to fill the registration, airport, and company and then upload it, the problem is that the information is not going to the database, any suggestion?
<?php
include("connection.php");
?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Matricula: <input type="text" name="registration" value=""/><br><br>
Aeroporto: <input type="text" name="airport" value=""/><br><br>
Companhia: <input type="text" name="company" value=""/><br><br>
Upload da foto: <input type="file" name="uploadfile" value=""/><br><br>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if ($_POST['submit'])
{
$mat = $_POST["registration"];
$air = $_POST["airport"];
$comp = $_POST["company"];
$filename = $_FILES["uploadfile"]["name"];
$tempname = $_FILES["uploadfile"]["tmp_name"];
$folder = "fotos/".$filename;
move_uploaded_file($tempname, $folder);
if($mat!="" && $air!="" && $comp!="" && $filename!="")
{
$query = "INSERT INTO aviacao VALUES
('$mat','$air','$comp','$folder')";
$data = mysql_query($conn, $query);
if($data)
{
echo "Foto inserida na Base de Dados";
}
}
else
{
echo "Preencher todos os campos";
}
}
?>
</body>
</html>
Database
1 matricula varchar(35)
2 aeroporto varchar(100)
3 companhia varchar(100)
4 foto varchar(100) (here should go the path to the folder where
the photos are beign stored)

php forms multiple input upload

I wrote a multiple form, but it doesn't work. Code inspector tells me something is wrong with move_uploaded_file function. Can anyone tell me what the problem is?
My HTML code:
<div class="setting post">
<form action="add-banner.php" method="post" enctype="multipart/form-data">
<input type="text" name="banner-title" placeholder="enter new banner title"><br>
<select name="banner-cat">
<?php
$get_cats = mysqli_query($db,'select * from cats');
while($row = mysqli_fetch_assoc($get_cats)){
?>
<option value="<?php echo $row['id']?>"><?php echo $row['cat_name'] ?></option>
<?php
}
?>
</select>
<br>
<input type="file" name="banner">
<br>
<input type="submit" name="upload" value="add new banner">
</form>
</div>
and this is my php code:
<?php
require_once 'db.php';
global $db;
$banner_title = $_POST['banner-title'];
$banner_cat = $_POST['banner-cat'];
$banner = $_FILES['banner']['name'];
$banner_tmp = $_FILES['banner']['tmp_name'];
$upload_file = move_uploaded_file($banner_tmp,'../../images/$banner');
$insert_banner = mysqli_query($db,"insert into banner(banner_title,banner_cat,banner_link) values ('$banner_title','$banner_cat','$banner')");
if($insert_banner && $upload_file){
$message = 'New banner Succesfully added';
echo "<script>
alert('".$message."');
window.location.href='post.php';
exit;
</script>";
}else{$message = 'Something goes Wrong';
echo "<script>
alert('".$message."');
window.location.href='post.php';
exit;
</script>";
}
?>
<?php
require_once 'db.php';
global $db;
$banner_title = $_POST['banner-title'];
$banner_cat = $_POST['banner-cat'];
$banner = basename($_FILES['banner']['name']);
$banner_tmp = $_FILES['banner']['tmp_name'];
$upload_file = move_uploaded_file($banner_tmp,'/images/$banner');
$insert_banner = mysqli_query($db,"insert into banner(banner_title,banner_cat,banner_link) values ('$banner_title','$banner_cat','$banner')");
if($insert_banner && $upload_file){
$message = 'New banner Succesfully added';
echo "<script>
alert('".$message."');
window.location.href='post.php';
exit;
</script>";
}else{$message = 'Something goes Wrong';
echo "<script>
alert('".$message."');
window.location.href='post.php';
exit;
</script>";
}
?>
// Don't use back folder for uploaded file, if use back folder, use full url for folder.

PHP Email Subscription, cannot post php file

Hey I have some trouble with my email subscription I'm trying to log into a textfile.
<!-- Signup Form -->
<form id="signup-form" action="form.php" method="post">
<input type="email" name="email" id="email" placeholder="Email Address" />
<input type="submit" value="Sign Up" />
</form>
Here's my PHP code:
<?php
if(empty($_POST['submit']) === false) {
$email = htmlentities(strip_tags($_POST['email']));
$logname = 'email.txt';
$logcontents = file_get_contents($logname);
if(strpos($logcontents,$email)) {
die('You are already subscribed.');
} else {
$filecontents = $email.',';
$fileopen = fopen($logname,'a+');
$filewrite = fwrite($fopen,$filecontents);
$fileclose = fclose($fileopen);
if(!$fileopen or !$filewrite or !$fileclose) {
die('Error occured');
} else {
echo 'Your email has been added.';
}
}
}
?>
I keep getting Cannot POST /form.php eventho the php file is at that path, someone knows what I'm possibly doing wrong? I'm quit noob at this :(
First of all add name attribute to your submit button, as in POST you will not get the form value of "submit" and your code will never go further
<form id="signup-form" action="form.php" method="post">
<input type="email" name="email" id="email" placeholder="Email Address" />
<input type="submit" name="submit" value="Sign Up" />
</form>
Now in your php code, you just need to check $_POST['submit'] in if condition and you have also done a typo, you have used a variable $fopen which is not defined, change that.
<?php
if($_POST['submit']) {
$email = htmlentities(strip_tags($_POST['email']));
$logname = 'email.txt';
$logcontents = file_get_contents($logname);
if(strpos($logcontents,$email)) {
die('You are already subscribed.');
} else {
$filecontents = $email.',';
$fileopen = fopen($logname,'a+');
$filewrite = fwrite($fileopen,$filecontents);
$fileclose = fclose($fileopen);
if(!$fileopen or !$filewrite or !$fileclose) {
die('Error occured');
} else {
echo 'Your email has been added.';
}
}
}
?>

Categories