MySQL querying error, I don't know why - php

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

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')"

Related

PHP POST requests only inserts blank rows

So i'm pretty new to PHP and can't understand why my POST request is not working. .
I'm just building a simple TodoApp and have a Add new task view like this:
add.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css" rel="stylesheet" />
<link rel="stylesheet" href="../css/style.css" />
<title>To-Do List</title>
</head>
<body>
<div class="wrapper">
<form action="" method="post">
<h2 class="title">Add a new task</h2>
<div class="content">
<div class="inputFields">
<label for="task">Task:</label>
<input type="text" name="task" placeholder="Task name" />
<label for="task">Description:</label>
<br />
<textarea type="text" name="description" placeholder="Add a description" rows="5"
cols="40"></textarea>
<br />
<button type="submit" class="btn">Save</button>
</div>
</div>
</form>
<button class="btn">Go back</button>
</div>
</body>
</html>
My db file DB.php looks like this:
<?php
error_reporting (E_ALL ^ E_NOTICE);
/**
* #package DBConnection
* #author Frida
*/
// DB Connection
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=TodoApp', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// GET - Get All tasks
$statement = $pdo->prepare('SELECT * FROM todo_list ORDER BY created DESC');
$statement->execute();
// Fetch all tasks as an assoc array
$tasks = $statement->fetchAll(PDO::FETCH_ASSOC);
// POST - Add a task
$task = $_POST['task'];
$description = $_POST['description'];
$date = date('Y-m-d H:i:s');
$pdo->exec("INSERT INTO todo_list (task, description, status, created)
VALUES ('$task', '$description', 0, '$date')
")
?>
This is my schema:
Todo schema
I've done var_dump($_POST) which shows the array without any issues, if anyone can help me out?
In your HTML form action attribute is empty which mean that form is submit to same file in your case add.php.
So you should use $_POST in add.php file either you must submit form to DB.php for example
<form action="DB.php" method="post">

Empty page on sql - php querying

I'm making a website where people can see their personal stats. I'm doing this by retrieving SQL data with a php system. Here are my 2 files :
<!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">
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="search.php" method="GET">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>
</body>
</html>
And this is the file that is loaded when a user clicks on search :
<?php
mysql_connect("localhost", "root", "Medionakoya0102") or die("Error
connecting to database: ".mysql_error());
mysql_select_db("isolate") or die(mysql_error());
?>
<!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">
<head>
<title>Search results</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
$query = $_GET['query'];
$min_length = 3;
if(strlen($query) >= $min_length){
$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM Player
WHERE (`name` LIKE '%".$query."%') OR (`uuid` LIKE '%".$query."%')")
or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<p><h3>".$results['join_date']."</h3>".$results['gems']."
</p>";
}
}
else{
echo "No results";
}
}
else{
echo "Minimum length is ".$min_length;
}
?>
</body>
</html>
Now the problem is, the index loads, but when I click on search, sometimes the page is just blank, or it gives me an error : HTTP ERROR 500
Hope someone can help
A 500 HTTP error is a response from the webserver, so your form is submitting correctly but there is most likely an error from PHP. Check your web server logs for an error message correlating to the form submissions. If you're not sure what the error is telling you please edit your question to include the error and I'm sure we can get it solved.
Also, some information about your environment would be useful: which webserver are you using, what's your PHP version, etc.

Input a number range to mysql database

how to input a number range, from 2 text fields, this is my code is seems to be not working, the range function i want it to be inserted to mysql database but this code is just to test if i can get the 2 textfields to connect to the range function. anyone can help me solve this problem?
<?php
$from = '.from';
$to = '.to';
$number = range('$from','$to');
print_r ($number);
?>
<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>New Waybill</title>
<link rel="shortcut icon" href="../img/icon.ico">
<link rel="stylesheet" type="text/css" href="../css/forms.css" />
</head>
<body>
<form id="form3" name="form1" method="post" action="">
<p>From:
<input type="text" name="from" id="form_number" class="from" value="0"/>
- To:
<input type="text" name="to" id="form_number" class="to" value="50" />
</p>
<p>Waybill Booklet:
<select name="waybill_booklet" id="form_list">
</select>
</p>
<p>
<input type="submit" name="button" id="form_button" value="OK!" />
</p>
</form>
</body>
</html>
Replace your php code with this:
<?php
if (!empty($_POST)) {
$from = $_POST['from'];
$to = $_POST['to'];
$number = range($from,$to);
print_r($number);
}
?>
To access a form post in PHP you use the $_POST superglobal. More info found here: http://www.php.net/manual/en/reserved.variables.post.php.
I've put a check to see if it's empty or not at the start, so when the page first loads it won't attempt to run the PHP code. Presumably you'll then replace the print_r with whatever you need to insert the data into your database.

