发表声明时无法获取php mysqli

I'm new to php and I've been trying to get a form to post to values to a database.

  • I've successfully connected to the database, because I can pull values from the table and display them in the HTML.

  • I believe the mysqli_query is correct because I can replace the $variables with examples and they work and do post.

  • However, I can't seem to find the correct combination of how to insert the $name and $desc variables I've tried $name, '$name', and '$_POST[friendname]. What am I missing?

<?php
include 'includes/connect.php';   
$name = $_POST[friendname];
$desc = $_POST[desc];
//WRITE TO DATABASE
if(!$_POST) {
echo "Form info: " . $name . " " . $desc;
echo "<br />Use the form below to add a new person! <br /><br />";
}
else {
$query = "INSERT INTO Friends (ID, Name, Description) VALUES ('NULL', $name, $desc)";
mysqli_query($sql, $query);
echo "<br />You've added " . $name . "<br /><br />";
}
$result = mysqli_query($sql,"SELECT * FROM Friends");
while($row = mysqli_fetch_array($result)) {
echo "<strong>" . $row['Name'] . "</strong> - " . $row['Description'];
echo "<br>";
}
?>

<p><strong>Add a new person to the database</strong></p>
<!-- FORM -->
<form name="addform" action="index.php" method="post">
Name:  <input type="text" name="friendname" /><br />
Description:  <input type="text" name="desc" /><br />
<input type="submit" name="submit" label="submit" />
</form>

try to replace

$query = "INSERT INTO Friends (ID, Name, Description)
VALUES ('NULL', $name, $desc)";

to

$query = "INSERT INTO Friends (ID, Name, Description)
VALUES ('NULL', {$name}, {$desc})";

instead of write SQL statement as string,you can use PDO

try not to put the id into the query, I assume that this column is auto-increment. Also you should put the variables as show below

$query = "INSERT INTO Friends (Name, Description) 
                   VALUES ('".$name."', '".$desc."')";

NOTE: If you can show us what data is storing in your database it would be of help

I've had to wrap the desc in backquotes which is MySQL-speak for "this is a variable name" because desc is a keyword. You will make your life easier if you change the column name to description instead.

PDO comes as standard on most PHP installations and does make things a lot easier.

<?php
$pdo = new PDO("mysql:host=localhost;dbname=mysql", "user", "password"); 

if (isset($_POST))
{
    $name = $_POST['friendname'];
    $desc = $_POST['desc'];
    echo "Form info: " . $name . " " . $desc;

    // WRITE TO DATABASE
    $sql = "INSERT INTO Friends(friendname, `desc`) VALUES (:fn, :d)";
    $query = $pdo->prepare($sql);
    $query->execute(array(':fn'=>$name, ':d'=>$desc));
    echo "<br />You've added " . $name . "<br /><br />";
    die;
}
?>

<p>Use the form below to add a new person!</p>

<p><strong>Add a new person to the database</strong></p>

<form name="addform" action="index.php" method="post">
    Name:  <input type="text" name="friendname" /><br />
    Description:  <input type="text" name="desc" /><br />
    <input type="submit" name="submit" label="submit" />
</form>

There is a reasonable example of executing a "SELECT *" here: How to fetch row with PDO