Jump to content

I thought it was solved. Not so! INSERT/UPDATE


facarroll

Recommended Posts

I put a snippet up yesterday, and thought it had been solved. It had a typo error, but there is still something wrong. The whole page follows, because it might be easier to see what I am doing. The form successfully inserts a new record OK, but it will not update a record that already exists as is intended. Instead, it always creates another new record. Where can I be wrong?

 

<?php
session_start();

// See if logged in member by checking session data
if (isset($_SESSION['recid'])) {
    // Put stored session variables into local php variable
    $userid      = $_SESSION['recid'];
    $username    = $_SESSION['name'];
    $school      = $_SESSION['college'];
    $payer_email = $_SESSION['payer_email'];
    $group1      = $_SESSION['group1'];
    $group2      = $_SESSION['group2'];
    $group3      = $_SESSION['group3'];
    $group4      = $_SESSION['group4'];
    $group5      = $_SESSION['group5'];
    $group6      = $_SESSION['group6'];

    
} else { ?>
<!-- make login box -->
<html>
<tr>
  <td> 
    <p> 
    <p> 
    <p></td>
</tr>
<table style= border align="center" border="1" width="400" height="200">
    <td align="center" valign="center">You need to<a href="login_allocate_quiz.php">log in</a>as an administrator to<br />
      allocate quizzes to student groups.</td>
</table>
</html>
<?php exit(); 
}

    // Set error message as blank upon arrival to page
    $errorMsg = "";
    // Check to see if the form has been submitted 
    if (isset($_POST['equip'])){
    //Connect to the database through include 
    include_once "demo_conn.php";
    //set variables    
    $equip     = $_POST['equip'];
    $egroup1   = $_POST['egroup1'];
    $egroup2   = $_POST['egroup2'];
    $egroup3   = $_POST['egroup3'];
    $egroup4   = $_POST['egroup4'];
    $egroup5   = $_POST['egroup5'];
    $egroup6   = $_POST['egroup6'];
    
    //create variables to manipulate filenames in database
    $lowquip = strtolower($equip);
    $wordquip = str_replace (" ", "", $lowquip);
    $bigpic    = $wordquip.'big';
    $smallpic  = $wordquip.'small';
    $bigpic    = $bigpic.'.jpg';
    $smallpic = $smallpic.'.jpg';
    $errorMsg = '<font size="-1"><u>ERROR:</u>   You have not entered the following required information.';
    $nogroup = ((!$egroup1) && (!$egroup2) && (!$egroup3) && (!$egroup4) && (!$egroup5) && (!$egroup6));
    $noequip = (!$equip);
    if ($nogroup)
        {$errorMsg .= "--- Student Group. (No data was forwarded to the database.)</font>";}
    else if($noequip)
        {$errorMsg .= "--- Quiz Name. (No data was forwarded to the database.)</font>";}
    else 
        {$query = mysql_query("SELECT equip FROM topics WHERE equip = '$equip' AND managerId = '$userid'");
              if (mysql_num_rows($query) > 0)
                mysql_query("UPDATE topics SET egroup1 = '$egroup1', egroup2 = '$egroup2', egroup3 = '$egroup3', egroup4 = '$egroup4', egroup5 = '$egroup5', egroup6 = '$egroup6' WHERE equip = '$equip' AND managerId = '$userid'");
            else
                mysql_query("INSERT INTO topics (managerId, equip, title, url_big, url_small, egroup1, egroup2, egroup3, egroup4, egroup5, egroup6) VALUES ('$userid','$wordquip', '$equip', '$bigpic', '$smallpic', '$egroup1', '$egroup2', '$egroup3', '$egroup4', '$egroup5', '$egroup6')") or die (mysql_error("error 1321"));            
        $URL='make_quiz_list_test4.php';
        header ('Location: '.$URL);
        }    
    }
