Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Insert data in to the textbox in PHP Reply To: Insert data in to the textbox in PHP

#169234
dnyanesh
Participant

Thanks you traq.
I need these SQL statements in PHP only. i had issue related to insert data in to database and again show it in table. i did it with the help of your support. i need your support for update and delete coding . i want to update the record in the table from web page . means i will click on update in from of row and it should redirect to update page and required field should get update .

here is the code for update

  <!DOCTYPE html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="description" content="" />
  <meta name="keywords" content="" />
  <meta name="author" content="" />
  <link rel="stylesheet" type="text/css" href="style.css"  />
  <title>First web page</title> 
  </head>

  <body>

  <div>

  &lt;?php include('includes/header.php'); ?&gt;

  &lt;?php include('includes/nav.php'); ?&gt;

  <div>
  &lt;h3&gt;Paragraph Element&lt;/h3&gt;
  &lt;?php
   function renderForm($id, $name, $email, $date,$mobile,$gender,$lang,$error)
   {
   ?&gt;
          &lt;form action=""  method="post" enctype="multipart/form-data"&gt;
          &lt;table class="tbldata" align="center" border="1px solid black"  style="background-color:!important" &gt;
            &lt;tr id="bgcolor"&gt;
            &lt;td colspan="2" align="center"  style="background-color: #CF3"&gt;Submit Employee Information&lt;/td&gt;
            &lt;/tr&gt;
            &lt;tr  id="bgcolor"&gt;
              &lt;td    &gt;Name&lt;/td&gt;
              &lt;td class="back"&gt;&lt;input  type="text" name="name"  value="&lt;?php echo $name ?&gt;"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr  id="bgcolor"&gt;
              &lt;td&gt;E-mail&lt;/td&gt;
              &lt;td class="back"&gt;&lt;input  type="text" name="email"  value="&lt;?php echo $email ?&gt;" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr  id="bgcolor"&gt;
              &lt;td&gt;DOB&lt;/td&gt;
              &lt;td class="back"&gt;&lt;input type="date"  name="date"  value="&lt;?php echo $date ?&gt;" /&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr  id="bgcolor"&gt;
              &lt;td&gt;Mobile_No&lt;/td&gt;
              &lt;td class="back"&gt;&lt;input type="text" name="mobile"  value="&lt;?php echo $mobile ?&gt;"/&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr  id="bgcolor"&gt;
              &lt;td&gt;Gender&lt;/td&gt;
              &lt;td class="back"&gt;&lt;input id="bgcolor" type="radio" name="gender"   value="&lt;?php echo $gender ?&gt;" checked="checked"&gt;M&lt;/input&gt; &lt;input type="radio" name="gender" value="&lt;?php echo $gender ?&gt;"&gt;F&lt;/input&gt;&lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr  id="bgcolor"&gt;
              &lt;td&gt;Language&lt;/td&gt;
              &lt;td class="back"&gt;&lt;select name="lang"&gt;&lt;option  value="&lt;?php echo $lang ?&gt;" &gt;English&lt;/option&gt;
              &lt;option  value="&lt;?php echo $data-&gt;lang ?&gt;"&gt;Marathi&lt;/option&gt;
              &lt;option  value="&lt;?php echo $data-&gt;lang ?&gt;"&gt;Hindi&lt;/option&gt; &lt;/select&gt;&lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr id="bgcolor"&gt;
          &lt;td&gt;Photo&lt;/td&gt;
          &lt;td class="back"&gt;&lt;input type="file" name="file"  value="&lt;?php echo $file ?&gt;" /&gt;&lt;/td&gt;    &lt;/tr&gt;
            &lt;tr&gt;
              &lt;td colspan="7"&gt;&lt;input type="submit" value="Submit" name="update" /&gt; <a href="dashboard.php"> &lt;input type="button"  value="Back"/&gt;</a> &lt;/td&gt; 
              &lt;/tr&gt;
          &lt;/table&gt;
          &lt;/form&gt;
  &lt;?php } ?&gt;

  </div>
  &lt;?php 
  $con = mysql_connect("localhost","root","");
  mysql_select_db("firstweb");



       if (isset($_POST['update']))
        { 

                 if (is_numeric($_POST['id']))
                 {

                       $id = $_POST['id'];
                       $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
                       $email = mysql_real_escape_string(htmlspecialchars($_POST['email']));
                       $date = mysql_real_escape_string(htmlspecialchars($_POST['date']));
                       $mobile = mysql_real_escape_string(htmlspecialchars($_POST['mobile']));
                       $gender = mysql_real_escape_string(htmlspecialchars($_POST['gender']));
                       $lang = mysql_real_escape_string(htmlspecialchars($_POST['lang']));


                        if ($name == '' || $email == '' || date == '' || mobile == '' )
                       {

                           $error = 'ERROR: Please fill in all required fields!';

                           renderForm($id, $name, $email, $date,$mobile,$gender,$lang,$error);
                       }
                       else
                       {

                               mysql_query("UPDATE emp_data SET name='$name',email='$email',date='$date',mobile='$mobile',gender='$gender',lang='$lang',image='$image' where id='$id'")
                               or die(mysql_error());
                               header("Location: dashboard.php"); 
                       }
                 }
                 else
                 {
                       echo 'Error!';
                 }
            }
            else
            {
                 if (isset($_GET['id']) &amp;&amp; is_numeric($_GET['id']) &amp;&amp; $_GET['id'] &gt; 0)
                 {

                       $id = $_GET['id'];
                       $result = mysql_query("SELECT * FROM emp_data WHERE id=$id")
                       or die(mysql_error()); 
                       $row = mysql_fetch_array($result);

                       if($row)
                       {


                            $name = $row['name'];
                            $email = $row['email'];
                            $date = $row['date'];
                            $mobile = $row['mobile'];
                            $gender = $row['gender'];
                            $lang = $row['lang'];
                       renderForm($id, $name, $email, $date,$mobile,$gender,$lang,'');
                       }
                       else
                       {
                       echo "No results!";
                       }
                 }
                else
                 {
                      echo 'Error!';
                 }
        }
 ?&gt;



  &lt;?php include('includes/sidebar.php'); ?&gt;

  &lt;?php include('includes/footer.php'); ?&gt;

  </div>

  &lt;/body&gt;

  &lt;/html&gt;