mysql update getting input values from other table

Discussion in 'PHP' started by MrPJH, Oct 23, 2014.

  1. #1
    i have a settings table for my php script
    which i want to update
    settings table contains
    id | name | value | type
    AI |baseurl|localhost|0-1

    have loaded them on input with this
    <?php
              $query = "SELECT * FROM settings";
              $result = mysql_query($query) or die(mysql_error());
              while($row = mysql_fetch_array($result)) {
              $setting_name = $row['setting_name'];
              $setting_value = $row['setting_value'];
              ?>
    
    <input type = "text" name="<?= $setting_name ?>" value = "<?= $setting_name ?>"
    }
    
    PHP:
    it displayes all inputs as i want
    but on changing or resubmitting it gives error

    here is my update query
    
              if (isset($_POST["submit"])) {
                  $set1 = $_POST[$setting_name]; //LINE 31
                  $set2 = $_POST[$setting_value]; // LINE 32
    
              $set_change = "UPDATE settings SET setting_value = '$set2' WHERE setting_name = '$set1'";
              $res_change = mysql_query($set_change) or die(mysql_error());
             
              }                  
                ?>
    
    PHP:
    it gives following errors
    
    Notice: Undefined variable: setting_name in C:\xampp\htdocs\399\panel\settings.php on line 30
    
    Notice: Undefined index: in C:\xampp\htdocs\399\panel\settings.php on line 30
    
    Notice: Undefined variable: setting_value in C:\xampp\htdocs\399\panel\settings.php on line 31
    
    Notice: Undefined index: in C:\xampp\htdocs\399\panel\settings.php on line 31
    
    HTML:

     
    MrPJH, Oct 23, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Well, I'm assuming that you're trying to use a variable that isn't set.
    Follow me on this: You're loading the variables from the database and then outputting them to a form. Which is fine. However, are you sure you want the value to be ['name'] and not the actual ['value']? As it is now, it's redundant to add the value to the output from the query.
    But, when you're trying to update, the variables aren't set anymore - you're reloading the page on submit, and the variables aren't set. What you need to do is set those variables outside the loops, at the beginning of the page.
    Apart from that, this is highly insecure - you're using mysql_, which has been deprecated for quite a while, and you're not validating your input at all - I suggest you look into using mysqli_ or PDO, and prepared queries.
    Here's an example, based on PDO:
    
    <?php
        //assumes you have a database-object created called $dbh
    
        $query = $dbh->query("SELECT * FROM settings");
    
            while($row = $query->fetch(PDO::FETCH_ASSOC)) {
                $setting_name = $row['setting_name'];
                $setting_value = $row['setting_value'];
    
                echo '<input type="text" name="'.$setting_name.'" value="'.$setting_value.'">';
            }
    
            if (isset($_POST["submit"])) {
                $set1 = $_POST[$setting_name]; //this might be set, but it will always be set to the last value pulled from the database
                $set2 = $_POST[$setting_value]; //same for this
                // I suggest doing it like this, if you don't want to write the name specifically:
                $dbh->beginTransaction(); //this starts a set of queries to run 
                foreach ($_POST as $key => $value) {
                    $updatename = $key;
                    $updatevalue = $value;
                    $change_{$key} = $dbh->prepare("UPDATE settings SET setting_value = :updatevalue WHERE setting_name = :updatename");
                    $change_{$key}->execute(array(':updatevalue'=>$updatevalue,':updatename'=>$updatename));
                }
                $dbh->commit(); //executes the prepared queries        
              }                  
    ?>
    
    PHP:
    Also, you should avoid shortcuts like <?= for echoing, and also, don't drop out and go in on php all the time - just echo out stuff directly.
     
    PoPSiCLe, Oct 24, 2014 IP
  3. MrPJH

    MrPJH Well-Known Member

    Messages:
    1,066
    Likes Received:
    7
    Best Answers:
    1
    Trophy Points:
    155
    #3
    this is an in house application not going to be live forever so i am not worried about mysql deprecated or security measure are not set
    as all of the queries on other pages are with mysql i have no option for any thing other

    about values what i want is
    input forms load values from setting table so if a user do not change a value it will remain as it is and only changed inputs get updated

    this is for defined things
    some use config.php and define like $baseurl = "localhost/test/" and call it as they need
    but i am storing them into table
    for example if you go to digitalpoint.com/account/contact-details you will see your details
    if you dont touch an input it stays same but you chnage only changed inputs are getting saved to your profile and non touched remain as they were
     
    MrPJH, Oct 26, 2014 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,895
    Likes Received:
    4,553
    Best Answers:
    123
    Trophy Points:
    665
    #4
    were you able to use any of @PoPSiCLe's suggestions?

    What errors are you getting now?
     
    sarahk, Oct 26, 2014 IP