Jump to content

php insert


Ronel

Recommended Posts

Not being able to upload images in the directory with a path name in the database?

1) This is my code for insert into the database and directory:

<?php


$host="localhost";
$username="root";
$pass="";
$db="registration";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Database connection error");
}

// insert query for register page
if(isset($_POST['ronel'])){
     $images = $_FILES['file']['name'];
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["file"]["name"]);

    // Select file type
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Valid file extensions
  $extensions_arr = array("jpg","jpeg","png","gif","pdf");

  // Check extension
  if( in_array($imageFileType,$extensions_arr) )
  {

$details=$_POST['details'];
$location=$_POST['location'];
$checkbox=$_POST['checkbox'];
$injured=$_POST['injured'];    
$agegender=$_POST['agegender'];   
$contact=$_POST['contact'];
$empid=$_POST['empid'];
$dept=$_POST['dept'];    
$organization=$_POST['organization'];     
$summary=$_POST['summary']; 
$name=$_POST['name'];
$outcome=$_POST['outcome'];    
$cause=$_POST['cause'];     
$action=$_POST['action'];
$reportedname=$_POST['reportedname'];
$position=$_POST['position'];    
$organisation=$_POST['organisation'];
$reportedcontact=$_POST['reportedcontact'];
$reporteddept=$_POST['reporteddept'];    
$status="Pending";
$comment=$_POST['comment'];
	
	
	 $query="INSERT INTO `proposals` (`details`,`location`,`date`,`time`,`checkbox`,`injured`,`agegender`,`contact`,`empid`,`dept`  ,`organization`,`summary`,`image`,`outcome`,`cause`,`action`,`reportedname`,`position`,`organisation`,`reportedcontact`,`reporteddept`,`status`,`comment`) VALUES ('$details','$location', current_timestamp(),current_timestamp(),'$checkbox','$injured','$agegender','$contact','$empid','$dept' ,'$organization','$summary','$name','$outcome','$cause','$action','$reportedname','$position','$organisation','$reportedcontact','$reporteddept','$status','$comment')";
	
	$res=mysqli_query($conn,$query);
	if($res){
		$_SESSION['success']="Not Inserted successfully!";
		header('Location:');
	}else{
		echo "<script>alert('Proposal not applied!');</script>";
	}
      // Upload file
     move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$image);
	}
}
date_default_timezone_set("Asia/Kolkata");
?>

2) Here is the input file:

<form class="form-horizontal" method="post" action="" enctype="multipart/form-data">
            <input type="hidden" name="ronel" value="">
  <div class="form-group">
  <label style="position:absolute; left:63%; top:425px;" for="inputEmail" class="col-lg-3"><b>Upload Images Here :</b></label><br><br>
  <div class="col-lg-9">
  <input style="position:absolute; left:78%; top:420px;" type="file" name="file" enctype="multipart/form-data" class="form-control" name="incident_reference" onchange="document.getElementById('inc_ref').src = window.URL.createObjectURL(this.files[0]); document.getElementById('inc_ref').className +='_active'; document.getElementById('inc_ref_span').className += '_hidden'">
  </div><iframe id="inc_ref" class="form-group" width="220px" height="130px" style="position:absolute; left:78%; top:32%;"></iframe></div>
</form>

3) error code:


Notice: Undefined index: name in /opt/lampp/htdocs/create-nearmiss.php on line 57
ONGC TRIPUR

 

Edited by Ronel
change of var
Link to comment
Share on other sites

When you figure out where your "name" field went to, I highly suggest you modify some of your db field names.  Using "date" and "time" is not good practice.  NAME the field, don't just say date or time.  And are all of these fields necessary on that one record?  Should there be some division of them amongst a couple of tables to make a proper RDBMS?

Link to comment
Share on other sites

15 hours ago, Barand said:

Are you sure the two code samples you posted are related?

The first processes 21 $_POST inputs, the second has 2 input fields.

<?php

session_start();

if (!isset($_SESSION['username'])) { 
	$_SESSION['msg'] = "You have to log in first"; 
	header('location: login.php'); 
} 


if (isset($_GET['logout'])) { 
	session_destroy(); 
	unset($_SESSION['username']); 
	header("location: login.php"); 
} 
?>

