1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Update a MySQL Database

Discussion in 'PHP' started by rhoula, Oct 7, 2015.

  1. #1
    I have a mysql database with the following columns:

    id, f_name, l_name, cell, home, work, address, email

    I need to make a php form that will update the database.

    Thank you in advance for your help.
     
    Last edited: Oct 7, 2015
    rhoula, Oct 7, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    I'm assuming something is missing from the original post. Like the rest of the column names. And no, you do not need to make a "PHP form". You need to make a HTML form which is then processed by PHP. Create the form, try to create the PHP, then, if you get stuck, post the code you have, and we'll be happy to help.
     
    PoPSiCLe, Oct 7, 2015 IP
  3. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #3
    Edited thank you so much. I don't know how that happened.
     
    rhoula, Oct 7, 2015 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Before you even get started work through some php & mysql tutorials. Back in the day the example was to build a guestbook, there will be a modern equivalent. Its not going to be something you use but a learning tool. Your requirements are pretty simple but the moment you ask the public to fill in a form there are security checks that are needed, etc, and the tutorial will take you through those step by step.
     
    sarahk, Oct 7, 2015 IP
  5. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #5
    This is what I have:


    <?php
    require_once('db.php');
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // get value of id that sent from address bar
    $id=$_GET['id'];
    
    // Retrieve data from database
    $sql="SELECT * FROM $tbl_name WHERE id='$id'";
    $result=mysql_query($sql);
    $rows=mysql_fetch_array($result);
    ?>
    
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <form name="form1" method="post" action="update_ac.php">
    <td>
    <table width="100%" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td>&nbsp;</td>
    <td colspan="3"><strong>Update mysql Data</strong> </td>
    </tr>
    
    <tr>
    
    <tr><td align="center">&nbsp;</td>
    
    
    <tr><td align="right"><strong>f_name:</strong></td><td align="center"><input name="f_name" type="text" id="name" value="<? echo $rows['f_name']; ?>"></td></tr>
    
    <tr><td align="right"><strong>l_name:</strong></td><td align="center"><input name="l_name" type="text" id="name" value="<? echo $rows['l_name']; ?>"></td></tr>
    
    <tr><td align="right"><strong>cell:</strong></td><td align="center"><input name="cell" type="text" id="name" value="<? echo $rows['cell']; ?>"></td></tr>
    
    <tr><td align="right"><strong>home:</strong></td><td align="center"><input name="home" type="text" id="name" value="<? echo $rows['home']; ?>"></td></tr>
    
    <tr><td align="right"><strong>work:</strong></td><td align="center"><input name="work" type="text" id="name" value="<? echo $rows['work']; ?>"></td></tr>
    
    <tr><td align="right"><strong>address:</strong></td><td align="center"><input name="address" type="text" id="name" value="<? echo $rows['address']; ?>"></td></tr>
    
    <tr><td align="right"><strong>email:</strong></td><td align="center"><input name="email" type="text" id="name" value="<? echo $rows['email']; ?>"></td></tr>
    
    </strong></td><td align="center">
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    
    <td>
    <input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
    </td>
    
    <td align="center">
    <input type="submit" name="Submit" value="Submit">
    </td>
    
    <td>&nbsp;</td>
    </tr>
    
    </table>
    </td>
    </form>
    </tr>
    </table>
    
    <?php
    // close connection
    mysql_close();
    ?>
    PHP:
    and this is the update_ac.php


    <?php
    require_once('db.php');
    
    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    // update data in mysql database
    $sql="UPDATE $tbl_name SET f_name='$f_name', l_name='$l_name', cell='$cell', home='$home', work='$work', address='$address', email='$email', WHERE id='$id'";
    $result=mysql_query($sql);
    
    // if successfully updated.
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='display.php'>View result</a>";
    }
    
    else {
    echo "ERROR";
    }
    PHP:
     
    Last edited by a moderator: Oct 7, 2015
    rhoula, Oct 7, 2015 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    That code is utterly horrible, sorry to say. First, the PHP doesn't have any kind of security involved, nor does it use modern ways of communicating with the database (mysql_ has been deprecated for a decade, and SHOULD NOT BE USED).
    Then the HTML is... wow. You're using tables for something that isn't tables at all (there's never, ever a need to put a form on the page in the form of a table - there might be a reason to put forms INSIDE a table, but never ever format the HTML for the form inside a table - that's semantically wrong, and completely unnecessary).
    There is... so much wrong with that code, I don't really know where to start. You should toss it and start over. If noone else has stepped up, I might try to make a better version tomorrow (it's 3 AM here right now), but... seriously. Throw it away, start over.
     
    PoPSiCLe, Oct 7, 2015 IP
    KangBroke likes this.
  7. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #7
    Thank you for your input and your honesty. That is all I know. I'm not much of a coder. I just use other people's code and try to make it work.

    I think I'm too old to learn any programming :(

    Please let me know what to do.

    Thank you...
     
    rhoula, Oct 7, 2015 IP
  8. Keteki

    Keteki Active Member

    Messages:
    112
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #8
    You want insert or update??

    If update, below is the simple code

    <?php
    
    if(isset($_POST['ok'])) {
    
    define("DB_HOST", "localhost");
    define("DB_USERNAME", "database username");
    define("DB_PASSWORD", "database password");
    define("DB_DATABASE", "database name");
    $connect = mysql_connect(DB_HOST, DB_USERNAME, DB_PASSWORD) or die("Database Connection Error");
    mysql_select_db(DB_DATABASE) or ("Database Selection Error");
    
    $id = $_POST['id'];
    $f_name = $_POST['fname'];
    $l_name = $_POST['lname'];
    $cell = $_POST['cell'];
    $home = $_POST['home'];
    $work = $_POST['work'];
    $address = $_POST['address'];
    $email = $_POST['email'];
    
    $sql_update = "UPDATE tablegoeshere SET f_name='$f_name', l_name='$l_name', cell='$cell', home='$home', work='$work', address='$address', email='$email', WHERE id='$id'";
    @mysql_query($sql_update);
    @mysql_close();
    
    }
    
    ?>
    
    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
      <strong>ID:</strong> <input type="text" name="id"></input><br /><br />
      <strong>First Name:</strong> <input type="text" name="fname"></input><br /><br />
      <strong>Last name:</strong> <input type="text" name="lname"></input><br /><br />
      <strong>Cell:</strong> <input type="text" name="cell"></input><br /><br />
      <strong>Home:</strong> <input type="text" name="home"></input><br /><br />
      <strong>Work:</strong> <input type="text" name="work"></input><br /><br />
      <strong>Address:</strong> <input type="text" name="address"></input><br /><br />
      <strong>Email:</strong> <input type="text" name="email"></input><br /><br />
      <input type="submit" name="ok" value="Submit">
    </form>
    Code (markup):
    Change the database username, database password, database name. And also change the "tablegoeshere" in the update section to your desired table. Save it as "update.php" and run from a browser.
     
    Last edited: Oct 9, 2015
    Keteki, Oct 8, 2015 IP
    rhoula likes this.
  9. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #9
    Okay... if you're gonna help someone, at least try to teach them good practice? The form is malformed, you're still using mysql_ and overall that is not really better than what he had before. Defining the DB-information? Are you dense?

    Here's a proper suggestion, using PDO instead of mysql_
    I'm a little rusty in creating this barebones (I'm using my own classes to run it on my own pages), so there might be some typos and errors, but it's a start
    
    <?php
    
    // create a PDO-handler
       $dsn = 'mysql:host=localhost; dbname=databasename; connect_timeout=15';
       $user = 'dbusername';
       $password = 'dbpassword';
       $dbname = 'dbname';
       $setAttributes = array(
         PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, //sets the default fetch mode to associative
         PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', //remove or change this if your database is not set to UTF-8
       );
       try {
         $dbh = new PDO($dsn, $user, $password, $setAttributes);
       } catch(PDOException $e) {
         return $e->getMessage();
       }
    
    // handle the form submission
    if (isset($_POST)) {
       $update = $dbh->prepare("UPDATE tablename SET f_name = :f_name, l_name = :l_name, cell = :cell, home = :home, work = :work, address = :address, email = :email WHERE id = :id");
       try {
         $update->execute([':f_name'=>$_POST['fname'],':l_name'=>$_POST['l_name'],':cell'=>$_POST['cell'],':home'=>$_POST['home'],':work'=>$_POST['work'],':address'=>$_POST['address'],':email'=>$_POST['email'],':id'=>$_POST['id']]);
        echo 'The information was updated';
       } catch(PDOException $e) {
         echo $e->getMessage();
       }
    }
    
    echo '<form action="#" method="post">
             <fieldset>
               <legend>Update information</legend>
              <label for="fname">First name:</label><input type="text" name="fname" id="fname">
              <label for="lname">Last name:</label><input type="text" name="lname" id="lname">
              <label for="cell">Cell:</label><input type="number" name="cell" id="cell">
              <label for="home">Home:</label><input type="number" name="home" id="home">
              <label for="work">Work:</label><input type="number" name="work" id="work">
              <label for="address">Address:</label><input type="text" name="address" id="address">
              <label for="email">Email:</label><input type="email" name="email" id="email">
              <input type="submit" value="Update information">
           </fieldset>
           </form>';
    
    ?>
    
    PHP:
     
    Last edited: Oct 9, 2015
    PoPSiCLe, Oct 9, 2015 IP
    rhoula likes this.
  10. rhoula

    rhoula Well-Known Member

    Messages:
    875
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    145
    #10
    Thank you so much for your help. If there is anything you think I can do to help y'all please let me know.

    Have a great day.
     
    rhoula, Oct 9, 2015 IP