#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Passing variables from a table row to PHP script


    I'm developing a web application for our village public library. This is a labor of love. I can put things in the database, and view the database just fine. With updates and deletes, I have a problem.

    In short, I have a pretty plain HTML file where the user selects the books he or she wishes to update or delete. That, in turn, calls a PHP routine, called upd2.php, which does the MySQL database query, and displays the results in a table, along with UPDATE and DELETE buttons and editable values in the fields of the table. Clicking one of those buttons calls a second PHP script, called upd3.php. The isbn field is the primary key on the database.

    Investigation shows that there appear to be no variables or values passed to the second PHP script, except for the button values (UPDATE or DELETE).

    So, passing parameters worked from the regular HTML file to the upd2.PHP script. That, in turn, wrote an HTML page, with a table having multiple columns and one or more rows. However, I cannot seem to use the HTML generated from this PHP script to call another PHP script.

    If I hard code a $bookid into the upd3.PHP script, it will delete that book fine.

    There are other columns in the tables and in the database, but for simplicity I've removed them here.

    (upd2.php)
    PHP Code:
     echo '<form action="upd3.php" method="post">';
    echo 
    "<table border='1'>

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

         
    $row = $result->fetch_assoc();
         
    $tti = $row['title'];
         
    $bookid = stripslashes($row['isbn']);
      echo '<tr>';
      echo '<td width=65> <input name=upda type=submit value="
    Update" > </td>';
      echo '<td width=65> <input name=dele type=submit value="
    Delete"> </td>';

    // Make this a hidden input later, since it would not make sense to edit this.
      echo '<td width=30><input name=bkid value="';
      echo $bookid;
      echo '" size=15 maxlength=30> </td>';

      echo '<td width=220><input name=titlee value="';
      echo $tti;
      echo '" size=20 maxlength=100> </td>';
      echo '<td width=110><input name=lauth value="';
      echo $authl;
      echo '" size=20 maxlength=30> </td>';
      echo '</tr>';
      }
    echo '</table>';
    echo '</form>';
      
      
    $result->free();
      
    $db->close();

    ?> 
    (upd3.php)
    PHP Code:
    <?php
      
    // create short variable names
      
    $upda=$_POST['upda'];
      
    $dele=$_POST['dele'];
      
    $titl=$_Post['titlee'];
      
    $authl=$_Post['lauth'];
      
    $bookid=$_Post['bkid'];



     
    //This works okay, but I left out the details for this post
     
    $db = new mysqli([I]parameters[/I]);

      if (
    mysqli_connect_errno()) 
      {
         echo 
    'Error: Could not connect to database.  Please try again later.';
         exit;
      }



        
    $bookid addslashes($bookid);

     
    $query "delete from books where isbn = '".$bookid."'";

      
    $result $db->query($query);
      if (
    $result)
    {
          echo  
    $db->affected_rows.' book deleted from database.'
    }

      
    $result->free();
      
    $db->close();


    ?>
    If I hard code something in for $bookid, like $bookid="444" right above the $query= line, this works great.

    The UPDATE code in upd3.php works similarly, but I'm working on getting it debugged on the simpler case of DELETE.

    Any help would be appreciated.
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,742
    Rep Power
    9397
    PHP variables are case-sensitive. $_POST and $_Post are two different things.

    For development you should always have two php.ini settings set a certain way: error_reporting=E_ALL (or better) and display_errors=on. Enable those, restart your webserver, and run your script as it is now. You should see error messages.

IMN logo majestic logo threadwatch logo seochat tools logo