简体   繁体   中英

Foreach loop and variables in php

Happy new yar. I am still new to php so I need your great help. I am designing a post page where visitors can post anything. I am almost through but when I post something to the page, the new post over ride the old one, I am certain I need to use foreach loop but my problem I can't define the $posting variable.

My bad, but I really need you guys to help me. here is my coding:

<?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
}
?>

And please i also need the code to make the post a link to its page

check your id is auto increment or not if not then make it autoincrement

then try to change this code in your page :

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

to

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

There are a couple problems with your code. The first is that you mysql_query when you should be using mysqli_query, same mysql_fetch_assoc, should be mysqli_fetch_assoc -- please replace all references of mysql_* with mysqli_*. The second is you are running a query inside of a function. The third problem is your query is wrong. And... you do not need a foreach in the way you are using it.

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;
}

You had a typo, it should be SELECT not SLECT . You do need to concatenate variables when you are inside of a double quoted string. I would link to the PHP documentation about it but is not super clear. Basically the following lines all result in the same output:

$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!

Notice how you can put a variable inside of a string with double quotes. But you cannot do this:

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

The reason you should use mysqli_query is because mysql_query is no longer supported. Inside of your function you will need to use the special keyword global to get the variable of your database connection (I set it to $db in the example but you might have it called something else).

Basically, global is a PHP keyword that will allow you to access variables that have been defined in the global scope. Read about in the documentation by clicking here

[..] within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.

Lastly.. you do not have the variable $posting defined correctly.

You can fix this by getting rid of your call to foreach, replace this line:

foreach ($posting as $posting) {

With these 2 lines:

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

Your $posting variable is undefined, when it looks like you actually just want to query the database and get all the rows from the prayer table.

This query will give you warnings in MYSQL but execute due to PK auto increment:

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

Solution is that define column names in this query and if your id is PK than not use because its auto increment:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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