SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2008
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error adding new event: Duplicate entry '0' for key 1-how do I fix this?

    OK, the following code adds an event to the database. I had events in the database already, origionally with IDs 1-9. I deleted event 1 to test my delete code so it was deleted. To test my add code I added an event and it worked fine, giving it an id of 0. However now when I try to add an event this error comes up:Error adding new event: Duplicate entry '0' for key 1.
    Can you tell me how to fix this in my code?Does it have any thing to do with putting in mysql_insert_id()?
    PHP Code:
    <?php

    //connect to the database server
    $dbcnx = @mysql_connect('localhost''root''password');
    if (!
    $dbcnx
    {
        exit(
    '<p>Unable to connect to the ' .
        
    'database server at this time.</p>');
    }

    //select the prototype database
    if (!@mysql_select_db('prototype')) 
    {
        exit(
    '<p>Unable to locate the joke ' .
        
    'database at this time.</p>');
    }

    if (isset(
    $_POST['eventName']))
    {
      
    // A new event has been entered
      // using the form.

      
    $eventType $_POST['eventType'];
      
    $eventName $_POST['eventName'];
      
    $date $_POST['demo1'];
      
    $time $_POST['time'];
      
    $moduleCode $_POST['mid'];

      
    $sql =   "INSERT INTO events SET
                   EventType = '
    $eventType ', 
                  EventName = '
    $eventName', 
                  Date = '
    $date ',
                  Time = '
    $time', 
                  ModuleCode= '
    $moduleCode'";  
      
      if (@
    mysql_query($sql)) 
      {
        echo 
    '<p>New event added</p>';
      } 
      else 
      {
        exit(
    '<p>Error adding new event: ' mysql_error() . '</p>');
      }

      
    ?>
      

    <p><a href="<?php echo $_SERVER['PHP_SELF']; ?>">Add another joke</a></p>
    <p><a href="viewallevents.php">Return to view all events</a></p>


    <?php
    }
    else 
    // Allow the user to enter a new event

    {
      
    $modules = @mysql_query('SELECT ModuleCode FROM modules');
      if (!
    $modules
      {
        exit(
    '<p>Unable to obtain module code list from the database.</p>');
      }
      
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p><img src="untitled.jpg" alt="ardF" width="318" height="210" align="right" /></p>
    <h1>&nbsp;</h1>
    <h1>
      <label></label>
      Add Event</h1>
    <p>Event Name:
      <input type="text" name="eventName" size="25"  />
    </p>

    <p>Event Type:
    <select name="eventType" size="1">
      <option selected value="">Select One</option>
      <option value="">---------</option>
      <option value="">Lecture</option>
        <option value="">Lab</option>
      <option value="">Tutorial</option>
      <option value="">Workshop</option>
      <option value="">Study group</option>
      <option value="">Other</option>
    </select></p>

    <p>Date:
    <input id="demo1" type="text" size="25">
    <a href="javascript:NewCal('demo1','ddmmyyyy')"><img src="cal.jpg" width="17" height="16" border="0" alt="Pick a date" /></a></p>

    <p>Time:
      <input type="text" name="time" size="25"  />
    </p>

    <p>Module Code:
    <select name="mid" size="1">
      <option selected value="">Select One</option>
      <option value="">---------</option>
    <?php
      
    while ($module mysql_fetch_array($modules)) 
      {
        echo 
    "<option>"$module['ModuleCode'] ."</option>\n";  
      }
    ?>
    </select></p>

    <input type="submit" value="SUBMIT" />
    </form>
    <?php }?>
    </body>
    </html>

  2. #2
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    please post your CREATE TABLE statement for the events table.
    Check out our new Industry News forum!
    Keep up-to-date with the latest SP news in the Community Crier

    I edit the SitePoint Podcast


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
  •