Jump to content

help w/mysqli


ataloss

Recommended Posts

Hi fellows. I'm trying mysqli for the first time and failing miserably. I'm trying to select a value from

a dropdown and update a field (taxrate) in a database (numbersdb)..The nessage I get is

"update_taxrate' was not set, so no update attempted".Thanks for any help.

<?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'])) {
// $id=$_POST['id'];
            $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.";
        }
      ?>
<!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>


      

Link to comment
Share on other sites

thanks guys for your observations.  firstly, the "id" is there, I just don't know where to put it.  the input field..please do corrrect me where I'm wrong, I'm thinking that input code was for entering arguments and wouldn't that defeat the purpose of the dropdown menu? I haven't quite caught on to this yet.  thanks for further help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.