what is wrong with this code

Discussion in 'PHP' started by dougvcd, May 14, 2007.

  1. #1
    this is my insert code
    <?php
    $con = mysql_connect("this left for security reasons");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("my database", $con);
    
    $sql="INSERT INTO exchange (name, username, password, email, parkname, parkloc, caravan)
    VALUES
    ('$_POST[name]','$_POST[username]','$_POST[password]','$_POST[email]','$_post[parkname]','$_post[parkloc]','$_post[caravan]')";
    
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
    mysql_close($con)
    ?>
    PHP:
    and this is the form
    <html>
    
    <body>
    <form action="insert.php" method="post">
    
          name: 
          <input type="text" name="name" />
          username: 
          <input type="text" name="username" />
          password:
          <input type="text" name="password" />
          email: 
          <input type="text" name="email" />
          park name:
          <input type="text" name="parkname" />
          park location:
          <input type="text" name="parkloc" />
          caravan details:
          <input type="text" name="caravan" />
          <input type="submit" />
    </form>
    
    </body>
    </html>
    HTML:
    this is my query code
    ]<?php
    $con = mysql_connect("");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("", $con);
    
    $result = mysql_query("SELECT * FROM exchange");
    
    echo "<table border='1'>
    <tr>
    <th>parkname</th>
    <th>parkloc</th>
    <th>caravan
    </tr>";while($row = mysql_fetch_array($result))
      {
      echo "<tr>";
      echo "<td>" . $row['parkname'] . "</td>";
      echo "<td>" . $row['parkloc'] . "</td>";
      echo "<td>" . $row['caravan'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";mysql_close($con);]?>
    
    PHP:
    all that shows in the database is the first 4 fields
    cheers
    Doug
     
    dougvcd, May 14, 2007 IP
  2. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #2
    1.
    - php variable names are case sensitive so if you want to use $_POST superglobal write $_POST not $_post
    2. mysql_close($con);]?>
    - remove "']"

    Did you ran it?
     
    gibex, May 14, 2007 IP
  3. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    big thanks for that worked a treat
    one other question i ment to ask is how do i get it to move to another page after the record is add
    at the moment comes up with record added and stays there
    can i add some thing like continue then go to home page

    echo "1 record added";mysql_close($con)?>
    PHP:
    once again big thanks
    Doug
     
    dougvcd, May 14, 2007 IP
  4. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #4
    you can use header function to redirect after adding the records.you can use it in this way: header("Location:pagename");
    at it at the bottom of your PHP script for adding the records in DB.just modify the pagename.
     
    coderbari, May 14, 2007 IP
  5. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    header("Location: index.php");
    
    PHP:
     
    CodyRo, May 14, 2007 IP
  6. rgchris

    rgchris Peon

    Messages:
    187
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just make sure not to echo out any data before you call the other URL. It'll cause an error.
     
    rgchris, May 14, 2007 IP
  7. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ob_start()
    ob_end_flush()
     
    CodyRo, May 14, 2007 IP
  8. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks to all
    now sorted
    and on to the next problem
    cheers
    Doug
     
    dougvcd, May 15, 2007 IP
  9. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #9
    glad to help :)
     
    gibex, May 15, 2007 IP