1

I am currently making an edit page, where you would click on a link to edit a specific user, it would bring you to edit.php?id= and it would show all their info they currently have set and what they don't have set. In which you could edit it to your liking then save it. I'm sort of getting used to mysqli as I recently transitioned over from mysql. When i load edit.php page up i get these errors in the text boxes: <br /><b>Notice</b>: Undefined variable: user in <b>C:\xampp\htdocs\edit.php</b> on line <b>13</b><br /> and the line changing depending on the text box.

Edit.php

<?php include_once('header.php'); ?>
    <?php include_once('classes/check.class.php'); ?>
    <?php include_once('config/db.php'); ?>


    <?php if( protectThis("1, 2, 3") ) : ?>

    <div id="newform">
        <form method="post" role="form">
            <div class="form-group">
                <label for="user">User</label>
                <input name="user" type="text" class="form-control" id="user" placeholder="Username" value="<?php echo $user; ?>"> 
            </div>
            <div class="form-group">
                <label for="position">Position</label>
                <input name="position" type="text" class="form-control" id="position" placeholder="GG" value="<?php echo $pos; ?>">
            </div>
            <div class="form-group">
                <label for="date">Date</label>
                <input name="date" type="text" class="form-control" id="date" placeholder="<?php echo date('d M y'); ?>" value="<?php echo $date; ?>">
            </div>
            <div class="form-group">
                <label for="tag">Tag</label>
                <input name="tag" type="text" class="form-control" id="tag" placeholder="blue" value="<?php echo $tag; ?>">
            </div>
            <div class="form-group">
                <label for="adt">ADT</label>
                <input name="adt" type="text" class="form-control" id="adt" placeholder="BINGO" value="<?php echo $adt; ?>">
            </div>
            <div class="form-group">
                <label for="exp">EXP</label>
                <input name="exp" type="text" class="form-control" id="exp" placeholder="420" value="<?php echo $exp; ?>">
            </div>
            <div class="form-group">
                <label for="reg">Regiment</label>
                <input name="reg" type="text" class="form-control" id="reg" placeholder="P" value="<?php echo $reg; ?>">
            </div>
            <div class="form-group">
                <label for="notes">Notes</label>
                <input name="notes" type="text" class="form-control" id="notes" placeholder="away" value="<?php echo $note; ?>">
            </div>
            <center>
            <button name="submit" value="submit" type="submit" class="btn btn-default">Submit</button>
            </center>
        </form>
    </div>

    <?php

    //Submitting the information from the form----------------------------------

    if(isset($_POST['submit'])){
    //Values to be inserted into the DB
    $user = $_POST['user'];
    $date = $_POST['date'];
    $tag  = $_POST['tag'];
    $pos  = $_POST['position'];
    $adt  = $_POST['adt'];
    $exp  = $_POST['exp'];
    $reg  = $_POST['reg'];
    $note = $_POST['notes'];

    //Preparing the statement
    $query = "INSERT INTO players (user, date, tag, position, adt, exp, reg, notes) VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
    $statement = $mysqli->prepare($query);

    //Binding Parameters for markers, where (s = string, i = integer, d = double, b = blob)
    $statement->bind_param('sssssiss', $user, $date, $tag, $pos, $adt, $exp, $reg, $note);

    //Execution
    if($statement->execute()){
        header('Location: home.php');
    }else{
        die('Error : ('.$mysqli->errno .') '. $mysqli->error);
    }
    $statement->close();
    }

    // Grabbing the information for the form----------------------------------------

    if(isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0){

    $ids = $_GET['id'];

    $result = $mysqli->query("SELECT * FROM players WHERE id = $ids");

    $row = $result->fetch_array();

    if($row){
    $id   = $row['id'];
    $user = $row['user'];
    $date = $row['date'];
    $tag  = $row['tag'];
    $pos  = $row['position'];
    $adt  = $row['adt'];
    $exp  = $row['exp'];
    $reg  = $row['reg'];
    $note = $row['notes'];
    }else{}

    // Frees the memory associated with a result
    $result->free();

    }




    ?>

    <?php endif; ?>


    <?php include_once('footer.php'); ?>
Riley
  • 153
  • 3
  • 3
  • 13
  • use a ternary operator – Funk Forty Niner Aug 07 '15 at 01:38
  • @Fred-ii- In replacement of my current echo? If so could you give me an example? I can't wrap my head around how I would go about it. – Riley Aug 07 '15 at 01:42
  • 1
    `$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];` http://php.net/manual/en/language.operators.comparison.php – Funk Forty Niner Aug 07 '15 at 01:45
  • 2
    @Riley, simple, place your code -> "Grabbing the information for the form" section above HTML - vars aren't defined, before query. That's it. And check with: http://php.net/manual/en/function.isset.php – sinisake Aug 07 '15 at 01:46
  • And, ternary operator isn't answer here - you can use it, or not... it is more stylistic choice. :) – sinisake Aug 07 '15 at 01:50
  • @nevermind I beg to differ. I feel that the OP wants to echo values in inputs after submitting their form. It's not a stylistic issue, but more as practicality. OP can decide, but I feel that a ternary in values is far more effective than having to write a whole bunch of `if/else`'s. The above was an example. – Funk Forty Niner Aug 07 '15 at 01:53
  • Here's the actual Riley, pulled from my own stuff `value=""` replace as necessary and do the same for the others, *enjoy* – Funk Forty Niner Aug 07 '15 at 01:56
  • even better `value=""` against XSS injection. – Funk Forty Niner Aug 07 '15 at 01:57

0 Answers0