www.webdeveloper.com
Results 1 to 2 of 2

Thread: Database reading and adding

  1. #1
    Join Date
    Apr 2013
    Posts
    2

    Database reading and adding

    I am trying to add and read email from a database however I have some problems:

    1-For adding well it doesn't work: my html page wants for the user to enter TO, SUBJECT and a MESSAGE and the database have: emaiID, emailSubject, emailText, emailDataTime, FK_usernameFrom, FK_usernameTo.
    2-For the list of email it works but with some problemas: I want to put it in a table but the titles appears every new list (information).

    Code:
    function addEmail($email, $user, $subject )
    {
    	$conn = openDatabase();
    	
    	$sql = "insert into emails set FK_usernameTo='$user', emailText='$email',  emailSubject='$subject', emailDateTime=now();";
    	
    	$result = mysqli_query($conn, $sql);
    	
    	if ($result)
    		$message="Email Sent";
    	else
    		$message="Error sending email";
    	
    	closeDatabase($conn);
    	
    	return $message;
    } 
     
     
    function getEmail($email)
    {
    	  $resultTable = "<table border='1'>";
      
      $conn = openDatabase();
      
      $sql = "select * from emails where FK_usernameFrom='$email';";
      
      $result = mysqli_query($conn, $sql);
      
      	$formNum=0;
      
      if ($result)
      {
       while ($row = mysqli_fetch_array($result))
       {
        $from=$row['FK_usernameFrom'];
        $emailSubject=$row['emailSubject'];
        $emailDate=$row['emailDateTime'];
        $message=$row['emailText'];
        
        $resultTable .= "<tr><th>FROM</th><th>SUBJECT</th><th>RECEIVED</th><th>DELETED</th></tr><tr><td>$from</td><td><a href='notesOps.php>'$emailSubject</a></td><td>$emailDate</td><td><a href='emailOps.php?delete=$emailID'>Delete<a></td></tr>";
    	
    	$formNum++;
       
       }
      }
      mysqli_free_result($result);
      closeDatabase($conn);
      
      $resultTable.= "</table>";
      
      return $resultTable;
    }

  2. #2
    Join Date
    Feb 2013
    Posts
    64
    This might help you out with getting to add the email to the database. I just have to say I have no idea if this code works or not, but if it doesn't it shouldn't be to far off. Plus the connection part to MySQL I believe you can figure out. I would also suggest visiting php.net manual and going over that for additional help.

    PHP Code:
    <?php 
    function addEmail($email$user$subject )
    {
        
    $link mysqli_connect("localhost""root""password""yourDatabase");
        
        
    /* check connection */
        
    if (!$link) {
           
    printf("Connect failed: %s\n"mysqli_connect_error());
           exit();
        }
        
        
    $stmt mysqli_prepare($link"INSERT INTO emails VALUES (?, ?, ?, now())"); 
        
    /* The above  is you query statement and since you are using myqli datetime
        you don't have to bind the variable and make sure you
        have the datetime in this  format 0000-00-00 00:00:00 */
        
        
    mysqli_stmt_bind_param($stmt'sss'$user$email$subject); // These bind the variables to the query statement
         
        /* execute prepared statement */
        
    mysqli_stmt_execute($stmt);       
        
        
    $result mysqli_stmt_affected_rows($stmt);
       
    /* if successful the result should show the row being inserted */ 
        
    if ($result)
            
    $message="Email Sent";
        else
           
    $message="Error sending email";    
           
        
    /* close statement and connection */
        
    mysqli_stmt_close($stmt);
        
          
    /* close connection */
        
    mysqli_close($link);        
            
        return 
    $message;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles