PHP回显问题,显示不正确

时间:2016-03-24 15:46:56

标签: php html mysql

所以我正在为uni制作一个电子商务网站,一切都很好,直到我真的尝试用chrome浏览网站。基本上我是从MySQL生成一个表的东西。我遇到的问题高于表格所在的位置。请原谅代码一团糟。我在添加内容之间。



<?php $command='select * from products' ; $runCommand=m ysqli_query($connection, $command); if (mysqli_num_rows($runCommand)>0){ echo "
<form id=\ "shoppingcart\" action=\ "cart.php\" method=\ "post\">"; echo "
  <table>"; echo "
    <thead>"; echo "
      <tr>"; echo "
        <th scope=\ "col\">Image</th>"; echo "
        <th scope=\ "col\">Item</th>"; echo "
        <th scope=\ "col\">Qty</th>"; echo "
        <th scope=\ "col\">UpdatedQty</th>"; echo "
        <th scope=\ "col\">Price</th>"; echo "</tr>"; echo "</thead>"; while($ROWVARIABLE= mysqli_fetch_assoc($runCommand)) { //line BROKE goes here //need an if quantity=0 then don't print the bunch we have under here...// echo "
    <tr>"; echo "
      <td>
        <img src='". $ROWVARIABLE["image"]. "' alt='". $ROWVARIABLE["decription"]. "' height='200' '</img></td>"; //this is still one line, don't flip shit. It 's the img with a hover over Decription
			echo "<td>". $ROWVARIABLE["name"]. "</td>"; //Item 
			echo "<td>".newID[$ROWVARIABLE["ID"]]."</td>"; //Quantity
			echo "<td><select name=\"updateQ\" id=\"updateQ\">"; //Updated Quantity
			echo "<option selected=\"selected\">No change</option>";
			echo "<option>1</option>";
			echo "<option>2</option>";
			echo "<option>3</option>";
			echo "<option>4</option>";
			echo "<option>5</option>";
			echo "<option>6</option>";
			echo "<option>7</option>";
			echo "<option>8</option>";
			echo "<option>9</option>";
			echo "</select>";
			echo "</td>";
			//BROKE 2.0
			// Gotta fix this ^^ line to display £Price of all units added(£Price of one unit)
			
		}
	echo "</table>";
	echo "</br>";
	}
	else{
		echo "Table broken, pls stahp!";
	}
	?>
&#13;
&#13;
&#13;

如果你运行代码snippit,它会在表格上面抛出一堆回声,我不知道为什么或如何修复它。我想这可能是一个菜鸟错误,但我真的很恐慌。该文件保存为.php,我使用<!DOCTYPE html>启动该文件。这是我文件的顶部

<?php $connection = mysqli_connect(the stuff I need to connect.); ?> 认为是相关的。

https://gyazo.com/0dd6a3d7b025133c5b061e6af06f1091

1 个答案:

答案 0 :(得分:3)

当回显大量HTML时,人们经常遇到这样的问题。 PHP的优势之一是它是一种出色的模板语言。您可以通过使用它来避免这些类型的问题。只需编写HTML,并在需要插入动态内容的地方使用PHP。通过这种方式,您不必担心所有各种报价转义等问题。您的代码可能存在其他问题,但这至少应该说明我所谈论的一般概念:

<?php
$command = 'select * from products';
$runCommand = mysqli_query($connection, $command);
if (mysqli_num_rows($runCommand) > 0): ?>
<form id="shoppingcart" action="cart.php" method="post">
    <table>
        <thead>
            <tr>
                <th scope="col">Image</th>
                <th scope="col">Item</th>
                <th scope="col">Qty</th>
                <th scope="col">UpdatedQty</th>
                <th scope="col">Price</th>
            </tr>
        </thead>
        <?php while($ROWVARIABLE = mysqli_fetch_assoc($runCommand)): ?>
        <tr>
            <td>
                <img src="<?= $ROWVARIABLE["image"] ?>"
                     alt="<?= $ROWVARIABLE["decription"] ?>" height="200">
            </td>
            <td><?= $ROWVARIABLE["name"] ?></td>
            <td><?= newID[$ROWVARIABLE["ID"]] ?></td>
            <td>
                <select name="updateQ" id="updateQ">
                    <option selected="selected">No change</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                </select>
            </td>
        </tr>
    <?php endwhile; ?>
</table>
</br>
<?php else: ?>
    Table broken, pls stahp!
<?php endif; ?>