Trying to learn.

Discussion in 'PHP' started by William, May 9, 2006.

  1. #1
    I Am trying to learn PHP and trying to make a very simple guestbook but it seems I am doing something wrong and I can't figure out what.

    The problem is that it isn't inserting the entries into the DB and I get an error when trying to display entries a Manually entered in phpmyadmin. I would really appriciat if someone could take a quick look at the code and se if they se what I have done wrong.

    I am using the following code for the file.

     <html>
    <head>
    <title>Guestbook</title>
    </head>
    
    <body>
    <h1>Make a entry </h1>
    <form action="guestentry.php" method="POST">
    
    		<p>Name: <br />
    		<input type="text" name="fornamn" size="30" /></p>
    
    		<p>Email: <br />
    		<input type="text" name="email" size="30" /></p>
    		
      <p>Text: <br />
        <textarea name="entry" cols="100" rows="5" wrap="VIRTUAL"></textarea>	
      </p>
    		
    		<p>refered by: <br />
    		<input type="text" name="ref" size="30" /></p>
    
    		<p><input type="reset" value="Clear!" /> 
    		<input type="submit" value="Send!" /></p>
    
    </form>
    
    <h1>Guestbook entires</h1>
    <?php
    $link = mysqli_connect ('localhost','root','','guest');
    if (!$link) {
      echo mysql_connect_error($link);
    }
    
    $query = mysqli_query($link, "SELECT * FROM guest LIMIT 10");
    $guest = mysqli_fetch_assoc($query);
    
    while ($row = mysqli_fetch_assoc($query))
    {
      echo '<p>Name:<br />';
      echo $row['user_name'] . '</p>';
      
        echo '<p>Email:<br />';
      echo $row['user_email'] . '</p>';
    
      echo '<p>text:<br />';
    
      echo $row['entry_text'] . '</p>';
      
       echo '<p>refered by:<br />';
    
      echo $row['ref'] . '</p>';
    
    }
     
    mysqli_close($link);
    
    ?>
    </body>
    </html>
    
    Code (markup):
    and the following code in "guestentry.php" to insert entries into the Mysql DB

    
    <?php
    $link = mysqli_connect ('localhost','root','','guest');
    if (!$link) {
      echo mysqli_connect_error($link);
    }
    
    $name = $_POST['fornamn'];
    $email = $_POST['email'];
    $text = $_POST['entry'];
    $ref = $_POST['ref'];
    
    $sql = "INSERT INTO guest (user_name, user_email, entry_text, user_ref) VALUES ('$name, $email, $text, $ref')";
    
    
    
    mysqli_query($link, $sql);
    mysqli_close($link);
    
    
    echo 'Thanks for posting';
    ?>
    
    Code (markup):
     
    William, May 9, 2006 IP
  2. Danny

    Danny Active Member

    Messages:
    732
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #2
    why are you using "mysqli_connect"?

    Also what is the error?

    On all of your insert scripts change them to

    )or die(mysql_error());

    and they will spit out an error if they dont insert. I use this when debugging scripts and it works well. I later wrap them in if statements though.

    Danny
     
    Danny, May 9, 2006 IP
  3. William

    William Well-Known Member

    Messages:
    1,310
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    140
    #3
    I am using "mysqli_connect" because my book teaches me to do it that way.

    The insert data procedure don't provide me with any error. I get

    Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\MKFC\index.php on line 36
    and same on line 38 when I try to display data.

    How do you mean change the insert scripts to
    )or die(mysql_error());

    ???
     
    William, May 9, 2006 IP
  4. Danny

    Danny Active Member

    Messages:
    732
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #4
    change
    mysqli_query($link, $sql);

    to

    mysqli_query($link, $sql)or die(mysql_error());
     
    Danny, May 9, 2006 IP
  5. William

    William Well-Known Member

    Messages:
    1,310
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    140
    #5
    OK. I have tried that.

    It didn't provide any error but prevented the echo 'Thanks for posting'; text to be shown when i made an entry.

    Thanks for helping me figur this out.
     
    William, May 9, 2006 IP
  6. Danny

    Danny Active Member

    Messages:
    732
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #6
    that is odd If it isnt erroring it should be going into the database.

    Try mysql_connect instead of mysqli_connect
     
    Danny, May 9, 2006 IP
  7. drewbe121212

    drewbe121212 Well-Known Member

    Messages:
    733
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    125
    #7
    mysqli_connect

    As far as I can tell, mysqli_connect is used just for gathering information about the database/connection.

    Use mysql_connect() instead.

    Change:
    $link = mysqli_connect ('localhost','root','','guest');

    to
    $link = mysql_connect ('localhost','root','','guest');
     
    drewbe121212, May 10, 2006 IP
  8. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    mysqli is the 'improved' version of the mysql library.

    From http://dev.mysql.com/downloads/connector/php/ :
    The main features of the mysqli extension are:
    * access to all MySQL 4.1/5.0 features
    * a procedural interface that is similar to the mysql extensions
    * an object-oriented interface that is easier to extend than the procedural interface


    mysqli is required for connecting to MySQL 4.1 and above.
     
    TwistMyArm, May 10, 2006 IP