Scritpt is Not obtaining the ID.

Discussion in 'PHP' started by Matt Ridge, Feb 21, 2012.

  1. #1
    Here is my code, it is updated now it has been broken between html and php, honestly I think it looks ugly, but people suggest I learn how to code this way. In doing so I think I broke the ability to obtain the id. Can someone here help me fix that last bit?

    Here is the new code:

    
    <?php
    require_once('tb/connectvars.php');
    echo '<div id="postwrap">';
    ?>
    
    
    <!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" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>PDI NCMR - Edit</title>
      <link rel="stylesheet" type="text/css" href="tb/postie.css" />
    </head>
    <body>
       <div id="logo">
        <img src="../images/PDI_Logo_2.1.gif" alt="PDI Logo" />
    </div>
    
    
    <?php
      if (isset($_POST['submit'])) {
    // Connect to the database
        $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    ;    
    // Enter data into the database
        $id = mysqli_real_escape_string($dbc, trim($_GET['id']));
        $ab = mysqli_real_escape_string($dbc, trim($_POST['ab']));
        $date = mysqli_real_escape_string($dbc, trim(date('Y-m-d',strtotime ($_POST['date']))));
        $part = mysqli_real_escape_string($dbc, trim($_POST['part']));
        $rev = mysqli_real_escape_string($dbc, trim($_POST['rev']));
        $partdesc = mysqli_real_escape_string($dbc, trim($_POST['partdesc']));
        $ncmrqty = mysqli_real_escape_string($dbc, trim($_POST['ncmrqty']));
        $comp = mysqli_real_escape_string($dbc, trim($_POST['comp']));
        $ncmrid = mysqli_real_escape_string($dbc, trim($_POST['ncmrid']));
        $rma = mysqli_real_escape_string($dbc, trim($_POST['rma']));
        $jno = mysqli_real_escape_string($dbc, trim($_POST['jno']));
        $fdt = mysqli_real_escape_string($dbc, trim($_POST['fdt']));
        $cof = mysqli_real_escape_string($dbc, trim($_POST['cof']));
        $fab1= mysqli_real_escape_string($dbc, trim($_POST['fab1']));
        $fab2= mysqli_real_escape_string($dbc, trim($_POST['fab2']));
        $fab3= mysqli_real_escape_string($dbc, trim($_POST['fab3']));
        $non= mysqli_real_escape_string($dbc, trim($_POST['non']));
        $dis= mysqli_real_escape_string($dbc, trim($_POST['dis']));
        $comm= mysqli_real_escape_string($dbc, trim($_POST['comm']));
        $caad= mysqli_real_escape_string($dbc, trim($_POST['caad']));
        $po= mysqli_real_escape_string($dbc, trim($_POST['po']));
        $pod = mysqli_real_escape_string($dbc, trim(date('Y-m-d',strtotime($_POST['pod']))));
        $dri = mysqli_real_escape_string($dbc, trim(date('Y-m-d',strtotime($_POST['dri']))));
    
    
        $query = "UPDATE ncmr SET ab = '$ab', date = '$date', part = '$part', rev = '$rev' , partdesc = '$partdesc' , ncmrqty = '$ncmrqty' , comp = '$comp' , ncmrid = '$ncmrid' , rma = '$rma' , jno = '$jno' , fdt = '$fdt' , cof = '$cof' , fab1 = '$fab1' , fab2 = '$fab2' , fab3 = fab3' , non = '$non' , dis = '$dis' , comm = '$comm' , caad = '$caad' , po = '$po' , pod = '$pod' , dri = '$dri'  WHERE id = '$id'";
    
    
    // echo your raw query and look for obvious errors
         echo "Query is : " . $query . "<br />";
      
    // and at least use a basic mechanism to trap possibles errors
           mysqli_query($dbc, $query)  or die('Query Error : ' . mysqli_error($dbc));
    
    
    // Clear the data to clear the form
        $id = "";
        $ab = "";
        $date = "";
        $part = "";
        $rev = "";
        $partdesc = "";
        $ncmrqty = "";
        $comp = "";
        $ncmrid = "";
        $rma = "";
        $jno = "";
        $fdt = "";
        $cof = "";
        $fab1= "";
        $fab2= "";
        $fab3= "";
        $non= "";
        $dis= "";
        $comm= "";
        $caad= "";
        $po= "";
        $pod = "";
        $dri = "";
    
    
    // Confirm success with the user
            echo '<p>If you wish to edit more NCMRs, please <a href="list.php">go to the admin page!</a></p>';
    
    
                mysqli_close($dbc);
                }
        
      else {    
     
    // Connect to the database
      $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    // Grab the profile data from the database
     if (!isset($_GET['id'])) {
        $query = "SELECT * FROM ncmr WHERE id = '$id'";
      }
      else {
        $query = "SELECT * FROM ncmr WHERE id = '" . $_GET['id'] . "'";
      }
      $data = mysqli_query($dbc, $query);
    
    
      if (mysqli_num_rows($data) == 1) { 
    // The user row was found so display the user data
        $row = mysqli_fetch_array($data);
        echo'<div id="title"><h3 id="NCMR2">Non-Conforming Materials Report (NCMR:&nbsp;&nbsp;' . $row['rma'] . ')</h3></div>';
        
        if (empty($row['ab'])) $row['ab'] == "Empty";
        if (empty($row['date'])) $row['date'] == "Empty";
        if (empty($row['part'])) $row['part'] == "Empty";
        if (empty($row['rev'])) $row['rev'] == "Empty";
        if (empty($row['partdesc'])) $row['partdesc'] = "Empty";
        if (empty($row['ncmrqty'])) $row['ncmrqty'] = "Empty";
        if (empty($row['comp'])) $row['comp'] == "Empty";
        if (empty($row['ncmrid'])) $row['ncmrid'] == "Empty";
        if (empty($row['rma'])) $row['rma'] == "Empty";
        if (empty($row['jno'])) $row['jno'] == "Empty";
        if (empty($row['non'])) $row['non'] == "Empty";
        if (empty($row['dis'])) $row['dis'] == "Empty";
        if (empty($row['comm'])) $row['comm'] == "Empty";
        if (empty($row['caad'])) $row['caad'] == "Empty";
        if (empty($row['po'])) $row['po'] =="Empty";
        if (empty($row['pod'])) $row['pod'] =="Empty";
        if (empty($row['dir'])) $row['dri'] =="Empty";
        
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset>
            <div id="box1">
                <div id="ab"><span class="b">Added By:&nbsp;&nbsp;</span><input type="text" name="ab" value="<?php echo $row['ab']?>" /></div>
                <div id="date"><span class="b">Date Filed:&nbsp;&nbsp;</span><input type="text" name="date" value="<?php echo $row['date']?>" /></div>
                <div id="part"><span class="b">Part Number:&nbsp;&nbsp;</span><input type="text" name="part" value="<?php echo $row['part']?>" /></div>
                <div id="rev"><span class="b">Part Revision:&nbsp;&nbsp;</span><input type="text" name="rev" value="<?php echo $row['rev']?>" /></div>
                <div id="partdesc"><span class="b">Part Description:&nbsp;&nbsp;</span><textarea rows="4" cols="22" value=><?php echo $row['partdesc']?></textarea></div>
                <div id="ncmrqty"><span class="b">NCMR Qty:&nbsp;&nbsp;</span><input type="text" name="ncmrqty" value="<?php echo $row['ncmrqty']?>"/></div>    
            </div>
            
            <div id="box2">
                <div id="comp"><span class="b">Company:&nbsp;&nbsp;</span><input type="text" name="comp" value="<?php echo $row['comp']?>" /></div>
                <div id="ncmrid"><span class="b">Customer NCMR ID:&nbsp;&nbsp;</span><input type="text" name="ncmrid" value="<?php echo $row['ncmrid']?>" /></div>
                <div id="rma"><span class="b">Internal RMA #:&nbsp;&nbsp;</span><input type="text" name="rma" value="<?php echo $row['rma']?>" /></div>
                <div id="jno"><span class="b">Job #:&nbsp;&nbsp;</span><input type="text" name="jno" value="<?php echo $row['jno']?>" /></div>
            </div>
            
            <div id="box3">
                <h2>Failure</h2>
                    <div id="cof"><span class="b">Class of Failure:&nbsp;&nbsp;</span><input type="text" name="cof" size="15" value="<?php echo $row['cof']?>" /></div>
                    <div id="fdt"><span class="b">Failure Due To:&nbsp;&nbsp;</span><input type="text" name="fdt" size="15" value="<?php echo $row['fdt']?>" /></div>
            </div>
    <?php
            echo '<div id="box4">';
                echo '<h2>Fabricators</h2>';
    if ($row['fab1']=="--None--")
    {
        echo'<div id="fab1">';
        $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
        $mysqli->select_db('user');
        $result = $mysqli->query("SELECT * FROM user"); 
        echo "<SELECT name='fab1'>\n";
        while($row = $result->fetch_assoc())
        {
            echo "<option value='{$row['userid']}'>{$row['user']}</option>\n";
        }
        echo "</select>\n";
        echo '</div>';
    }
    else
    {
        echo '<div id="fab1"><span class="b"></span><input type="text" name="fab1"  size="20" value="' . $row['fab1'] . '" /></div>';
    }
    
    
    if ($row['fab2']=="--None--")
    {
        echo'<div id="fab1">';
        $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
        $mysqli->select_db('user');
        $result = $mysqli->query("SELECT * FROM user"); 
        echo "<SELECT name='fab2'>\n";
        while($row = $result->fetch_assoc())
        {
            echo "<option value='{$row['userid']}'>{$row['user']}</option>\n";
        }
        echo "</select>\n";
        echo '</div>';
    }
    else
    {
        echo '<div id="fab2"><span class="b"></span><input type="text" name="fab2"  size="20" value="' . $row['fab2'] . '" /></div>';
    }
    if ($row['fab3']=="--None--")
    {
        echo'<div id="fab3">';
        $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); 
        $mysqli->select_db('user');
        $result = $mysqli->query("SELECT * FROM user"); 
        echo "<SELECT name='fab3'>\n";
        while($row = $result->fetch_assoc())
        {
            echo "<option value='{$row['userid']}'>{$row['user']}</option>\n";
        }
        echo "</select>\n";
        echo '</div>';
    }
    else
    {
        echo '<div id="fab3"><span class="b"></span><input type="text" name="fab3"  size="20" value="' . $row['fab3'] . '" /></div>';
    }
    echo '</div>';
    ?>
            <div id="box5">
                <div id="non"><span class="b">Nonconformity:&nbsp;&nbsp;</span><textarea rows="4" cols="105" value=><?php echo $row['non']?></textarea></div>
                <div id="dis"><span class="b">Disposition:&nbsp;&nbsp;</span><textarea rows="4" cols="105" value=><?php echo $row['dis']?></textarea></div>
                <div id="comm"><span class="b">Comments:&nbsp;&nbsp;</span><textarea rows="4" cols="105" value=><?php echo $row['comm']?></textarea></div>
                <div id="caad"><span class="b">Comments and/or Additional Details:&nbsp;&nbsp;</span><textarea rows="4" cols="105" value=><?php echo $row['caad']?></textarea></div>
            </div>
    
    
            <div id="podr">
            <div id="po"><span class="b">PO:&nbsp;&nbsp;</span><input type="text" name="po"  size="7" value="<?php echo $row['po']?>" /></div>
            <div id="pod"><span class="b">PO Date:&nbsp;&nbsp;</span><input type="text" name="pod"  size="7" value="<?php echo $row['pod']?>" /></div>
            <div id="dri"><span class="b">Date Received:&nbsp;&nbsp;</span><input type="text" name="dri"  size="7" value="<?php echo $row['dri']?>" /></div>
            </div>
                <div id="button2"><input type="submit" value="Submit Edits" name="submit" /></div>
            </div>
        </fieldset>
    </form>
    <?php
            }
        }
    
    
    
    
    echo '</div>';
    ?>
    </body>
    </html>
    
    Code (markup):

    I am getting an error, but also I am getting something later on in the script too:

    The first bit shows me that the ID is not pulling, the second I have no idea, I think this may dissapear after the ID is pulled correctly, but my luck it won't be. Because I use the same code in partdesc. So I don't know what's going on really.

    Can someone help please...
     
    Last edited: Feb 21, 2012
    Matt Ridge, Feb 21, 2012 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    LOL i know why you can't find the error, your code ISNT good readable!

    Please try to write your queries like this

    
        $query = "UPDATE ncmr 
    SET ab = '$ab', 
    date = '$date', 
    part = '$part', 
    rev = '$rev' , 
    partdesc = '$partdesc' , 
    ncmrqty = '$ncmrqty' , 
    comp = '$comp' , 
    ncmrid = '$ncmrid' , 
    rma = '$rma' , 
    jno = '$jno' , 
    fdt = '$fdt' , 
    cof = '$cof' , 
    fab1 = '$fab1' , 
    fab2 = '$fab2' , 
    fab3 = fab3' , 
    non = '$non' , 
    dis = '$dis' , 
    comm = '$comm' , 
    caad = '$caad' , 
    po = '$po' , 
    pod = '$pod' , 
    dri = '$dri'  
    WHERE id = '$id'";
    
    Code (markup):
    and please use mysql_real_escape_string to be shure that your data is database secure!
     
    EricBruggema, Feb 21, 2012 IP
  3. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #3
    fab3 = fab3' see the error? :D
     
    EricBruggema, Feb 21, 2012 IP
  4. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Gah! I thought I fixed that a long time ago. I really hate that.. .ok that part works, but now for some reason it is still not obtaining the ID...
     
    Matt Ridge, Feb 21, 2012 IP
  5. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #5
    I already did use prior mysql_real_escape_string to there.
     
    Matt Ridge, Feb 21, 2012 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #6
    Then where do you request the new ID?

    And for the record, this is just stupid
    
        $ab = mysqli_real_escape_string($dbc, trim($_POST['ab']));
        $part = mysqli_real_escape_string($dbc, trim($_POST['part']));
        $rev = mysqli_real_escape_string($dbc, trim($_POST['rev']));
        $partdesc = mysqli_real_escape_string($dbc, trim($_POST['partdesc']));
        $ncmrqty = mysqli_real_escape_string($dbc, trim($_POST['ncmrqty']));
        $comp = mysqli_real_escape_string($dbc, trim($_POST['comp']));
        $ncmrid = mysqli_real_escape_string($dbc, trim($_POST['ncmrid']));
        $rma = mysqli_real_escape_string($dbc, trim($_POST['rma']));
        $jno = mysqli_real_escape_string($dbc, trim($_POST['jno']));
        $fdt = mysqli_real_escape_string($dbc, trim($_POST['fdt']));
        $cof = mysqli_real_escape_string($dbc, trim($_POST['cof']));
        $fab1= mysqli_real_escape_string($dbc, trim($_POST['fab1']));
        $fab2= mysqli_real_escape_string($dbc, trim($_POST['fab2']));
        $fab3= mysqli_real_escape_string($dbc, trim($_POST['fab3']));
        $non= mysqli_real_escape_string($dbc, trim($_POST['non']));
        $dis= mysqli_real_escape_string($dbc, trim($_POST['dis']));
        $comm= mysqli_real_escape_string($dbc, trim($_POST['comm']));
        $caad= mysqli_real_escape_string($dbc, trim($_POST['caad']));
        $po= mysqli_real_escape_string($dbc, trim($_POST['po']));
    
    Code (markup):
    why not using array and loop it for example

    
    foreach (array('ab', 'part', 'rev', 'etc') AS $key)
    {
         $$key = mysql_real_escape_string(trim($_POST[$key]));
    }
    
    Code (markup):
    and then the rest of the lines as you want to be. Just tips, do something with it! :)
     
    EricBruggema, Feb 21, 2012 IP
  7. Matt Ridge

    Matt Ridge Peon

    Messages:
    166
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #7
    It may be stupid to you, but I am still learning, I picked up HTML and basic PHP coding less than 6 months ago, and SQL integration less than 5 months ago... I am a very new programmer in this manner. So please what is stupid to you I am rather proud of working, even though it may not be pretty, or "clean". If you have suggestions offer them, don't insult someone just because you know you can do better... I can't because I am still learning, and I am as we are speaking learning about arrays through a book, never mind looping arrays...

    I have been working on this code since the beginning because my company is too cheap to hire an web expert, and told me to learn it to make what you see.

    Also, my ID is being pulling from a page called list.php.

    Here is said script:

    
    <!DOCTYPE html 
         PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>PDI NCMR Admin Panel</title>
    <link rel="stylesheet" type="text/css" href="../CSS/postie.css" />
    <body>
    <?php
    echo '<div id="admin">';
    //Show the navagation menu
    require_once('../hf/nav.php');
    echo '<hr id="line">';
    echo '<h2 id="title">Latest NCMRs </h2>';
        
      // Connect to the database 
        require_once('../connectvars.php');
      $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    
      // Retrieve the data from MySQL
      $query = "SELECT * FROM ncmr";
      $data = mysqli_query($dbc, $query);
        
      echo '<table>';
      echo '<tr class="desc"><th>NCMR ID</th> <th>Part</th> <th>Date</th> <th>Actions</th></tr>';
            while ($row = mysqli_fetch_array($data)) { 
        // Display the data
        echo '<tr class="ncmrdata">';
            echo '<td>' . $row['ncmrid'] .'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp</td>';
            echo '<td>' . $row['partdesc'] .'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp</td>';
            echo '<td>' . date("M d,Y",strtotime($row['date'])) . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp</td>';
            echo '<td class="editlist"><a class="elist" href="comment.php?id=' . $row['id'] . '"><strong>Comment</a></strong>&nbsp;&nbsp<strong><a class="elist" href="edit.php?id=' . $row['id'] . '">Edit</a>&nbsp;&nbsp<a class="elist"href="print.php?id=' . $row['id'] . '" target="_blank">Print</a>';
      echo '</td></tr>';
      }
      echo '</table>';
      
         mysqli_close($dbc);
         require_once('../hf/footer.php')
    
    
    ?>
    </body>
    </html>
    
    Code (markup):
     
    Matt Ridge, Feb 21, 2012 IP