SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Edit/Update form

    I am still trying to find what's wrong with this code to make it display correctly so db entries may be edited. See attachment when it's approved.
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","root","") or die('Could not connect: ' mysql_error());
    mysql_select_db("job_listing"$con);
    $sql "SELECT * FROM jobs";
    $result=mysql_query($sql);
    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <table class="listing">
    <form method="post" action="<?php $_SERVER['REQUEST_URI']; ?>">
    <?php
    while($row=mysql_fetch_array($result)){
    ?>
            <tr>
              <th>Job Number:</th>
              <td><input type='text' name='JobNumber[]' value="<? echo . $row['JobNumber'] . ; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Title:</th>
              <td><input type='text' name='JobTitle[]' value="<? echo  $row['JobTitle'] . ; ?> ">   </td>
            </tr>
            <tr>
              <th>Company:</th>
              <td><input type='text' name='JobCompany[]'value="<? echo . $row['JobCompany'] . ; ?> ">   </td>
            </tr>
            <tr>
              <th>Location:</th>
              <td><input type='text' name='JobLocation[]'value="<? echo . $row['JobLocation'] . ; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Description:</th>
              <td><input type='textarea' name='JobDescription[]' > <? echo . $row['JobDescription'] . ; ?> </textarea>   </td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
    <?php
    }
    ?>
      <tr>
        <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
      </tr>
    </form>
    </table>
    <?php

    if(isset($_POST['Submit'])){
        for(
    $i=0;$i<$count;$i++){
        
    $JobTitle[$i] = mysql_real_escape_string($JobTitle[$i]);
        
    $JobCompany[$i] = mysql_real_escape_string($JobCompany[$i]);
        
    $JobLocation[$i]= mysql_real_escape_string($JobLocation[$i]);
        
    $JobDescription[$i] = mysql_real_escape_string($JobDescription[$i]);
        
    $JobNumber[$i] = mysql_real_escape_string($JobNumber[$i]);
        
    $sql1="UPDATE jobs SET JobTitle='".$JobTitle[$i]."', JobCompany='".$JobCompany[$i]."', JobLocation='".$JobLocation[$i]."', JobDescription='".$JobDescription['$i']."' WHERE JobNumber=".$JobNumber[$i];
        
    $result1=mysql_query($sql1);
    }
    }
    if(
    $result1){
        echo 
    'all done';
    }
    ?>
    Attached Images Attached Images

  2. #2
    SitePoint Enthusiast rashedirshad's Avatar
    Join Date
    Sep 2009
    Location
    Rawalpindi, Pakistan
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your JobNumber is set to Autoincrement then it starts from 1 and your first value index 0. What is the error you are getting?
    Also try to echo $count, and see what it displays. I have seen sometimes it does not show the number of records properly.

    Khan
    _______________________
    http://www.peoplesourceinternational.com/

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get any error code. Everything displays except the textarea jobDescription which is blank. Any changes made to any input area will not submit. And there is only one submit button and there should be one on each listing.
    I have made a few changes to the form.
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","root","") or die('Could not connect: ' mysql_error());
    mysql_select_db("job_listing"$con);
    $sql "SELECT * FROM jobs";
    $result=mysql_query($sql);
    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <table class="listing">
    <form method="post" action="<?php $_SERVER['REQUEST_URI']; ?>">
    <?php
    while($row=mysql_fetch_array($result)){
    ?>
            <tr>
              <th>Job Number:</th>
              <td><input type='text' name='JobNumber[]' size="20" maxlength="250" value="<?php echo $row['JobNumber']; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Title:</th>
              <td><input type='text' name='JobTitle[]' size="100" maxlength="250" value="<?php echo $row['JobTitle']; ?> ">   </td>
            </tr>
            <tr>
              <th>Company:</th>
              <td><input type='text' name='JobCompany[]' size="100" maxlength="250" value="<?php echo $row['JobCompany']; ?> ">   </td>
            </tr>
            <tr>
              <th>Location:</th>
              <td><input type='text' name='JobLocation[]' size="100" maxlength="250" value="<?php echo $row['JobLocation']; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Description:</th>
              <td><textarea input type='text' name='JobDescription[]' rows="10" cols="61" value="<?php echo $row['JobDescription']; ?>" </textarea>   </td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
    <?php
    }
    ?>
      <tr>
        <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
      </tr>
    </form>
    </table>
    <?php

    if(isset($_POST['Submit'])){
        for(
    $i=0;$i<$count;$i++){
        
    $JobTitle[$i] = mysql_real_escape_string($JobTitle[$i]);
        
    $JobCompany[$i] = mysql_real_escape_string($JobCompany[$i]);
        
    $JobLocation[$i]= mysql_real_escape_string($JobLocation[$i]);
        
    $JobDescription[$i] = mysql_real_escape_string($JobDescription[$i]);
        
    $JobNumber[$i] = mysql_real_escape_string($JobNumber[$i]);
        
    $sql1="UPDATE jobs SET JobTitle='".$JobTitle[$i]."', JobCompany='".$JobCompany[$i]."', JobLocation='".$JobLocation[$i]."', JobDescription='".$JobDescription['$i']."' WHERE JobNumber=".$JobNumber[$i];
        
    $result1=mysql_query($sql1);
    }
    }
    if(
    $result1){
        echo 
    'all done';
    }
    ?>

  4. #4
    SitePoint Enthusiast rashedirshad's Avatar
    Join Date
    Sep 2009
    Location
    Rawalpindi, Pakistan
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    <textarea input

    Use <textarea> not <input>

    PHP Code:


    <tr>

              <th>Job Description:</th>

              <td><textarea name='JobDescription[]' > <? echo . $row['JobDescription'] . ; ?> </textarea>   </td>

            </tr>
    Okay. You have made another mistake. <textarea input

    Use this

    PHP Code:
    <tr>

              <th>Job Description:</th>

              <td><textarea name='JobDescription[]' rows="10" cols="61" value="<?php echo $row['JobDescription']; ?>" </textarea>   </td>

            </tr>


    Last edited by rashedirshad; Sep 9, 2009 at 13:21. Reason: Editing
    Khan
    _______________________
    http://www.peoplesourceinternational.com/

  5. #5
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Either way it remains blank as if there is nothing in that db field.

  6. #6
    SitePoint Enthusiast rashedirshad's Avatar
    Join Date
    Sep 2009
    Location
    Rawalpindi, Pakistan
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry I could not see the whole code the mistake is the Value attribute you have put in <Textarea>

    Use this

    PHP Code:
    <tr>

              <th>Job Description:</th>

              <td><textarea name='JobDescription[]' rows="10" cols="61"><?php echo $row['JobDescription']; ?> </textarea>   </td>

            </tr>
    This will work perfectly.

    Khan
    _______________________
    http://www.peoplesourceinternational.com/

  7. #7
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great that works. It still does not update the db with any changes made though.

  8. #8
    SitePoint Enthusiast rashedirshad's Avatar
    Join Date
    Sep 2009
    Location
    Rawalpindi, Pakistan
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay. Use this as $sql1
    PHP Code:
    $sql1="UPDATE jobs SET JobTitle='".$JobTitle[$i]."', JobCompany='".$JobCompany[$i]."', JobLocation='".$JobLocation[$i]."', JobDescription='".$JobDescription[$i]."' WHERE JobNumber=".$JobNumber[$i]; 
    Khan
    _______________________
    http://www.peoplesourceinternational.com/

  9. #9
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I added your code and add this line also
    PHP Code:
    if (! $result1) echo mysql_error(); 
    after this line
    PHP Code:
    $result1=mysql_query($sql1); 
    and got this error
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","root","") or die('Could not connect: ' mysql_error());
    mysql_select_db("job_listing"$con);
    $sql "SELECT * FROM jobs";
    $result=mysql_query($sql);
    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <table class="listing">
    <form method="post" action="<?php $_SERVER['REQUEST_URI']; ?>">
    <?php
    while($row=mysql_fetch_array($result)){
    ?>
            <tr>
              <th>Job Number:</th>
              <td><input type='text' name='JobNumber[]' size="20" maxlength="250" value="<?php echo $row['JobNumber']; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Title:</th>
              <td><input type='text' name='JobTitle[]' size="100" maxlength="250" value="<?php echo $row['JobTitle']; ?> ">   </td>
            </tr>
            <tr>
              <th>Company:</th>
              <td><input type='text' name='JobCompany[]' size="100" maxlength="250" value="<?php echo $row['JobCompany']; ?> ">   </td>
            </tr>
            <tr>
              <th>Location:</th>
              <td><input type='text' name='JobLocation[]' size="100" maxlength="250" value="<?php echo $row['JobLocation']; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Description:</th>
              <td><textarea name='JobDescription[]' rows="10" cols="61"><?php echo $row['JobDescription']; ?> </textarea>   </td>

            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
    <?php
    }
    ?>
      <tr>
        <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
      </tr>
    </form>
    </table>
    <?php

    if(isset($_POST['Submit'])){
        for(
    $i=0;$i<$count;$i++){
        
    $JobTitle[$i] = mysql_real_escape_string($JobTitle[$i]);
        
    $JobCompany[$i] = mysql_real_escape_string($JobCompany[$i]);
        
    $JobLocation[$i]= mysql_real_escape_string($JobLocation[$i]);
        
    $JobDescription[$i] = mysql_real_escape_string($JobDescription[$i]);
        
    $JobNumber[$i] = mysql_real_escape_string($JobNumber[$i]);
    $sql1="UPDATE jobs SET JobTitle='".$JobTitle[$i]."', JobCompany='".$JobCompany[$i]."', JobLocation='".$JobLocation[$i]."', JobDescription='".$JobDescription[$i]."' WHERE JobNumber=".$JobNumber[$i];
        
    $result1=mysql_query($sql1);
        if (! 
    $result1) echo mysql_error();
        
    }
    }
    if(
    $result1){
        echo 
    'all done';
    }
    ?>

  10. #10
    SitePoint Enthusiast rashedirshad's Avatar
    Join Date
    Sep 2009
    Location
    Rawalpindi, Pakistan
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo $sql1 and paste the query here here.
    Khan
    _______________________
    http://www.peoplesourceinternational.com/

  11. #11
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I echo $sql1 or if I use
    PHP Code:
    if (! $result1) echo mysql_error(); 
    I get the same result

    UPDATE jobs SET JobTitle='', JobCompany='', JobLocation='', JobDescription='' WHERE JobNumber=UPDATE jobs SET JobTitle='', JobCompany='', JobLocation='', JobDescription='' WHERE JobNumber=

    I'm beginning to think I am no longer connected to the db once the form is filled.

  12. #12
    SitePoint Enthusiast rashedirshad's Avatar
    Join Date
    Sep 2009
    Location
    Rawalpindi, Pakistan
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Use this
    PHP Code:

    <?php

    $con 
    mysql_connect("localhost","root","") or die('Could not connect: ' mysql_error());

    mysql_select_db("job_listing"$con);

    $sql "SELECT * FROM jobs";

    $result=mysql_query($sql);

    // Count table rows

    $count=mysql_num_rows($result);

    ?>

    <table class="listing">

    <form method="post" action="<?php $_SERVER['REQUEST_URI']; ?>">

    <?php

    while($row=mysql_fetch_array($result)){

    ?>

            <tr>

              <th>Job Number:</th>

              <td><input type='text' name='JobNumber[]' value="<? echo . $row['JobNumber'] . ; ?> ">   </td>

            </tr>

            <tr>

              <th>Job Title:</th>

              <td><input type='text' name='JobTitle[]' value="<? echo  $row['JobTitle'] . ; ?> ">   </td>

            </tr>

            <tr>

              <th>Company:</th>

              <td><input type='text' name='JobCompany[]'value="<? echo . $row['JobCompany'] . ; ?> ">   </td>

            </tr>

            <tr>

              <th>Location:</th>

              <td><input type='text' name='JobLocation[]'value="<? echo . $row['JobLocation'] . ; ?> ">   </td>

            </tr>

            <tr>

              <th>Job Description:</th>

              <td><input type='textarea' name='JobDescription[]' > <? echo . $row['JobDescription'] . ; ?> </textarea>   </td>

            </tr>

            <tr>

              <td>&nbsp;</td>

              <td>&nbsp;</td>

            </tr>

    <?php

    }

    ?>

      <tr>

        <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>

      </tr>

    </form>

    </table>

    <?php



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

    $sql "SELECT * FROM jobs";

    $result=mysql_query($sql);

    // Count table rows

    $count=mysql_num_rows($result);

        for(
    $i=0;$i<$count;$i++){

        
    $JobTitle[$i] = mysql_real_escape_string($JobTitle[$i]);

        
    $JobCompany[$i] = mysql_real_escape_string($JobCompany[$i]);

        
    $JobLocation[$i]= mysql_real_escape_string($JobLocation[$i]);

        
    $JobDescription[$i] = mysql_real_escape_string($JobDescription[$i]);

        
    $JobNumber[$i] = mysql_real_escape_string($JobNumber[$i]);

        
    $sql1="UPDATE jobs SET JobTitle='".$JobTitle[$i]."', JobCompany='".$JobCompany[$i]."', JobLocation='".$JobLocation[$i]."', JobDescription='".$JobDescription['$i']."' WHERE JobNumber=".$JobNumber[$i];

        
    $result1=mysql_query($sql1);

    }

    }

    if(
    $result1){

        echo 
    'all done';

    }

    ?>
    Khan
    _______________________
    http://www.peoplesourceinternational.com/

  13. #13
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get results if I use this code. However it does not show the update in the form after submission but does show the change on the output php page.
    PHP Code:
    <?php
    $con 
    mysql_connect("localhost","root","") or die('Could not connect: ' mysql_error());
    mysql_select_db("job_listing"$con);
    $sql "SELECT * FROM jobs";
    $result=mysql_query($sql);
    // Count table rows
    $count=mysql_num_rows($result);
    ?>
    <table class="listing">
    <form method="post" action="<?php $_SERVER['REQUEST_URI']; ?>">
    <?php
    while($row=mysql_fetch_array($result)){
    ?>
            <tr>
              <th>Job Number:</th>
              <td><input type='text' name='JobNumber[]' size="20" maxlength="250" value="<?php echo $row['JobNumber']; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Title:</th>
              <td><input type='text' name='JobTitle[]' size="100" maxlength="250" value="<?php echo $row['JobTitle']; ?> ">   </td>
            </tr>
            <tr>
              <th>Company:</th>
              <td><input type='text' name='JobCompany[]' size="100" maxlength="250" value="<?php echo $row['JobCompany']; ?> ">   </td>
            </tr>
            <tr>
              <th>Location:</th>
              <td><input type='text' name='JobLocation[]' size="100" maxlength="250" value="<?php echo $row['JobLocation']; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Description:</th>
              <td><textarea name='JobDescription[]' rows="10" cols="61"><?php echo $row['JobDescription']; ?> </textarea>   </td>

            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
    <?php
    }
    ?>
      <tr>
        <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
      </tr>
    </form>
    </table>
    <?php

    if(isset($_POST['Submit'])){
        for(
    $i=0;$i<$count;$i++){
        
    $JobTitle[$i] = mysql_real_escape_string($_POST['JobTitle'][$i]);
        
    $JobCompany[$i] = mysql_real_escape_string($_POST['JobCompany'][$i]);
        
    $JobLocation[$i]= mysql_real_escape_string($_POST['JobLocation'][$i]);
        
    $JobDescription[$i] = mysql_real_escape_string($_POST['JobDescription'][$i]);
        
    $JobNumber[$i] = mysql_real_escape_string($_POST['JobNumber'][$i]);
    $sql1="UPDATE jobs SET JobTitle='".$JobTitle[$i]."', JobCompany='".$JobCompany[$i]."', JobLocation='".$JobLocation[$i]."', JobDescription='".$JobDescription[$i]."' WHERE JobNumber=" .$JobNumber[$i];
    echo 
    $sql1;
        
    $result1=mysql_query($sql1);
        
    }
    }
    if(
    $result1){
        echo 
    'all done';
    }
    ?>

  14. #14
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I finally have this resolved. For those watching this thread here is the code I ended up with.

    PHP Code:
    <?php
    // connect to database
    $con mysql_connect("","","") or die('Could not connect: ' mysql_error());
    mysql_select_db("job_listing"$con);

    // If user POSTed an update
    if(isset($_POST['Submit'])){
      
    $count count($_POST['JobNumber']);
      
    $done 0;
        for(
    $i=0;$i<$count;$i++){
          
    $JobTitle[$i] = mysql_real_escape_string($_POST['JobTitle'][$i]);
          
    $JobCompany[$i] = mysql_real_escape_string($_POST['JobCompany'][$i]);
          
    $JobLocation[$i]= mysql_real_escape_string($_POST['JobLocation'][$i]);
          
    $JobDescription[$i] = mysql_real_escape_string($_POST['JobDescription'][$i]);
          
    $JobNumber[$i] = mysql_real_escape_string($_POST['JobNumber'][$i]);
          
    $sql1="UPDATE jobs SET JobTitle='".$JobTitle[$i]."', JobCompany='".$JobCompany[$i]."', JobLocation='".$JobLocation[$i]."', JobDescription='".$JobDescription[$i]."' WHERE JobNumber=".$JobNumber[$i];
          
    $result1=mysql_query($sql1);
          if (
    $result1$done++;
      }
      echo 
    $done ' updates succeeded (out of ' $count ' attempted.)';
    }
    /* NOTE: $result1 will NOT be defined if there are no updates.  
    Also, $result1 will be TRUE or FALSE based on the results of the LAST call.    */
    //if($result1){
    //
    //}

    // Show the current data
    $sql "SELECT * FROM jobs";
    $result=mysql_query($sql);
    // Count table rows
    $count=mysql_num_rows($result);
    ?>

    <?php
    while($row=mysql_fetch_array($result)){
    ?>
    <table class="listing">
    <form method="post" action="<?php $_SERVER['REQUEST_URI']; ?>">
            <tr>
              <th>Job Number:</th>
              <td><input type='text' name='JobNumber[]' size="20" maxlength="250" value="<?php echo $row['JobNumber']; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Title:</th>
              <td><input type='text' name='JobTitle[]' size="100" maxlength="250" value="<?php echo $row['JobTitle']; ?> ">   </td>
            </tr>
            <tr>
              <th>Company:</th>
              <td><input type='text' name='JobCompany[]' size="100" maxlength="250" value="<?php echo $row['JobCompany']; ?> ">   </td>
            </tr>
            <tr>
              <th>Location:</th>
              <td><input type='text' name='JobLocation[]' size="100" maxlength="250" value="<?php echo $row['JobLocation']; ?> ">   </td>
            </tr>
            <tr>
              <th>Job Description:</th>
              <td><textarea name='JobDescription[]' rows="10" cols="61"><?php echo $row['JobDescription']; ?> </textarea>   </td>
            </tr>
            <tr>
              <td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
            </tr>
    </form>
    </table>
    <?php
    }
    ?>


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
  •