繁体   English   中英

PHP中的Foreach循环和变量

[英]Foreach loop and variables in php

新年快乐。 我还是php新手,因此需要您的大力帮助。 我正在设计一个帖子页面,访问者可以在其中发布任何内容。 我快要经历了,但是当我在页面上发布新内容时,新的内容超过了旧的内容,我确定我需要使用foreach循环,但是我的问题是我无法定义$posting变量。

我不好,但我真的需要你们帮助我。 这是我的编码:

<?php
include 'connect.php';
?>

<?php
if (isset($_POST['submit'])) {
    $title = $_POST['title'];
    $post = $_POST['post'];
    $name = $_POST['name'];

    if (empty($title) or empty($post) or empty($name)) {
        $message = "Please fill in all fields";
    } else {
        mysql_query("INSERT INTO prayer VALUES('', '" . $title . "', '" . $post . "', '" . $name . "')");
        $message = "PRAYER REQUEST SUBMITTED!!!";
    }

    echo"$message";
}
?>
<form method="post" action="prayerpage.php">
    <table width="80%">
        <tr>
            <td><b>Name:</b><input type="text" name="name" />
                <b>Title:</b><input type="text" name="title" /></td>
        </tr>
        <tr>
            <td><b>Prayer<br>Request:</b></td>
            <td><textarea name='post' rows='10' cols='40'></textarea></td>
        </tr>
        <tr>
            <td></td>
            <td><input type="submit" name="submit" value="SUMIT"/></td>
        </tr>
    </table>
</form>
<hr width="70%">
<?php

function postid($id) {
    $array = array();
    $q = mysql_query("SLECT * FROM prayer WHERE id='.$id.'");
    while ($r = mysql_fetch_assoc($q)) {
        $array['id'] = $r['id'];
        $array['title'] = $r['title'];
        $array['name'] = $r['name'];
        $array['post'] = $r['post'];
    }
    return $array;
}

foreach ($posting as $posting) {
    ?>
    <table width="60%">
        <tr>
            <td><font color="blue"><?php echo $title; ?></font></td>
        </tr>
        <tr>
            <td><?php echo $post; ?> - <font color="blue"><?php echo $name; ?></font>         </td>
        </tr>
    </table>
    <hr noshade width="50%">
    <?php
}
?>

而且,我还需要代码来使帖子成为其页面的链接

检查您的ID是否为自动递增,如果不是,则将其设为自动递增

然后尝试在您的页面中更改此代码:

mysql_query("INSERT INTO prayer VALUES('', '".$title."', '".$post."', '".$name."')");

mysql_query("INSERT INTO prayer VALUES('".$title."', '".$post."', '".$name."')");

您的代码有几个问题。 第一个是当您应该使用mysqli_query时使用mysql_query,相同的mysql_fetch_assoc应该是mysqli_fetch_assoc -请用mysqli_ *替换所有mysql_ *的引用。 第二个是您正在函数内部运行查询。 第三个问题是您的查询错误。 而且...您不需要在使用它时就使用foreach。

function postid($id) {
    global $db;
    $array = array();
    $id = (int)$id; // cast as int to prevent SQL injection
    $q = mysqli_query($db, "SELECT * FROM prayer WHERE id=$id");
    while ($r = mysqli_fetch_assoc($q)) {
        $array['id'] = $r['id'];
        $array['title'] = $r['title'];
        $array['name'] = $r['name'];
        $array['post'] = $r['post'];
    }
    return $array;
}

您有错别字,应该是SELECT not SLECT 当您在双引号字符串中时,确实需要连接变量。 我会链接到有关它的PHP文档,但不是很清楚。 基本上,以下各行都将产生相同的输出:

$amazing = "neato!";
$example1 = "This is my variable: $amazing"; // Output: This is my variable: neato!
$example2 = 'This is my variable: ' . $amazing;  // Output: This is my variable: neato!
$example3 = "This is my variable: " . $amazing;  // Output: This is my variable: neato!

注意如何将变量放在带双引号的字符串中。 但是您不能这样做:

$doesNotWork = 'This is my variable: $amazing'; // Output: This is my variable: $amazing
$doesNotWork = "This is my variable: '.$amazing.'"; // Output: This is my variable: '.$amazing.'

您应该使用mysqli_query的原因是因为不再支持mysql_query。 在函数内部,您将需要使用特殊关键字global来获取数据库连接的变量(在示例中,我将其设置为$ db,但您可能将其称为其他名称)。

基本上, global是一个PHP关键字,它将允许您访问已在全局范围中定义的变量。 点击此处阅读文档

[..]在用户定义的函数中引入了局部函数范围。 默认情况下,函数内部使用的任何变量都限于局部函数范围。

最后..您没有正确定义变量$ posting。

您可以通过摆脱对foreach的调用来解决此问题,替换此行:

foreach ($posting as $posting) {

用这2行:

$q = mysqli_query($db, "SELECT * FROM prayer");
while ($posting = mysqli_fetch_assoc($q)) {

您的$ posting变量是未定义的,看起来您实际上只是想查询数据库并从祷告表中获取所有行。

该查询将在MYSQL中给您警告,但由于PK自动递增而执行:

mysql_query("INSERT INTO prayer 
            VALUES('', '" . $title . "', '" . $post . "', '" . $name . "')");

解决方案是在此查询中定义列名称,如果您的ID为PK,则定义为不使用,因为其自动递增:

mysql_query("INSERT INTO prayer (title,post,name) 
             VALUES('".$title."','".$post."','".$name."')");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM