Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
When I click the submit button it shows the error above

What I have tried:

<?php
include "db1.php";
include "config.php";

$db = new database();

if (isset($_POST['submit'])){
	$title = $_POST['title'];
	$content = $_POST['content'];
$query = "INSERT INTO posts(title,content) VALUES('$title',$content')";

$run=$db->insert($query);

}

?>



<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>

</body>
</html>

<form action="insert_post.php" method="post" enctype="multipart/form-data">
	<div class="form-group">
	<center><table width="800" align="center" border="2">
		<tr bgcolor="orange">
		<td colspan="6"><h1 style="text-align:center">Post EDIT</h1></td>
		</tr>
		<tr>
			<td align="right" bgcolor="orange">Post title:</td>
			<td><input type="text" name="title" size="60"></td>
		</tr>
		<tr>
			<td align="right" bgcolor="orange">Post Content:</td>
			<td><textarea name="content" rows="15" cols="40"></textarea></td>
		</tr>
     <tr>

	<td colspan="6" align="center" bgcolor="orange"><input type="Submit" name="submit" value="Publish Now"  /></td>
</tr>	






	</table>
Posted
Updated 2-Mar-23 3:03am
Comments
Jochen Arndt 17-Oct-17 12:07pm    
The error message is quite clear:
There is no entry in the $_POST array with the index 'content'.

When calling this from a form, ensure that there is an input field with the name 'content'.

PHP
$query = "INSERT INTO posts(title,content) VALUES('$title',$content')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
As per your submitted code there is a syntax error in line 9
$query = "INSERT INTO posts(title,content) VALUES('$title',$content')";

This should change as
$query = "INSERT INTO posts(title,content) VALUES('$title','$content')";

Reason you are missing a quote before $content
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900