Home Ask Login Register

Developers Planet

Your answer is one click away!

sarathi February 2016

insert query working, update not working?

This insert program work perfect But same as update not working any body help to give correct code

Insert.php

 <html>
 <form role="form" action="" method="post">
 <input type="text" name="fname" placeholder="First name..." id="fname">
 <input list="dept" placeholder="Choose Dept"  name="dept" required/>
 <datalist id="dept">
 <option>
 <?php 
  include 'dblayer.php';
  $query = mysqli_query($mysqli,"SELECT department FROM department");
  while($row=mysqli_fetch_array($query))
    {
       echo "<option value='". $row['department']."'>".$row['department'] .'</option>';
    }
  ?>
  </option>
  </datalist>

<?php
include "dblayer.php";
if(isset($_POST["submit"]))
{
    $fname  = $_POST["fname"];
    $dept   = $_POST["dept"];
    $result = mysqli_query($mysqli, "INSERT INTO employee(fname,department)
            SELECT '$fname', dept_id  FROM department WHERE department = '$dept' LIMIT 1");

    if($result)
        {
            echo "<script>alert('New employee register successfully!')</script>";
            echo "<script>window.open('home.php','_self')</script>";
        }
    else 
        {
            echo "<script>alert('something went wrong!')</script>";
        }
    }
?>

Update.php

This page join query some problem. I think department values get but not save record. So join query correct format answer give anybody

 <?php
 include 'dblayer.php';
 $action = isset( $_POST['action'] ) ? $_POST['action'] : "";
 if($action == "update")
  { 
    $query = "UPDATE employee SET fname = '".$mysqli->real_escape_string($_POST['fname'])."', department='".$mysqli->real_escape_string($_POST['department'])."' where id='".$mysqli->real_escape_string($_REQUEST['id'])."'";
   if( $mysqli->query($query) ) {
        echo "<script>alert('updated!')</sc        

Answers


AnkiiG February 2016

Change

<input list="dept" placeholder="Choose Dept"  name="dept" required/>

to

<input list="dept" placeholder="Choose Dept"  name="department" required/>

And in your insert script: $dept = $_POST["department"];

Post Status

Asked in February 2016
Viewed 3,536 times
Voted 5
Answered 1 times

Search




Leave an answer


Quote of the day: live life