?>
<!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" dir="ltr" lang="en-US" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta http-equiv="X-UA-Compatible" content="IE=4">
<!-- IE5 mode -->
<meta http-equiv="X-UA-Compatible" content="IE=7.5">
<!-- IE7 mode -->
<meta http-equiv="X-UA-Compatible" content="IE=100">
<!-- IE8 mode -->
<meta http-equiv="X-UA-Compatible" content="IE=a">
<!-- IE5 mode -->
<title>Manager selected quiz list</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<!--[if IE 6]><link rel="stylesheet" href="style.ie6.css" type="text/css" media="screen" /><![endif]-->
<!--[if IE 7]><link rel="stylesheet" href="style.ie7.css" type="text/css" media="screen" /><![endif]-->
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<div id="art-page-background-simple-gradient">
  <div id="art-page-background-gradient"></div>
</div>
<div id="art-main">
  <div class="art-sheet">
    <div class="art-sheet-tl"></div>
    <div class="art-sheet-tr"></div>
    <div class="art-sheet-bl"></div>
    <div class="art-sheet-br"></div>
    <div class="art-sheet-tc"></div>
    <div class="art-sheet-bc"></div>
    <div class="art-sheet-cl"></div>
    <div class="art-sheet-cr"></div>
    <div class="art-sheet-cc"></div>
    <div class="art-sheet-body">
      <div class="art-header">
        <div class="art-header-png"></div>
        <div class="art-header-jpeg"></div>
        <div class="art-logo">
          <h1 id="name-text" class="art-logo-name"><a href="#">Safety Testing Online</a></h1>
          <div id="slogan-text" class="art-logo-text">Safety - The Most Valuable Insurance</div>
        </div>
      </div>
      <div class="art-nav">
        <div class="l"></div>
        <div class="r"></div>
        <div class="art-nav-center">
          <ul class="art-menu">
            <li> <a href="../../index.php" class="active"><span class="l"></span><span class="r"></span><span class="t">Home</span></a> </li>
            <li> <a href="../demo_main.php"><span class="l"></span><span class="r"></span><span class="t">Demonstrations Page</span></a>
              <ul>
              </ul>
            </li>
            <li> <a href="logout.php"><span class="l"></span><span class="r"></span><span class="t">Admin Logout</span></a> </li>
          </ul>
        </div>
      </div>
      <div class="art-content-layout">
        <div class="art-content-layout-row">
          <div class="art-layout-cell art-content">
            <div class="art-post">
              <div class="art-post-tl"></div>
              <div class="art-post-tr"></div>
              <div class="art-post-bl"></div>
              <div class="art-post-br"></div>
              <div class="art-post-tc"></div>
              <div class="art-post-bc"></div>
              <div class="art-post-cl"></div>
              <div class="art-post-cr"></div>
              <div class="art-post-cc"></div>
              <div class="art-post-body"></div>
              <div class="art-post-bl"></div>
              <div class="art-post-br"></div>
              <div class="art-post-tc"></div>
              <div class="art-post-bc"></div>
              <div class="art-post-cl"></div>
              <div class="art-post-cr"></div>
              <div class="art-post-cc"></div>
              <div class="art-post-body">
                <div class="art-post-inner art-article">
                  <table style="table-layout:fixed; width:850px;">
                    <tr>
                      <td style="text-align:left; width:30px;"> </td>
                      <td style="text-align:left; width:350px;"><font size="+1">Allocation of Woodwork Quizzes to Groups.</font></td>
                      <td style="text-align:left; text-align:center; vertical-align:bottom; width:450px;"><font color="#0000FF">Administrator: <?php echo $username; ?> - <?php echo $school; ?>.</font></td>
                      <td style="text-align:left; width:20px;"></td>
                    </tr>
                  </table>
                  <table style="table-layout:fixed; width:850px;">
                    <tr>
                      <td style="text-align:left; width:30px;"></td>
                      <td style="text-align:left; vertical-align:bottom; width:800px;"><font color="#FF0000"><?php echo "$errorMsg"; ?></font></td>
                      <td style="text-align:left; width:20px;"></td>
                    </tr>
                  </table>
                  <table style="table-layout:fixed; border:thin; text-align:left; width:850px;">
                    <tr>
                      <td style="text-align:right; width:35px;"></td>
                      <td style="text-align:left; width:190px;"></td>
                      <td style="text-align:right; width:20px;"></td>
                      <td style="text-align:left; width:210px;"><font color="#FF0000"></font></td>
                      <td style="text-align:right; width:20px;"></td>
                      <td style="text-align:left; width:110px;"></td>
                      <td style="text-align:right; width:20px;"></td>
                      <td style="text-align:right; width:180px;"></td>
                      <td style="text-align:right; width:35px;"></td>
                    </tr>
                    <hr/>
                    <tr>
                      <td rowspan="12"></td>
                      <td rowspan="12">
                      <br/>
                      <form method="post" enctype="multipart/form-data" action="make_quiz_list_test4.php">
                        <!--<form method="post" action="< ?php echo $PHP_SELF;?>">-->
                        <select size="25" name="equip" />
                        
                        <option selected value="">No Quiz Selected</option>
                        <option value="Biscuit Jointer">Biscuit Jointer</option>
                        <option value="Dowelling Machine">Dowelling Machine</option>
                        <option value="Drill Cordless">Drill - Cordless</option>
                        <option value="Drill with Cord">Drill - Power Corded</option>
                        <option value="Pedestal Drill">Drill - Pedestal</option>
                        <option value="Edge Tool Sharpener">Edge Tool Sharpener</option>
                        <option value="General Workshop Safety">General Workshop Safety</option>
                        <option value="Pedestal Grinder">Grinder - Pedestal</option>
                        <option value="Hand Tools">Hand Tools</option>
                        <option value="Jointer">Jointer (fixed)</option>
                        <option value="Jointer Surfacer Combo">Jointer/Surfacer (combo)</option>
                        <option value="Shaper">Shaper (fixed)</option>
                        <option value="Lathe for Wood">Lathe - Wood</option>
                        <option value="Linisher">Linisher</option>
                        <option value="Mortiser with Hollow Chisel">Mortiser - Hollow Chisel</option>
                        <option value="Nailgun for Brads">Nailer - Air (brads)</option>
                        <option value="Planer Portable">Planer - (portable)</option>
                        <option value="Router Portable">Router - Plunge</option>
                        <option value="Router with Sliding Table">Router Table - Slide</option>
                        <option value="Sander Belt">Sander - Belt (fixed)</option>
                        <option value="Sander Disc Belt Combo">Sander - Belt/Disc Combo)</option>
                        <option value="Sander Portable Belt">Sander - Belt (portable)</option>
                        <option value="Sander Disc">Sander - Disc (fixed)</option>
                        <option value="Sander Orbital">Sander - Orbital</option>
                        <option value="Sander Random Orbit">Sander - Random Orbital</option>
                        <option value="Sander Bobbin">Sander - Vertical Spindle</option>
                        <option value="Saw Bandsaw">Saw - Bandsaw</option>
                        <option value="Saw Bench">Saw - Circular (bench)</option>
                        <option value="Saw Portable Circular">Saw - Circular (portable)</option>
                        <option value="Saw Drop or Chop">Saw - Drop (alt Chop)</option>
                        <option value="Saw Drop and Slide">Saw - Drop & Slide</option>
                        <option value="Jigsaw Portable">Saw - Jigsaw (portable)</option>
                        <option value="Saw Panel">Saw - Panel (fixed)</option>
                        <option value="Saw Radial Arm">Saw - Radial Arm</option>
                        <option value="Saw Sabre">Saw - Sabre(portable)</option>
                        <option value="Saw Scroll">Saw - Scroll</option>
                        <option value="Screw Driver Portable">Screwdriver - Portable</option>
                        <option value="Thicknesser">Thickness Planer</option>
                        </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                        <td rowspan="7"><br />
                          <br />
                          <br />
                          <input type="checkbox" name="egroup1" value="1" />
                          <?php echo $group1; ?>
                          <p>
                            <input type="checkbox" name="egroup2" value="1" />
                            <?php echo $group2; ?>
                          <p>
                            <input type="checkbox" name="egroup3" value="1" />
                            <?php echo $group3; ?>
                          <p>
                            <input type="checkbox" name="egroup4" value="1" />
                            <?php echo $group4; ?>
                          <p>
                            <input type="checkbox" name="egroup5" value="1" />
                            <?php echo $group5; ?>
                          <p>
                            <input type="checkbox" name="egroup6" value="1" />
                            <?php echo $group6; ?>
                          <p>
                            <input type="submit" value="submit" name="Send Data">
                      </form>
                      </td>
                      <td></td>
                      <td></td>
                      <td></td>
                    </tr>
                    <tr>
                      <td></td>
                      <td style="border:solid; color:#006600; 1px; padding:10px; text-align:left;"><font size="-1">Select a Quiz Topic from the drop down list on the left. Then go to the check boxes on the right to select all of the groups that are expected to undertake this test.
                        <p>You can select only one quiz each time, but you can select from one to six groups to undertake that quiz.
                        <p>Repeat this process until you have allocated all of the tests that you require each group to complete.
                        <p>If you make a mistake you can delete a quiz allocation <a href="../../under_con.php">here</a> at any time. 
                        </font></td>
                      <td></td>
                      <td></td>
                      <td style="color:#006600;"><font size="-1">Go back to the <br />
                        <a href="member_account.php">main edit page</a>.<br/>
                         <br />
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                         <br/>
                        </font></td>
                    </tr>
                    <tr>
                      <td></td>
                      <td> <br/>
                         <br/>
                         <br/>
                         <br/></td>
                      <td></td>
                      <td></td>
                      <td></td>
                    </tr>
                  </table>
                  <!-- /article-content -->
                </div>
                <div class="cleared"></div>
              </div>
              <div class="cleared"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="cleared"></div>
    <div class="art-footer">
      <div class="art-footer-inner">
        <div class="art-footer-text">
          <p><a href="#">Contact Us</a> | <a href="#">Terms of Use</a> | <a href="#">Trademarks</a> | <a href="#">Privacy Statement</a><br />
            <?php 