Checking Login from form then redirecting to another page

This was my last attempt that I tried as a last effort:
index.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">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Customer Login</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="wrapper">
<div class="login">
<form name="loginForm" action="loginCheck.php" method="post">
<?php require("protect/serverInfo.php"); ?>
Email: <input type="text" name="Email" maxlength="35" /><br />
Password: <input type="text" name="Password" maxlength="4" /><br />
<input type="submit" name ="submit"/>
</form>
</div>
</div>
</body>
</html>
loginCheck.php
<?php
session_start();
$_SESSION['email'] = $_POST['Email'];
$_SESSION['password'] = $_POST['Password'];
require("protect/serverInfo.php");
$myusername=$_POST[Email];
$mypassword=$_POST[Password];
$result = mysql_query("SELECT * FROM Customers WHERE Email='$myusername' AND Password=$mypassword");
$count=mysql_num_rows($result);
if($count==1){
header('Location: customer.php');
exit();
}
else{
header('Location: index.php');
exit();
}
?>
customer.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">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<link href="stylesheet.css" rel="stylesheet" type="text/css" />
<?php
session_start();
$myusername = $_SESSION['email'];
$mypassword = $_SESSION['password'];
?>
</head>
<body>
<?php
echo"success";
?>
</body>
</html>
I just need a very simple way to have a form post, the post info to be checked if correct then redirect if correct and pass the post data with it. I have been trying to use sessions and redirects but it doesn't to work quite right. What is the easiest way to accomplish this. At the moment I have been using PHP to check the login info from a MySQL database.
You need to use "session_start()" before you do anything else on the page.
Other few things, I avoid storing passwords on a page.. it just seems like a security issue.
Your login form should generate $_SESSION data based on the mysql information returned from the queury, not the form information that the user submited. You need to check against your customers database, to make sure they are an actual customer.
Also, avoid using the "header()" function, especially when working with sessions. I typically have a "redirect" function in php that does something like this...
function redirect($url) {
echo "<script type='text'/javascript'>window.location='" . $url . "';</script>";
}

PHP not working with Expression Web 3

I'm starting to try to learn PHP while using Expression Web 3. I set up PHP runtime and configured the ini. What happens is my script doesn't do as it should.
My page looks like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<title>Sports</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<link href="master.css" rel="stylesheet" type="text/css" />
<style type="text/css">
</style>
</head>
<body>
<div id="masthead">
</div>
<div id="top_nav">
<ul>
<li>Sports </li>
<li>Clubs</li>
<li>Cloud</li>
</ul>
</div>
<div id="container">
<div id="left_col">
</div>
<div id="page_content">
</div>
</div>
<div id="footer">
<form method="post">
</form>
</div>
<form action="action.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p>Your age: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
</body>
</html>
Then my PHP file looks like:
<!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">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>action</title>
</head>
<body>
Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.
</body>
</html>
What should happen is for example if I type John as name and 50 as age, it should output Hi John, you are 50 years old.
But instead it says (with Firefox) :
Hi you are years old
in Internet Explorer 8, it just outputs the whole php script.
I'm not sure where I went wrong.
Thanks
Either you are not running these files from a PHP enabled webserver or the webserver is not configured to serve the used file extension as PHP. How to install and configure a webserver is best asked on ServerFault.com. Have a look at XAMP or Zend Server CE.
In addition, you have a terribly low acceptance rate. Why not go back to some previous questions and you asked and accept some of the answers you have been given?

Categories