Page 1 of 1

Update Query Not Working

Posted: Sat May 17, 2008 11:13 pm
by csand
I'm trying to write some php script that will on the first page ask for the record to alter by throwing all the id's into a drop down select menu (which works), it then includes a php file that will display all the data in a form's text fields below with an edit button- which when pressed should include another php page that will post all the fields and update the database with the new information. Unfortunuatly, everything is working but updating the database.

Here's my code:

Page 1:

Code: Select all

<p>
<form method="post">
<table>
<tr>
    <td>
    PIN&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    </td>
    <td>
        <select name='pin'>
        
        <?php
        include 'LoginDb.php';
        include 'UseActives.php';
 
        $query="SELECT pin from brothers";
        $pins=mysql_query($query)
            or die('Query Database failed.');
        while($row = mysql_fetch_row($pins))
        {
            $pin = $row[0];
            echo '<option>';
            echo $pin;
            echo'</option>';
        };
        
        while(list($pin) = mysql_fetch_array($pins,MYSQL_NUM))
        {
        $pin=$pin;
        };
        ?>
        </select>
    
    </td>
    <td>
        &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="select" type="submit" value="Select Brother">
    </td>   
</tr>
</form>
</table>
</p>
 
<?php
if(isset($_POST['select']))
{
    include 'inputBrotherRecord.php';
}
?>
 
<?php
include 'CloseDb.php';
?>
PHP Page 2 (inputBrotherRecord.php)

Code: Select all

<?php
 
$pin=$_POST['pin'];
 
include 'LoginDb.php';
include 'UseActives.php';
 
$query1="SELECT name,street1,street2,home,zip,hNumber,schNumber,major ".
        "FROM brothers ".
        "WHERE pin='$pin'";
 
$result = mysql_query($query1);
 
while(list($name,$street1,$street2,$home,$zip,$hNumber,$schNumber,$major) = mysql_fetch_array($result,MYSQL_NUM))
{
?>
 
<h2>
    PIN <?php echo $pin;?>
</h2>
 
<p>
<table>
<form method="post">
<tr>
    <td>
    Name
    </td>
    <td>
        <input id='name' type="text" value="<?php echo $name;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    Address Line 1
    </td>
    <td>
        <input name='street1' type="text" value="<?php echo $street1;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    Address Line 2
    </td>
    <td>
        <input name='street2' type="text" value="<?php echo $street2;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    City, State
    </td>
    <td>
        <input name='home' type="text" value="<?php echo $home;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    ZIP Code
    </td>
    <td>
        <input name='zip' type="text" value="<?php echo $zip;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    Home Number
    </td>
    <td>
        <input name='hNumber' type="text" value="<?php echo $hNumber;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    School Number
    </td>
    <td>
        <input name='schNumber' type="text" value="<?php echo $schNumber;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    Major
    </td>
    <td>
        <input name='major' type="text" value="<?php echo $major;?>">
    </td>
</tr>
</table>
</p>
<?php
}
?>
<p>
<input name="edit" type="submit" value="Edit Record">&nbsp
<input name="delete" type="submit" value="Delete Record">
 
<?php
 
if(isset($_POST['edit']))
{
    include 'editBrotherRecord.php';
}
 
?>
</p>
 
<p>
<a href="\BrotherMgt.php">Back to Brother Management Menu</a>
</p>
PHP Page 3 (editBrotherRecord.php)

Code: Select all

<?php
 
    include 'LoginDb.php';
    include 'UseActives.php';
    
    $name=$_POST["name"];
    $street1=$_POST['street1'];
    $street2=$_POST['street2'];
    $home=$_POST['home'];
    $zip=$_POST['zip'];
    $hNumber=$_POST['hNumber'];
    $schNumber=$_POST['schNumber'];
    $major=$_POST['major'];
    $query2="UPDATE brothers ".
        "SET name='$name',street1='$street1',street2='$street2',home='$home',".
        "zip='$zip',hNumber='$hNumber',schNumber='$schNumber',major='$major' ".
        "WHERE pin='$pin'";
    mysql_query($query2)
        or die('Update failed.');   
?>

Re: Update Query Not Working

Posted: Sun May 18, 2008 4:46 pm
by csand
bump. someone please try to take a look. It's just the update (3rd page) that's not working.

Edit: Did some more work and realized that pin probably wasn't being passed from inputBrotherRecord to editBrotherRecord.

I added

Code: Select all

<input name='pin' type="hidden" value="<?php echo $pin; ?>">
to inputBrotherRecord and

Code: Select all

$pin=$_POST['pin'];
to editBrotherRecord...but still no luck.

Re: Update Query Not Working

Posted: Sun May 18, 2008 5:13 pm
by califdon
You can't include another file and expect the POST operation to pass any data. Your Form must specify the action parameter (see http://www.w3.org/TR/html401/interact/forms.html#h-17.3) and when the Submit button is clicked, it will call the URL in the action parameter and pass the Form's fields as specified in the method parameter.

In other words, you need:
  1. An HTML document with a Form element that specifies an action URL;
  2. A PHP script with the URL specified;
  3. PHP code in that script that interacts with the database.