<?php


$host="localhost";
$username="root";
$pass="";
$db="registration";

$conn=mysqli_connect($host,$username,$pass,$db);
if(!$conn){
	die("Database connection error");
}

// insert query for register page
if(isset($_POST['ronel'])){
     $image = $_FILES['file']['name'];
  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["file"]["name"]);

    // Select file type
  $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

  // Valid file extensions
  $extensions_arr = array("jpg","jpeg","png","gif","pdf");

  // Check extension
  if( in_array($imageFileType,$extensions_arr) )
  {

$details=$_POST['details'];
$location=$_POST['location'];
$checkbox=$_POST['checkbox'];
$injured=$_POST['injured'];    
$agegender=$_POST['agegender'];   
$contact=$_POST['contact'];
$empid=$_POST['empid'];
$dept=$_POST['dept'];    
$organization=$_POST['organization'];     
$summary=$_POST['summary']; 
$name=$_POST['name'];
$outcome=$_POST['outcome'];    
$cause=$_POST['cause'];     
$action=$_POST['action'];
$reportedname=$_POST['reportedname'];
$position=$_POST['position'];    
$organisation=$_POST['organisation'];
$reportedcontact=$_POST['reportedcontact'];
$reporteddept=$_POST['reporteddept'];    
$status="Pending";
$comment=$_POST['comment'];
	
	
	 $query="INSERT INTO `proposals` (`details`,`location`,`date`,`time`,`checkbox`,`injured`,`agegender`,`contact`,`empid`,`dept`  ,`organization`,`summary`,`image`,`outcome`,`cause`,`action`,`reportedname`,`position`,`organisation`,`reportedcontact`,`reporteddept`,`status`,`comment`) VALUES ('$details','$location', current_timestamp(),current_timestamp(),'$checkbox','$injured','$agegender','$contact','$empid','$dept' ,'$organization','$summary','$name','$outcome','$cause','$action','$reportedname','$position','$organisation','$reportedcontact','$reporteddept','$status','$comment')";
	
	$res=mysqli_query($conn,$query);
	if($res){
		$_SESSION['success']="Not Inserted successfully!";
		header('Location:');
	}else{
		echo "<script>alert('Proposal not applied!');</script>";
	}
      // Upload file
     move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$image);
	}
}
date_default_timezone_set("Asia/Kolkata");
?>
<html>

<body style="background-color:F8FFFF;">
    <table style="position:absolute; bottom:83.2%; left:226px; width:66.3%; height:16.5%;">
        <tr>
            <th style="font-weight:600; font-size:18px">ONGC TRIPURA POWER COMPANY LIMITED</th>
        </tr>
        <tr>
            <td style="font-weight:600; font-size:18px">INITIAL EHS INCIDENT REPORT FORM OTPC/SOP/EHS/001/R1</td>
        </tr>
    </table>
    <div class="container">
        <img src="otpc1" alt="Paris" style="width:130px; height:75px; border: 1px solid #ddd;
  border-radius: 4px; position: relative;
  left: 85%; bottom:17px;
  padding: 10px;">

        <img src="otpc" alt="Paris" style="width:170px; height:75px; border: 1px solid #ddd;
  border-radius: 4px; position: relative;
  right:140px; bottom:16px;
  padding: 15px;">
    </div>
    <div class="col-xs-6 col-xs-push-3 well">
        <form class="form-horizontal" method="post" action="" enctype="multipart/form-data">
            <input type="hidden" name="ronel" value="">
            <fieldset style="position:relative; width:95%; top:0px; left:10px;">
                <legend>NEW PROPOSAL NO.</legend>
                <!----left box----------->
                <!----right box----------->
                <div class="col-xs-9">
                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="font-size:16px;color:black;"><b>Details of Incident:</b></label><br>
                        <div class="col-lg-9">

                            <textarea rows="5" cols="110" name="details" class="form-control" maxlength="1000" onkeyup="textCounter(this,'counter4',1000);"></textarea><br><input style="position:absolute; left:75.3%; top:102px;" disabled maxlength="3" size="3" value="1000" id="counter4">
                        </div>
                    </div><br>
                    <fieldset style="position:relative; width:73.05%; height:50px; border:1px solid gray;">
                        <div class="form-group" style="position:relative; left:80px; top:-8px;">
                            <label style="position:relative; right:80px; top:25px;" for="inputEmail" class="col-lg-3"><b>Location:</b></label>
                            <div class="col-lg-9">
                                <textarea name="location" class="form-control" rows="2" cols="56" maxlength="100" onkeyup="textCounter(this,'counter5',100);"></textarea><input style="position:absolute; left:54%; bottom:0px;" disabled maxlength="3" size="2" value="100" id="counter5">
                            </div>
                        </div>

                        <div class="form-group" style="position:absolute; left:638px; bottom:22px;">
                            <label style="position:relative; right:58px; top:20px;" for="inputEmail" class="col-lg-3"><b>Date:</b></label>
                            <div class="col-lg-9">
                                <textarea style="position:relative; top:0px;right:19%;" cols="10" rows="1" disabled><?php echo date('Y-m-d'); ?>
                            </textarea>
                            </div>
                            </div>
                            <div class="form-group" style="position:relative; left:780px; bottom:54px;">
                                <label style="position:relative; right:67px; top:20px;" for="inputEmail" class="col-lg-3"><b>Time:</b></label>
                                <div class="col-lg-9">
                                    <textarea style="position:relative; top:0px;right:3%;" cols="10" rows="1" disabled><?php
