lukecolli98 lukecolli98 - 3 months ago 10
HTML Question

MySQL querying error, I don't know why

Ok so what I want to build:
A website where you login (I will do this later). There is a form where you submit your news report, which then goes into a MySQL database. This then gets displayed in a table view on the iPhone (that comes later).
As I mentioned, I get an 'Error querying database'. I have tried to fix it but I am new to MySQL and PHP, so I don't know what else to do.
I have this set up on a home server (WAMP).

My report.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Football Central News Report Submission Page</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h1>Football Central News Report Submission Page</h1>
<h2>Sumbit your football news report here</h2>
<h3>CHECK FOR MISTAKES !!!</h3>
<form method="post" action="report.php">
<label for="title">Title:</label>
<input type="text" name="title" />
<br />
<label for="author">Author:</label>
<input type="text" name="author" />
<br />
<label for="subtitle">Subtitle:</label>
<input type="text" name="subtitle" />
<br />
<label for="body">Body:</label>
<textarea name="body"></textarea>
<br />
<label for="image">Image:</label>
<input type="file" id="image" name="image" />
<br />
<input type="submit" value="Submit your news report" name="submit" />
</form>
</body>
</html>


My report.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Football Central News Report Submission Page</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h1>Football Central News Report Submission Page (Confirmation)</h1>

<?php
$title = $_POST['title'];
$author = $_POST['author'];
$subtitle = $_POST['subtitle'];
$body = $_POST['body'];
$image = $_POST['image'];

$dbc = mysqli_connect('localhost', 'root', 'xxxxxx', 'news_reports')
or die('Error connecting to database server.');

$query = "INSERT INTO news_reports (title, author, subtitle, body) " .
"VALUES ('$title', '$author', '$subtitle', '$body', '$image')";

$result = mysqli_query($dbc, $query)
or die('Error querying database.');

mysqli_close($dbc);
?>

<p>
-Thanks for submitting the form.<br />
-Your news report has been submitted to the database and should appear in the app shorty.
</p>

</body>
</html>


AND FINALLY-My database structure (I can't post screenshots)
Fields:
report_id (primary key, auto-increment)
title
author
subtitle
body
image

P.S
For image /\ I am following "Head First PHP and MySQL", so it is the image name that is stored in the db, not the image (I haven't put this into the form, but I don't THINK this is the problem).

Sorry for the long post guys.
Luke

Answer Source

You have a mismatch. You named 4 columns to insert to but you defined 5 values. Add the missing column.

$query = "INSERT INTO news_reports (title, author, subtitle, body) " .
    "VALUES ('$title', '$author', '$subtitle', '$body', '$image')"