0

I'm trying to load data from a database table within my page.

this that I report below is a demonstrative example, to explain how I would like to realize it ... actually we are talking about 100 fields of the database and 1000 and passes rows of code of the table ...

<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$idCantiere = $_GET['idCantiere'];

$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";



    $result = mysqli_query($sql1, $conn);
    
   
    $details = mysqli_fetch_array($result);




    $savedNomeCantiere = $details["nomeCantiere"];
    $savedCodiceCommessa = $details["codiceCommessa"];
    $savedIndirizzoCantiere = $details["indirizzoCantiere"];

var_dump($details);


$result1 = $conn->query($sql1);

echo($nomeCantiere);

?>


<html>
<head>
</head>
<body>


   
        <table width="300" border="0">
          <tr>
            <td>Name</td>
            <td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
          </tr>
          <tr>
            <td>Cost</td>
            <td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
          </tr>
           <tr>
            <td>Cost</td>
            <td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
          </tr>
          
        </table>

<br/>

   
</body>
</html>

I tried with this type of upload that is putting an "echo" inside the "value" of the textbox, but it does not work .

This line is used to derive the "id" through page redirection.

$idCantiere = $_GET['idCantiere'];

if i want to try a var_dump($details) it return NULL

NarcosZTK_10
  • 137
  • 3
  • 11

2 Answers2

0

your code shoud be like this

<?php
$servername = "localhost";
$username = "progettocantiere";
$password = "";
$dbname = "my_progettocantiere";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 



$idCantiere = $_GET['idCantiere'];

$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '{$idCantiere'}";



    $result = $conn->query($sql1);


    $details = $result->fetch_array();




    $savedNomeCantiere = $details["nomeCantiere"];
    $savedCodiceCommessa = $details["codiceCommessa"];
    $savedIndirizzoCantiere = $details["indirizzoCantiere"];

var_dump($details);


$result1 = $conn->query($sql1);

echo($nomeCantiere);

?>


<html>
<head>
</head>
<body>



        <table width="300" border="0">
        <tr>
            <td>Name</td>
            <td><input type="text" name="savedNomeCantiere" style="text-align:right" value="<?php echo $savedNomeCantiere; ?>" /></td>
        </tr>
        <tr>
            <td>Cost</td>
            <td><input type="text" name="savedCodiceCommessa" style="text-align:right" value="<?php echo $savedCodiceCommessa; ?>" /></td>
        </tr>
        <tr>
            <td>Cost</td>
            <td><input type="text" name="savedIndirizzoCantiere" style="text-align:right" value="<?php echo $savedIndirizzoCantiere; ?>" /></td>
        </tr>

        </table>

<br/>


</body>
</html>
khalid
  • 121
  • 8
  • Don't just post a code dump. Please explain why this solution works. – waterloomatt Oct 02 '18 at 15:38
  • than this code $result = mysqli_query($sql1, $conn); shoud be $result = $conn->query($sql1); and this $details = mysqli_fetch_array($result); shoud be $details = $result->fetch_array(); because they used $conn = new mysqli to connect to db, you can use the original code of "$result = mysqli_query($sql1, $conn); $details = mysqli_fetch_array($result);" if you open connection with db like that $conn = mysqli_connect(...); ;) – khalid Oct 02 '18 at 17:19
0

One of the issues is that you're passing $conn and $sql1 in the wrong order.

$result = mysqli_query($sql1, $conn); should be $result = mysqli_query($conn, $sql1);

if i want to try a var_dump($details) it return NULL

That is exactly why you need to check $result before using it. mysqli_query returns an object if it was successful or false if it failed - http://php.net/manual/en/mysqli.query.php#refsect1-mysqli.query-returnvalues.

You don't need to get fancy here but you should at least do a sanity check.

...
$sql1 = "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'";
$result = mysqli_query($sql1, $conn);

if ($result !== false) {
    $details = mysqli_fetch_array($result);
    ...

Also, I'm required to point out "SELECT * FROM Cantiere WHERE idCantiere = '$idCantiere'"; is very dangerous because you don't have control over $idCantiere. Please lookup SQL Injection and how to avoid it.

waterloomatt
  • 3,662
  • 1
  • 19
  • 25