Need Help With MySQL Insert & Retrieval

Discussion in 'PHP' started by evanc93, Oct 11, 2010.

  1. #1
    Hi everyone,

    I need to some with something I am working on. I have an administrative section of my site and I am working on a page

    (settings.php). The purpose of this page is so that the admin can can some basic information about their website; such

    as the site title.

    So I created a database table, site_settings, that has 3 rows {site_name, bonus_points, ref_bonus}

    And on my Settings.php page I have a form with 3 fields {title, bonus, rbonus}. I have provided the html code of this

    form below.

    
    <form action="" method="post">
          <table>
            <tr>
              <td>Site Name:</td>
                <td><input type="text" name="title" value="" style="width:300px;" /> <img src="images/info.png" alt="Name 
    
    Of Your Site" title="Name Of Your Site" width="16" height="16" /></td>
            </tr>
            <tr>
              <td>Bonus Points:</td>
                <td><input type="text" name="bonus" value="" style="width:300px;" /> <img src="images/info.png" 
    
    alt="Points Awarded To New Members" title="Points Awarded To New Members" width="16" height="16" /></td>
            </tr>
            <tr>
              <td>Referral Bonus:</td>
                <td><input type="text" name="rbonus" value="" style="width:300px;" /> <img src="images/info.png" 
    
    alt="Points Awarded When Referral Completes An Offer" title="Points Awarded When Referral Completes An Offer" 
    
    width="16" height="16" /></td>
            </tr>
            <tr>
              <td><input type="submit" name="update" value="Save" /></td>
            </tr>
           </table>
    </form>
    
    Code (markup):
    What I want this form to do is like so. I want what is entered into the three fields to be saved to the database table once the form is submitted. I also want the values of the text inputs to be the same as those in the database table.

    Any/all help is appreciated!
    Thanks in advance.
     
    evanc93, Oct 11, 2010 IP
  2. max2010

    max2010 Greenhorn

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #2
    First, you should add a target file in the form:
    <form action="myscript.php" method="post">
    ...

    then, create a php file with the name you put as form action (myscript.php)
    and write something like this inside:


    // You should first define the DB parameters with your username and pw:

    $DBhost=''; // usually 'localhost'
    $DBuser=''; // username to access mysql
    $DBpass=''; // mysql password
    $DBname=''; // DB name (where you created your table)

    // then you should make safe the POST inputs

    if (get_magic_quotes_gpc()) {
    $site_name = stripslashes($_POST['title']);
    }
    else {
    $site_name = $_POST['title'];
    }

    $site_name = mysql_real_escape_string($site_name);

    $bonus_points=(int)$_POST['bonus'];
    $ref_bonus=(int)$_POST['rbonus'];

    // if bonus and ref bonus points can have decimals just change (int) with (float)

    // creating the sql query
    $mysql_query = "INSERT INTO `site_settings` (`site_name`, `bonus_points`, `ref_bonus`) VALUES ('".$site_name."', '".$bonus_points."', '".$ref_bonus."');";

    // connecting DB
    $db = mysql_connect ($DBhost, $DBuser, $DBpass)
    or die ("Error connecting MySQL");

    // selecting db
    mysql_select_db($DBname, $db)
    or die ("Error selecting MySQL ".$DBname);

    $insert = mysql_query($mysql_query, $db)
    or die ("Error inserting to MySQL");
     
    max2010, Oct 11, 2010 IP