0

I have a select value 'post', when someone wants to change the status of the post from unsolved to solved they have to choose a post en than click 'change to solved'

The problem is when I click on the button, it doesn't change because PHP takes not the whole title of the post. So when my post is called 'Photoshop crashes', it only sends 'Photoshop'. That's why it doesn't update in my database, the query can't find the right post, when the title of the post is only 1 word, my table gets updated.

<?php
if (isset($_POST['btnSolved'])) 
{
// wanneer er op de knop geklikt is proberen we user te saven in de databank
if (!empty($_POST['btnSolved'])) 
{
    $solved = $_POST['unsolved'];
    $conn = new mysqli("localhost","root","root","PhpProject");
    if ($conn -> connect_errno) {
        throw new Exception("No connection with database!");

    } else {
        $sql = "UPDATE Posts SET status = 'Solved' WHERE post='".$solved."'";
    }
   }
    $conn->query($sql);
  }
  ?>

In my body:

<h3>Change status</h3>
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
            <?php
            if(mysqli_num_rows($showBugs) > 0)
            {
                echo "<select name= unsolved>"; 
                while ($row = mysqli_fetch_assoc($showBugs)) 
                {
                    echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";
                }
                echo "</select>";
            }
            ?>
            <br />
            <input class="btn btn-info dropdown-toggle" type="submit" name="btnSolved" value="Change to solved" />
        </form>

This is what I get when I do a print of the $sql

UPDATE Posts SET status = 'Solved' WHERE post='Photoshop'

Does someone know why PHP can post 1 word, but not the whole title? It might be something stupid, but I don't know how to fix this.

4

4 回答 4

1

您的问题是 html 中没有引号。解决这个问题:

echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";

对此:

echo '<option value="' $row['subject'] . '">' . $row['subject'] . "</option>";
于 2013-05-21T10:57:12.340 回答
0

如果 $row['subject'] 包含两个单词,浏览器将无法正确解析:

echo "<option value=" . $row['subject'] . ">" . $row['subject'] . "</option>";

您的浏览器将读取

<option value=two words>

作为

<option value=two>

不知道如何处理单词

使用转义双引号

echo "<option value=\"" . $row['subject'] . "\">" . $row['subject'] . "</option>";
于 2013-05-21T10:57:00.853 回答
0

尝试将值括在单引号中,

echo "<option value='" . $row['subject'] . "'>" . $row['subject'] . "</option>";
于 2013-05-21T10:57:31.433 回答
0

您需要为 value 属性添加引号。如果你的值中有引号,你需要像这样$row['subject']通过htmlentities()

echo '<option value="' . htmlentities($row['subject']) . '">' . htmlentities($row['subject']) . "</option>";

在这种情况下,不需要标签的 htmlentities() ,但这是一种很好的做法。

于 2013-05-21T11:02:48.637 回答