It is possible to combine all 3 in one PHP script, but you can't do it by including a file in the script.

Re: Update Query Not Working

Posted: Sun May 18, 2008 7:11 pm
by csand
Thanks for the answer. I changed that and a few things and got it working. For those interested the final code...

editBrothers.php

Code: Select all

<p>
<form method="post" action="editBrothers.php">
<table>
<tr>
    <td>
    PIN&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    </td>
    <td>
        <select name='pin'>
        
        <?php
        include 'LoginDb.php';
        include 'UseActives.php';
 
        $query="SELECT pin from brothers";
        $pins=mysql_query($query)
            or die('Query Database failed.');
        while($row = mysql_fetch_row($pins))
        {
            $pin = $row[0];
            echo '<option>';
            echo $pin;
            echo'</option>';
        };
        
        while(list($pin) = mysql_fetch_array($pins,MYSQL_NUM))
        {
        $pin=$pin;
        };
        ?>
        </select>
    
    </td>
    <td>
        &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="select" type="submit" value="Select Brother">
    </td>   
</tr>
</form>
</table>
</p>
 
<?php
 
$pin=$_POST['pin'];
 
include 'LoginDb.php';
include 'UseActives.php';
 
$query1="SELECT name,street1,street2,home,zip,hNumber,schNumber,major ".
        "FROM brothers ".
        "WHERE pin='$pin'";
 
$result = mysql_query($query1);
 
while(list($name,$street1,$street2,$home,$zip,$hNumber,$schNumber,$major) = mysql_fetch_array($result,MYSQL_NUM))
{
?>
 
<h2>
    PIN <?php echo $pin;?>
</h2>
 
<p>
<table>
<form method="post" action="editBrotherRecord.php">
<tr>
    <td>
    Name
    </td>
    <td>
        <input name='name' type="text" value="<?php echo $name;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    Address Line 1
    </td>
    <td>
        <input name='street1' type="text" value="<?php echo $street1;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    Address Line 2
    </td>
    <td>
        <input name='street2' type="text" value="<?php echo $street2;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    City, State
    </td>
    <td>
        <input name='home' type="text" value="<?php echo $home;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    ZIP Code
    </td>
    <td>
        <input name='zip' type="text" value="<?php echo $zip;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    Home Number
    </td>
    <td>
        <input name='hNumber' type="text" value="<?php echo $hNumber;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    School Number
    </td>
    <td>
        <input name='schNumber' type="text" value="<?php echo $schNumber;?>">
    </td>
</tr>
<tr>
    <td>
        &nbsp
    </td>
</tr>
<tr>
    <td>
    Major
    </td>
    <td>
        <input name='major' type="text" value="<?php echo $major;?>">
    </td>
</tr>
<input name='pin' type="hidden" value="<?php echo $pin; ?>">
</table>
</p>
 
<p>
<input name="edit" type="submit" value="Edit Record">&nbsp
<input name="delete" type="submit" value="Delete Record">
 
</form>
</p>
 
<?php
}
?>
 
<?php
include 'CloseDb.php';
?>
 
<p>
<a href="\BrotherMgt.php">Back to Brother Management Menu</a>
</p>
editBrotherRecord.php

Code: Select all

<?php
 
$pin=$_POST['pin'];
 
if ($_POST['edit'])
{
    $name=$_POST['name'];
    $street1=$_POST['street1'];
    $street2=$_POST['street2'];
    $home=$_POST['home'];
    $zip=$_POST['zip'];
    $hNumber=$_POST['hNumber'];
    $schNumber=$_POST['schNumber'];
    $major=$_POST['major'];
    
    include 'LoginDb.php';
    include 'UseActives.php';
    
    $query2="UPDATE brothers ".
        "SET name='$name',street1='$street1',street2='$street2',home='$home',".
        "zip='$zip',hNumber='$hNumber',schNumber='$schNumber',major='$major' ".
        "WHERE pin='$pin'";
    mysql_query($query2)
        or die('Update failed.');
    echo "Query successful.";
}
 
if ($_POST['delete'])
{
    include 'UseActives.php';
    
    $query3="DELETE FROM brothers WHERE pin='$pin'";
    
    mysql_query($query3)
        or die('Delete failed.');
    echo "Delete successful.";
}
?>
 
<p>
<form method="post" action="editBrothers.php">
<table>
<tr>
    <td>
    PIN&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
    </td>
    <td>
        <select name='pin'>
        
        <?php
 
        $query="SELECT pin from brothers";
        $pins=mysql_query($query)
            or die('Query Database failed.');
        while($row = mysql_fetch_row($pins))
        {
            $pin = $row[0];
            echo '<option>';
            echo $pin;
            echo'</option>';
        };
        
        while(list($pin) = mysql_fetch_array($pins,MYSQL_NUM))
        {
        $pin=$pin;
        };
        ?>
        </select>
    
    </td>
    <td>
        &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<input name="select" type="submit" value="Select Brother">
    </td>   
</tr>
</table>
</form>
</p>
 
<p>
<a href="\BrotherMgt.php">Back to Brother Management Menu</a>
</p>