date_default_timezone_set("Asia/Kolkata");
echo date("h:i:sa");
?>
                            </textarea>
                                </div>
                            </div>
                    </fieldset>
                    <br>
                    <div class="form-group">

                        <label for="inputEmail" class="col-lg-3" style="font-size:16px;color:black;"><b>Classification(Tick the appropriate one, Leave blank if you find it difficulty):</b></label>
                        <div class="col-lg-9" name="checkbox">
                            <table>
                                <tr>
                                    <th>Nearmisscase</th>
                                    <th>First Aid Case</th>
                                    <th>Lost Time Injury</th>
                                    <th>Fatal</th>
                                    <th>Fire</th>
                                    <th>Emission/Discharge/Spill/Leak(Abnormal)</th>
                                    <th>Property Damage</th>
                                    <th>HIPO</th>
                                </tr>
                                <tr>
                                    <td><input type="checkbox" value="Nearmiss case" name="checkbox"></td>
                                    <td><input type="checkbox" value="First Aid Case" name="checkbox"></td>
                                    <td><input type="checkbox" value="Lost Time Injury" name="checkbox"></td>
                                    <td><input type="checkbox" value="Fatal" name="checkbox"></td>
                                    <td><input type="checkbox" value="Fire" name="checkbox"></td>
                                    <td><input type="checkbox" value="Emission/Discharge/Spill/Leak(Abnormal)" name="checkbox"></td>
                                    <td><input type="checkbox" value="Property Damage" name="checkbox"></td>
                                    <td><input type="checkbox" value="HIPO" name="checkbox"></td>
                                </tr>
                            </table>
                            <style>
                                table {
                                    font-family: sans-serif;
                                    border-collapse: collapse;
                                    width: 75.5%;
                                    font-size: 15px;
                                }

                                td,
                                th {
                                    border: 1px solid black;
                                    text-align: center;
                                    padding: 2px;
                                    font-weight: normal;
                                }

                            </style>
                        </div>
                    </div>
                    <br>

                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="font-size:17px;color:black;"><b>Details of Injured :</b></label>
                        <br>
                        <label for="inputEmail" class="col-lg-3"><b>Name:</b></label>
                        <div class="col-lg-9">
                            <textarea name="injured" cols="25" rows="1" class="form-control"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:20%; bottom:39px;">
                        <label for="inputEmail" class="col-lg-3"><b>Age/Gender:</b></label>
                        <div class="col-lg-9">
                            <textarea name="agegender" class="form-control" cols="3" rows="1"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:29%; bottom:78px;">
                        <label for="inputEmail" class="col-lg-3"><b>Contact:</b></label>
                        <div class="col-lg-9">
                            <textarea name="contact" class="form-control" cols="9" rows="1"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="position:absolute; left:1.2%; top:26.5%;">
                        <label for="inputEmail" class="col-lg-3"><b>Employee ID:</b></label>
                        <div class="col-lg-9">
                            <textarea name="empid" class="form-control" cols="3" rows="1"></textarea>
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:39%; bottom:117px;">
                        <label for="inputEmail" class="col-lg-3"><b>Organization:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="organization" class="form-control">
                        </div>
                    </div>

                    <div class="form-group" style="position:absolute; left:55%; bottom:74%;">
                        <label for="inputEmail" class="col-lg-3"><b>Department:</b></label>
                        <div class="col-lg-9">
                            <select name="dept" class="form-control">
                                <option value="">---------Select Dept--------</option>
                                <option value="MMD">MMD</option>
                                <option value="O&M">O&M</option>
                                <option value="Civil">Civil</option>
                                <option value="C&M">C&M</option>
                                <option value="Logistics">Logistics</option>
                                <option value="HR & ADMIN">HR & ADMIN</option>
                                <option value="Fire & Safety">Fire & Safety</option>
                                <option value="IT & MIS">IT & MIS</option>
                                <option value="F&M">F&M</option>
                                <option value="EMD">EMD</option>
                                <option value="C&I">C&I</option>
                                <option value="STORE">STORE</option>
                                <option value="EHS">EHS</option>
                                <option value="Tech Cell">Tech Cell</option>
                                <option value="Operation">Operation</option>
                                <option value="Chemist">Chemist</option>
                            </select>
                        </div>
                    </div><br><br>

                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:90px; font-size:17px;color:black;"><b>Summary Of Incident:</b></label>
                        <div class="col-lg-9" style="position:relative; bottom:90px;">
                            <textarea rows="8" cols="110" name="summary" class="form-control" maxlength="1000" onkeyup="textCounter(this,'counter3',1000);"></textarea><br><input style="position:absolute; left:72.5%;" disabled maxlength="3" size="3" value="1000" id="counter3">
                        </div>
                    </div>


                    <div class="form-group">
                        <label style="position:absolute; left:63%; top:425px;" for="inputEmail" class="col-lg-3"><b>Upload Images Here :</b></label><br><br>
                        <div class="col-lg-9">
                            
                            <input style="position:absolute; left:78%; top:420px;" type="file" name="file" enctype="multipart/form-data" class="form-control" name="incident_reference" onchange="document.getElementById('inc_ref').src = window.URL.createObjectURL(this.files[0]); document.getElementById('inc_ref').className +='_active'; document.getElementById('inc_ref_span').className += '_hidden'">
                        </div><iframe id="inc_ref" class="form-group" width="220px" height="130px" style="position:absolute; left:78%; top:32%;"></iframe>
                    </div>


                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:110px; font-size:17px;color:black;"><b>Potential outcome(Incase of Near Miss case or Potential Incident only):</b></label>
                        <div class="col-lg-9" style="position:relative; bottom:110px;">

                            <textarea rows="8" cols="110" name="outcome" class="form-control" maxlength="1000" maxlength="1000" onkeyup="textCounter(this,'counter2',1000);"></textarea><br><input style="position:absolute; left:76%; top:102px;" disabled maxlength="3" size="3" value="1000" id="counter2">
                        </div>
                    </div><br>
                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:110px; font-size:17px;color:black;"><b>Likely Cause:</b></label>
                        <div class="col-lg-9" style="position:relative; bottom:110px;">

                            <textarea rows="8" cols="110" name="cause" class="form-control" maxlength="1000" maxlength="1000" onkeyup="textCounter(this,'counter1',1000);"></textarea><br><input style="position:absolute; left:76%; top:102px;" disabled maxlength="3" size="3" value="1000" id="counter1">
                        </div>
                    </div><br>
                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:110px; font-size:17px;color:black;"><b>Immediate Action:</b></label>
                        <div class="col-lg-9" style="position:relative; bottom:110px;">

                            <textarea rows="8" cols="110" name="action" class="form-control" maxlength="1000" onkeyup="textCounter(this,'counter',1000);"></textarea><br><input style="position:absolute; left:76%; top:102px;" disabled maxlength="3" size="3" value="1000" id="counter">
                        </div>
                    </div><br>
                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:110px; font-size:17px;color:black;"><b>Incident reported by:</b></label>
                        <label for="inputEmail" class="col-lg-3" style="position:relative; bottom:80px; right:160px;"><b>Name:</b></label>
                        <div class="col-lg-9">
                            <input style="position:absolute; bottom:230px; left:10px;" type="text" name="reportedname" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:15%; bottom:100px;">
                        <label for="inputEmail" class="col-lg-3"><b>Position:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="position" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:31%; bottom:141px;">
                        <label for="inputEmail" class="col-lg-3"><b>Organisation:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="organisation" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:0%; bottom:130px;">
                        <label for="inputEmail" class="col-lg-3"><b>Contact:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="reportedcontact" class="form-control">
                        </div>
                    </div>
                    <div class="form-group" style="position:relative; left:175px; bottom:171px;">
                        <label for="inputEmail" class="col-lg-3"><b>Department:</b></label>
                        <div class="col-lg-9">
                            <input type="text" name="reporteddept" class="form-control">
                        </div>
                    </div>
                    <div class="col-lg-9">
                        <input type="hidden" name="status" class="form-control">
                    </div>

                    <div class="form-group">
                        <label for="inputEmail" class="col-lg-3"><b></b></label>
                        <div class="col-lg-9">
                            <input type="hidden" name="comment" class="form-control">
                        </div>
                    </div>
                </div>
            </fieldset>
            <br>
            <script type="text/javascript">
                function confSubmit(form) {
                    if (confirm("Are you sure you want to submit the form?")) {
                        form.submit();
                    } else {
                        alert("You decided to not submit the form!");
                    }
                }

            </script>
            <script>
                function textCounter(field, field2, maxlimit) {
                    var countfield = document.getElementById(field2);
                    if (field.value.length > maxlimit) {
                        field.value = field.value.substring(0, maxlimit);
                        return false;
                    } else {
                        countfield.value = maxlimit - field.value.length;
                    }
                }

            </script>
            <div class="form-group" style="position:absolute; left:33px;">
                <div class="col-lg-12">
                    <button class="button1" type="reset" class="btn btn-default">Cancel</button>
                    <button class="button1" type="submit" class="btn btn-primary" onClick="confSubmit(this.form);">Submit</button>
                    <style>
                        .button1:hover {
                            background-color: #555555;
                            color: aqua;
                            border: 3px solid grey;

                        }

                    </style>
                </div>
            </div>

        </form>
    </div>
