Why am I only getting the first word from the database field?

Asked

Viewed 131 times

0

The code below returns database values, but the field model only returns me the first word of the table field, the rest of the model name does not appear. What I am doing wrong?

<?php
include('conecta.php');

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



$sql = "SELECT product_id, model, price FROM ocqp_product LIMIT 5";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {



        echo '<form action="" method="post">
        <input type="text" name="pid" value='. $row["product_id"].' ><br>
        <input type="text" name="model" value='. $row['model'].' ><br>
        <input type="text" name="price" value='. $row["price"].' ><br>


        </form>';
   }
} else {
echo "0 results";
}
$conn->close();

Structure

http://i.stack.Imgur.com/bXgcR.png

  • 2

    All Names in inputs are repeated, do not know if this has got to be. Put the table structure

  • I already changed the Names, I hadn’t noticed yet thanks @Guilhermenascimento

  • *** Put the table structure [2]

  • put 1 or 2 lines of bank content, maybe help

3 answers

3


Try it like this, you weren’t putting quotes on value.

echo '<form action="" method="post">
        <input type="text" name="pid" value="'. $row["product_id"].'" ><br>
        <input type="text" name="model" value="'. $row['model'].'" ><br>
        <input type="text" name="price" value="'. $row["price"].'" ><br>


        </form>';

0

Augusto is correct add yet

echo '<input type="" name="" value="'.$row['name'].'" />';

0

Why you mix Mysqli methods, I recommend using Object Oriented mode, in case your code would look like this:

   <?php
include('conecta.php');

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



$sql = "SELECT product_id, model, price FROM ocqp_product LIMIT 5";
$result = $conn->query($sql);

if ($conn->num_rows($result) > 0) {
    // output data of each row
    while($row = $conn->fetch_assoc($result)) {



        echo '<form action="" method="post">
        <input type="text" name="pid" value="'. $row["product_id"].'" ><br>
        <input type="text" name="model" value="'. $row['model'].'" ><br>
        <input type="text" name="price" value="'. $row["price"].'" ><br>


        </form>';
   }
} else {
echo "0 results";
}
$conn->close();

Yeah, and you forgot the quotes in value!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.