$time = time () ; 
//This line gets the current time off the server
$year= date("Y",$time); 
//This line formats it to display just the year
echo "Copyright © 2010 - " . $year;
//this line prints out the copyright date range, you need to edit 2010 to be your opening year
?>
            All Rights Reserved.</p>
        </div>
      </div>
      <div class="art-footer-background"></div>
    </div>
    <div class="cleared"></div>
  </div>
</div>
<div class="cleared"></div>
<p class="art-page-footer">Designed by Frank.</p>
</div>
</body>
</html>

Link to comment
Share on other sites

in your INSERT query

mysql_query("INSERT INTO topics (managerId, equip, title, url_big, url_small, egroup1, egroup2, egroup3, egroup4, egroup5, egroup6)
  VALUES ('$userid','$wordquip', '$equip', '$bigpic', '$smallpic', '$egroup1', '$egroup2', '$egroup3', '$egroup4', '$egroup5', '$egroup6')") or die (mysql_error("error 1321"));

in fields list equip is on the 2nd position and in value list is 3rd   

Link to comment
Share on other sites

Why would you have the query strings intermingled with the query executions, instead of stored in variables so they could be echoed if debugging was necessary (which it obviously is)?

 

$query = "SELECT whatever FROM tbl";
$result = mysql_query($query) or die('<br />Query: ' . $query . '<br />Produced error: ' . mysql_error() . '<br />' );

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.