0

Found much information on the forum. But im stuck at a certain point.

What did i do:

First i load mysql database in a html table and hit the edit button of a row which executes update.php:

<div class="container-fluid">
     <table id="table_id" class="cell-border order-column row-border hover compact"  width="100%">
        <thead>
            <tr>
            <th width="2%">ID</th>
            <th width="6%">Debiteur</th>
            <th width="10%">Klantnaam</th>
            <th width="3%">Aantal Pallets</th>
            <th width="3%">Totaal Gewicht</th>
            <th width="30%">PB Nummers</th>
            <th width="10%">Toevoegdatum</th>
            <th width="10%">Edit</th>
        </thead>
        <tbody>
            <!-- Fetch from db -->
            <!-- Connect to db-->>
         <?php
        $conn = mysqli_connect("localhost","root","","export") or die("Error in Connection");

        $query = mysqli_query($conn, "SELECT exportid, deb_nmr, cost_name, numb_pal, tot_weight, pb_s, date FROM export_tabel");

        while ($result = mysqli_fetch_array($query)) {
            echo "<tr>
                <td>".$result['exportid']."</td>
                <td>".$result['deb_nmr']."</td>
                <td>".$result['cost_name']."</td>
                <td>".$result['numb_pal']."</td>
                <td>".$result['tot_weight']."</td>
                <td>".$result['pb_s']."</td>
                <td>".$result['date']."</td>
                <td><a href=\"update.php?id=$result[exportid]\"><button type='button' class='btn btn-success btn-xs glyphicon glyphicon-edit'></button></a>
            </tr>";
        }
?>

In update.php i try to load the selected table row in a form. This is where i get stuck.

   <?php

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}


$id=$_GET['id'];
$sql = mysql_query("SELECT exportid, deb_nmr, cost_name, numb_pal, tot_weight, pb_s, date, statusid FROM export_tabel WHERE exportid='$id'");
while ($row = mysql_fetch_array($sql)) {
    $i_d = $row['exportid'];
    $deb = $row['deb_nmr'];
    $name = $row['cost_name'];
    $pal = $row['numb_pal'];
    $weight = $row['tot_weight'];
    $pb = $row['pb_s'];
    $d_ate = $row['date'];
    $status = $row['statusid'];
}
?>

      <form action="updaterow.php" method="post">
         <div class="form-group">
          <label for="email">id:</label>
          <input type="text" class="form-control" name="id" id="id" value="<?php echo $i_d;?>">
        </div>
        <div class="form-group">
          <label for="email">Debiteur:</label>
          <input type="text" class="form-control" name="deb_nmr" id="debiteur" value="<?php echo $deb;?>"/>
        </div>
        <div class="form-group">
          <label for="pwd">Klantnaam:</label>
          <input type="text" class="form-control" name="cost_name" id="costname" value="<?php echo $name;?>"/>
        </div>
        <div class="form-group">
          <label for="pwd">Aantal Pallets:</label>
          <input type="text" class="form-control" name="numb_pal" id="numbpal" value="<?php echo $pal;?>"/>
        </div>
        <div class="form-group">
          <label for="pwd">Totaal gewicht:</label>
          <input type="text" class="form-control" name="tot_weight" id="totweight" value="<?php echo $weight;?>"/>
        </div>
        <div class="form-group">
          <label for="pwd">PB nummers:</label>
          <input type="text" class="form-control" name="pb_s" id="pbs" value="<?php echo $pb;?>"/>
        </div>
           <div class="form-group">
          <label for="email">date:</label>
          <input type="text" class="form-control" name="date" id="date" value="<?php echo $d_ate;?>"/>
        </div>
        <div class="form-group">
          <label for="pwd">statusid:</label>
          <input type="text" class="form-control" name="status" id="status" value="<?php echo $status;?>"/>
        </div>
        <input type="submit" value="update" >
       </form>

i am getting the following error code in the form fields:

<br /><b>Notice</b>:  Undefined variable: i_d in <b>/Applications/XAMPP/xamppfiles/htdocs/export/update.php</b> on line <b>31</b><br />

and

mysql_fetch_array() expects parameter 1 to be resource, object given in /Applications/XAMPP/xamppfiles/htdocs/export/update.php on line 10

Hope somebody can help me in the right direction:)

Rubinjo13
  • 65
  • 2
  • 11
  • I think you are getting this error because your sql query not return any result. If query no result, your while loop not iterate and variables will not be declared – Yuriy Korman May 07 '17 at 12:53
  • 1
    Why does you second script use the `MYSQL_` extension when your first code uses `MYSQLI_` if your connetion script is connection to `MYSQLI_` your second script should be generating lots of errors – RiggsFolly May 07 '17 at 13:03
  • Although I dont actually see an attempt to connect to any database in the second script – RiggsFolly May 07 '17 at 13:05
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly May 07 '17 at 13:05
  • thats a good question RiggsFolly. Found that example on the internet and used it. So that would be the fault? – Rubinjo13 May 07 '17 at 13:06
  • Finding help in other peoples code, is fine. But you have to **read and understand** what you are copying before actually trying to use it – RiggsFolly May 07 '17 at 13:08
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly May 07 '17 at 13:09
  • You are right RiggsFolly. Changed the Mysql to mysqli and it works now. – Rubinjo13 May 07 '17 at 13:18

0 Answers0