www.webdeveloper.com
Results 1 to 8 of 8

Thread: need help with mysqli

Hybrid View

  1. #1
    Join Date
    Apr 2013
    Posts
    61

    need help with mysqli

    What I'm trying to do is select once from the dropdown and use the selection
    to update the 'numbersdb' database via the submit button. This is current code. No message, no results. Thanks for your help.
    PHP Code:
    <?php
    $dbconnect 
    mysqli_connect('localhost','root','');
       
    mysqli_select_db($dbconnect'numbersdb') or die( "Unable to select database");
        
    $taxrate = (isset($_POST['submit'])) ? mysqli_real_escape_string($dbconnect$_POST['taxrate']) : '';
       
    $id = (isset($_POST['id'])) ? mysqli_real_escape_string($dbconnect$_POST['id']) : '';
        
    $result mysqli_query($dbconnect"SELECT * FROM numbdata");
        if (!empty(
    $_POST['update_taxrate'])) {
         
    $update mysqli_query($dbconnect"UPDATE numbdata SET taxrate = '$taxrate' WHERE id ='$id'");
         echo 
    "Taxrate has been set ...";
       }
     
    ?>
    HTML Code:
    <!DOCTYPE html> 
    <html>
       <head>
         <title>Select taxrate</title>
          <style type="text/css">
           body {
             background: #cff;
           }
           form {
             text-align: center;
           }
         </style>
       </head>
    <body>
       <form name="taxset" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
         <p><label>Select state/rate</label><p>
           <select name="taxrate">
             <option value="0.04000" selected>4% Alabama</option>
             <option value="0.05600">5.6% Arkansas</option>
           </select>
         </p>
         <!--<p><label>Update taxrate</label>
           <input type="text" name="update_taxrate">-->
         <p><input type="submit" name="submit" value="update"></p>
       </form>
    </body>
    </html>
    following is the print view of "numdata" database.

    Rows: 1
    id taxrate bank receiptno
    1 0.575 475.25 127

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    Just in case you're not seeing error messages, for now stick this at the top of your PHP code:
    PHP Code:
    <?php
    ini_set
    ('display_errors'true); // can change to false for production version
    error_reporting(E_ALL);
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2013
    Posts
    61
    Thanks for the input; the results the same. I think I'll like the mysqli - eventually.
    Right now I'm just scratching my....head. have a look?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    I'm tired, so may be some stupid errors in here, but try a bunch of defensive code to find out what's going on:
    PHP Code:
    <?php
    ini_set
    ('display_errors'true);
    error_reporting(E_ALL);
    $dbconnect mysqli_connect('localhost','root','');
    if(
    $dbconnect == false) {
        throw new 
    Exception("Connect failed: ".mysqli_connect_error());
    }
    if(
    mysqli_select_db($dbconnect'numbersdb') == false) {
        throw new 
    Exception("Select DB failed: ".mysqli_error($dbconnect));
    }
    $taxrate = (isset($_POST['submit'])) ? mysqli_real_escape_string($dbconnect$_POST['taxrate']) : '';
    $id = (isset($_POST['id'])) ? mysqli_real_escape_string($dbconnect$_POST['id']) : '';
    $result mysqli_query($dbconnect"SELECT * FROM numbdata");
    if (!empty(
    $_POST['update_taxrate'])) {
        
    $sql "UPDATE numbdata SET taxrate = '$taxrate' WHERE id ='$id'";
        
    $update mysqli_query($dbconnect$sql);
        if(
    $update == false) {
            throw new 
    Exception("Update query failed: ".mysqli_error($dbconnect).PHP_EOL.$sql);
        }
        echo 
    "Taxrate set for ".mysqli_affected_rows($dbconnect)." row(s).";
    }
    else {
        echo 
    "'update_taxrate' was not set, so no update attempted.";
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Apr 2013
    Posts
    61
    here is the output. I'm sorry to say, I know the message should make it obvious but how do I set 'update_taxrate' ?
    'update_taxrate' was not set, so no update attempted

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    You commented out the field that set the "update_taxrate" form field, so it's not be sent:
    HTML Code:
         <!--<p><label>Update taxrate</label>
           <input type="text" name="update_taxrate">-->
    If you don't need that field any more, then you need to check a different $_POST value for that if condition.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Apr 2013
    Posts
    61
    ok, that input box seems , to me, for entering an argument. I want the value selected from the menu to be the argument.
    Current code gives the following:
    'update_taxrate' was not set, so no update attempted.
    Proceeding gives the following:
    Taxrate set for 0 rows

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    And where is $_POST['id'] being set?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

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

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