Advanced Search

Results 1 to 5 of 5

Thread: Save Button

  1. #1
    Join Date
    Jul 2010
    Posts
    222
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Save Button

    Good day!

    I encountered Problem in my code. When i click the save button the data was not save in my database and also it did not appear automatically on my page3.php form.

    Here is my page3.php code:
    PHP Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    <script type="text/JavaScript">
    function confirmDelete(){
        var agree = confirm("Are you sure you want to delete this file?");
        if(agree){
            // direct browser to delete.php
            window.location = "./delete.php";
        } else {
            // do nothing and return false
            return false ;
        }
    }
    </script>
    </head>
     <body>
     <form name="machine1" action="page3.php" method="post">
     <table border="1">
     <tr>
     <td>Emp ID</td>
     <td>Last Name</td>
     <td>First Name</td>
     <td>Birthday</td>
     <td>Option</td>
     </tr>
     
     <?php 
    mysql_connect
    ("localhost""root""") or die(mysql_error()); 
    mysql_select_db("db_machine1") or die(mysql_error()); 
     
     
    $data_p mysql_query("SELECT * FROM tbl_machine1") or die(mysql_error());
    while(
    $info mysql_fetch_array$data_p ))
    {
        
    $emp_id $info['Emp_ID'];
        
    $lname $info['Last_Name'];
        
    $fname $info['First_Name'];
        
    $bday $info['Birthday'];
        
    ?>
        <tr>
        <td><?php echo $emp_id;?> </td>
        <td><?php echo $lname;?> </td>
        <td><?php echo $fname;?> </td>
        <td><?php echo $bday;?> </td>
           <td><a href = 'edit.php?id=$emp_id'>Edit</a> <a href='delete.php?id=$emp_id' onClick='confirmDelete();'>Delete</a></td>
        </tr>
    <?php
    }
    ?>


    </table>
    <A HREF="javascript:void(0)" onclick="window.open('add.php','welcome','width=300,height=200')">
    <input type="button" name="add" value="ADD"> </A>
     </body>
     </html>
    and here is my add.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <form method="post" action="page3.php" name="add">
    Last Name:&nbsp;<input type="text" name="Last_Name" id="Last_Name"><br/><br/>
    First Name:&nbsp;<input type="text" name="First_Name" id="First_Name"><br/><br/>
    Birthday:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="Birthday" id="Birthday"><br/><br/>
    <input type="submit" name="save" value="SAVE">

    <?php
    error_reporting
    (0);
    mysql_connect("localhost""root""") or die(mysql_error()); 
    mysql_select_db("db_machine1") or die(mysql_error()); 

    $Lname=$_POST['Last_Name'];
    $Fname=$_POST['First_Name'];
    $bday=$_POST['Birthday'];

    $query "INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) VALUES ('$Lname','$Fname','$bday')";
    $result=mysql_query($query);

    //header ('location:page3.php');

    ?>
    </form>
    </body>
    </html>
    I put error_reporting(0); in my add.php because when theres no error_reporting(0) theres a notice appear that undefined index $Lname, $Fname, $bday

    In that code happened is it add a emp_id and the bday was 0000-00-00.

    I hope somebody can help me..

    Thank you

  2. #2
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    227
    Thanks
    1
    Thanked 17 Times in 17 Posts

    Default

    You really need to start wrapping your code in if() to check if a variable is set, then you can get rid of the error_reporting, the undefined index only happens on a local server not on paid hosting servers cause they suppress that level of reporting.
    This is what I mean
    PHP Code:
    if(isset($_POST))
    {
      
    $Lname=$_POST['Last_Name'];
    $Fname=$_POST['First_Name'];
    $bday=$_POST['Birthday'];

    $query "INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) VALUES ('$Lname','$Fname','$bday')";
    $result=mysql_query($query);

    //header ('location:page3.php');

    This can be anything that you want to see if has been given a value to the server
    PHP Code:
    if(isset($_POST)) 
    Or
    PHP Code:
    if(isset($_POST['submit'])) 
    Or
    PHP Code:
    if(isset($any_var_you_want)) 
    As for the other issue, put
    PHP Code:
    echo mysql_error(); 
    after the failing query and it will tell you why.

  3. #3
    Join Date
    Jul 2010
    Posts
    222
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    I tried this code:

    PHP Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>

    <script language="JavaScript">
        function CloseChildWindow()
        {
            window.opener.location.href="page3.php";
            self.close();
        }
    </script>
    </head>

    <body>
    <form method="post" action="add.php" name="add">
    Last Name:&nbsp;<input type="text" name="Last_Name" id="Last_Name"><br/><br/>
    First Name:&nbsp;<input type="text" name="First_Name" id="First_Name"><br/><br/>
    Birthday:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="Birthday" id="Birthday"><br/><br/>
    <input type="submit" name="save" value="SAVE" onClick="CloseChildWindow();">
    <?php
    //error_reporting(0);
    $Lname "";
    $Fname"";
    $bday ""


    mysql_connect("localhost""root""") or die(mysql_error()); 
    mysql_select_db("db_machine1") or die(mysql_error()); 

    if(isset(
    $_POST['save'])){
    $Lname=$_POST['Last_Name'];
    $Fname=$_POST['First_Name'];
    $bday=$_POST['Birthday'];
    //$bday = date("d m Y", strtotime($info['Birthday'])); 



    $date date('Y-m-d'strtotime($bday));
    //$query = "INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) VALUES ('$Lname','$Fname','$bday')";
    //$result=mysql_query($query);

    $sql mysql_query("INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) 
    VALUES('"
    .$Lname."', '".$Fname."', '".$date."')");
    }

    //header ('location:page3.php');

    ?>
    </form>
    </body>
    </html>
    Before it work but now it did not work...I don't know why...

  4. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,496
    Thanks
    16
    Thanked 223 Times in 222 Posts
    Blog Entries
    1

    Default

    If it worked before your changes, revert back to the original code and then gradually introduce your changes until the point where it doesnt - I find this helps with troubleshooting.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) |
    The only limit to creativity is imagination: JemCon.org

  5. #5
    Join Date
    Jul 2010
    Posts
    222
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default

    I resolved my problem by this code:
    PHP Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>

    <link rel="stylesheet" href="ui.all.css" type="text/css" media="screen" />

    <script language="JavaScript">
        function CloseChildWindow()
        {
            window.opener.location.href="machine1.php";
            self.close();
        }
    </script>
    </head>

    <body>
    <form method="post" action="add.php" name="add">
    Last Name:&nbsp;<input type="text" name="Last_Name" id="Last_Name"><br/><br/>
    First Name:&nbsp;<input type="text" name="First_Name" id="First_Name"><br/><br/>
    Birthday:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" name="date" id="date"><br/><br/>
    <input type="submit" name="save" value="SAVE" onClick="CloseChildWindow();">
    <?php
    //error_reporting(0);
    $Lname "";
    $Fname"";
    $bday ""

    include 
    'connection.php';

    if(isset(
    $_POST['save'])){
    $Lname=$_POST['Last_Name'];
    $Fname=$_POST['First_Name'];
    $bday=$_POST['date'];



    $date date('Y-m-d'strtotime($bday));

    $Lname mysql_real_escape_string($Lname);
    $Fname mysql_real_escape_string($Fname);
    $date mysql_real_escape_string($date);

    $sql mysql_query("INSERT INTO tbl_machine1 (Last_Name, First_Name, Birthday) 
    VALUES('"
    .$Lname."', '".$Fname."', '".$date."')");

    }

    ?>
    </form>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js"></script>

    <script type="text/javascript">

    $(document).ready(function(){

    $("#date").datepicker({ showOn: 'button', buttonText: "Select your Birthday" });

    });

    </script>
    </body>
    </html>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •