Jump to content

Updating mysql database with php


dave601

Recommended Posts

I have a decent amount of experience with Java, but am rather new to PHP.  I am trying to use PHP to update existing rows of data in a mysql database.  I got most of this code from a Spoono tutorial (http://www.spoono.com/php/tutorials/tutorial.php?id=23).  My problem is that this code works perfectly for me in Firefox and Safari in OS X, however it will not work in Windows XP or Vista.  In Windows, the table "blue_table" should only be displayed once at the beginning.  However, it is displayed throughout the updating process.  Here is the code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>System Update Form</title>

<style type="text/css">
@import url('style.css');
</style>

</head>

<body>
    <div id="header">
        <div id="title">System Update Form</div>
    </div>

    <div id="content">

        <?
        require_once('db_login.php');

        $connection = mysql_connect($db_host,$db_username,$db_password);
        if(!$connection)
        {
            die("Could not connect to the database: <br/> ".mysql_error());

        } // end if

        $db_select = mysql_select_db($db_database);
        if(!$db_select)
        {
            die ("Could not select the database <br />".mysql_error());

        } // end if

        if(!isset($cmd))
        {
            $result = mysql_query("SELECT * FROM system");

            echo '<table id="blue_table" cellspacing="0">

            <tbody>
                <tr class="odd">
                <th>System ID</th>
                <th>System Manufacturer</th>
                <th>System Model</th>
                <th>System Model Number</th>
                <th>System Manpower</th>
            </tr>';

            $loopCount = 0;

            while($result_row = mysql_fetch_array($result, MYSQL_ASSOC))
            {
                if($loopCount % 2 === 0)
                {
                    echo "<tr class='even'><td>";
                    $s_id = $result_row["s_id"];
                    echo "<a href='systemupdate.php?cmd=edit&s_id=$s_id'>$s_id</a>".'</td><td>';
                    echo $result_row["s_man"] . '</td><td>';
                    echo $result_row["s_model"] . '</td><td>';
                    echo $result_row["s_model_num"] . '</td><td>';
                    echo $result_row["s_manpw"] . '</td></tr>';

                } // end if

                if($loopCount % 2 > 0)
                {
                    echo "<tr class='odd'><td>";
                    $s_id = $result_row["s_id"];
                    echo "<a href='systemupdate.php?cmd=edit&s_id=$s_id'>$s_id</a>".'</td><td>';
                    echo $result_row["s_man"] . '</td><td>';
                    echo $result_row["s_model"] . '</td><td>';
                    echo $result_row["s_model_num"] . '</td><td>';
                    echo $result_row["s_manpw"] . '</td></tr>';

                } // end if

                $loopCount = $loopCount + 1;

            } // end while

        } // end if(!isset($cmd))

        ?>

        <?
        if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
        {
            if (!isset($_POST["submit"]))
            {
                $s_id = $_GET["s_id"];
                $sql = "SELECT * FROM system WHERE s_id=$s_id";
                $result = mysql_query($sql);
                $myrow = mysql_fetch_array($result);

                // this if statement continued below HTML
        ?>

        <form action="systemupdate.php" method="post">
            <input type=hidden name="s_id" value="<?php echo $myrow["s_id"] ?>">

            <p><label>System Manufacturer: <INPUT TYPE="TEXT" NAME="s_man" VALUE="<?php echo $myrow["s_man"] ?>" SIZE=30></label></p>
            <p><label>System Model: <INPUT TYPE="TEXT" NAME="s_model" VALUE="<?php echo $myrow["s_model"] ?>" SIZE=30></label></p>
            <p><label>System Model Number: <INPUT TYPE="TEXT" NAME="s_model_num" VALUE="<?php echo $myrow["s_model_num"] ?>" SIZE=30></label></p>
            <p><label class="req">System Manpower: <INPUT TYPE="TEXT" NAME="s_manpw" VALUE="<?php echo $myrow["s_manpw"] ?>" SIZE=30></label></p>

            <input type="hidden" name="cmd" value="edit">
            <input type="submit" name="submit" value="submit">

        </form>

        <? } // end if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit") ?>

        <?
            if($_POST["$submit"])
            {
                $s_man = $_POST["s_man"];
                $s_model = $_POST["s_model"];
                $s_model_num = $_POST["s_model_num"];
                $s_manpw = $_POST["s_manpw"];

                $sql = "UPDATE system SET s_man='$s_man',s_model='$s_model',s_model_num='$s_model_num',s_manpw='$s_manpw' WHERE s_id=$s_id";

                $result = mysql_query($sql);
                echo "<br />Information updated.<br /><br />";

                echo '<h3><a href="systemupdate.php">Return to the system update page.</a> <br />
                      <a href="index.php">Return to the main page.</a></h3>';

            } // end if($_POST["$submit"])

        } // end if ($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
        ?>

    </div>

</body>
</html>

 

Thanks for any help!

Link to comment
Share on other sites

My problem is that this code works perfectly for me in Firefox and Safari in OS X, however it will not work in Windows XP or Vista.  In Windows, the table "blue_table" should only be displayed once at the beginning.  However, it is displayed throughout the updating process.

OS X/Windows has nothing to do with the problem. Your browsers rendering engine does.

 

You have to add in the </table> tag after your done looping through the results.

 

            } // end while
          echo "</table>";
        } // end if(!isset($cmd))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.