www.webdeveloper.com
Results 1 to 5 of 5

Thread: How can I update a SQL database table using PHP

  1. #1
    Join Date
    Jan 2013
    Posts
    4

    How can I update a SQL database table using PHP

    Hello, please I need help with this, i'm new to PHP I've been trying to figure out for over a week, an I've still found no answers.

    I want to send out emails to the subscribers of my eBook. I want to include a link in the email, so when the link is clicked, the subscribers database will be updated.

    When this link is clicked, the subscribers are redirected to my homepage.

    So i'm having trouble with everything, from start to finish.

    This is the code I've written so far:

    Code:
    <body>
     
    <a href="" target="_blank">Click Here To Say YES!</a>
     
    <?php
     
    mysql_connect("localhost", "root", "");
    mysql_select_db("rmn");
     
    $sql = mysql_query("UPDATE subscribe SET confirmaton ='YES' WHERE email= [email]");
     
    
     
    
    ?>
    </body> 
     
    
    Thank you very much!

  2. #2
    Join Date
    Feb 2013
    Posts
    110
    Mysql has been depreciated I would either us Mysqli or PDO for security reasons (I am using mysqli).

    Here's my connections screen to the database

    PHP Code:
    <?php
    $db 
    = new mysqli("localhost""root""******""bhr_db");
    /* check connection */
    if (mysqli_connect_errno()) {
        
    printf("Connect failed: %s\n"mysqli_connect_error());
        exit();
    }
    Here's how I insert to the database (I'll explain a few things after the code).

    PHP Code:
        function insert_content($page$user_date) {
                  global 
    $db;
               
              
    $stmt $db->prepare("INSERT INTO pages (blog_name, content, new_blog_date, update_date) VALUES (?,?,?,?)");
              
    $stmt->bind_param('ssss'$page['blog_name'], $page['content'], $user_date$user_date);                    
              
              
    /* execute prepared statement */
              
    $stmt->execute();                   
              
              
    /* close statement and connection */
              
    $stmt->close();        
        } 
    I use prepare statements (prepare & bind_param) and this is in OPP format....go to PHP dot net for a procedural example if it will make more sense to you. Notice how the ? marks corresponds to all the fieldnames in the database in the prepare statement. Then on the next line it binds it to your prepare statement. The s (stands for string, i for integer...etc.) corresponds to how many variable you will be binding. Finally it is executed withe execute statement.

    Doing something like this example will hopefully help you.


    John

  3. #3
    Join Date
    Feb 2013
    Posts
    110
    I meant to write OOP ....

  4. #4
    Join Date
    Feb 2013
    Posts
    110
    Updating a database even even easier...

    PHP Code:
    function update_content($id$page$user_date) {
            
            global 
    $db;
                
            
    // define sensible test values
            
            // create a prepared statement
            
    $update_stmt $db->prepare('
                UPDATE
                   pages
                SET
                   blog_name =?,               
                   content = ?,
                   update_date = ?
                WHERE
                   id = ?
            '
    );
            
            
    /* bind parameters for markers */
            
    $update_stmt->bind_param('ssss'$page['blog_name'], $page['content'], $user_date$id);
            
            
    /* execute query */
            
    $update_stmt->execute();
            
            
    /* close statement */
            
    $update_stmt->close();
        } 

  5. #5
    Join Date
    Feb 2013
    Posts
    110
    For some strange reason I re-read your question I don't think I answer your question properly.

    Well, semi-partially.

    I would do a user form instead....

    something like this....

    HTML Code:
    <form action="example.php?page" method="post">
                      Username:<br />
                      <input type="text" name="username" value="" />
                      <br />
                      <br />
                      E-Mail:<br />
                      <input type="text" name="email" value="" />
                      <br />
                      <br />
                      Password:<br />
                      <input type="password" name="password" value="" />
                      <br />
                      <br />
                      <input type="submit" value="Register" />
                    </form>
    then just have your code in the php file....

    but to do it the way you want I'm sure someone else can answer you that part of the question...sorry about that....

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