</body>

</html>

Dear Sir this is the whole post and the full input fields. Kindly show some light.

Error Message: Notice: Undefined index: name in /opt/lampp/htdocs/create-nearmiss.php on line 57

Link to comment
Share on other sites

Here's a section of your form

image.png.5e8111f9d2292ea1392434ae4b12f778.png

Becacause you have used checkboxes you are inviting the user to select one or more applicable options. In the example shown, a reasonable combination of "Fatal / Fire / Property Damage".

However, because of the way you have named them in your code (all same name "checkbox"), your processing will only receive the last one. In this example that is the least important.

To receive all the selected options, append "[]" to the checkbox names.

                                <tr>
                                    <td><input type="checkbox" value="Nearmiss case" name="checkbox[]"></td>
                                    <td><input type="checkbox" value="First Aid Case" name="checkbox[]"></td>
                                    <td><input type="checkbox" value="Lost Time Injury" name="checkbox[]"></td>
                                    <td><input type="checkbox" value="Fatal" name="checkbox[]"></td>
                                    <td><input type="checkbox" value="Fire" name="checkbox[]"></td>
                                    <td><input type="checkbox" value="Emission/Discharge/Spill/Leak(Abnormal)" name="checkbox[]"></td>
                                    <td><input type="checkbox" value="Property Damage" name="checkbox[]"></td>
                                    <td><input type="checkbox" value="HIPO" name="checkbox[]"></td>
                                </tr>

This will then cause them to be POSTed as an array

$_POST = Array
(
    [checkbox] => Array
        (
            [0] => Fatal
            [1] => Fire
            [2] => Property Damage
        )

    [btnSubmit] => Submit
)

If you want the user to select only one, use radio buttons.

Just my 0.02 worth.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.