This question already has answers here:
Why shouldn't I use mysql_* functions in PHP?
(14 answers)
Closed 3 years ago.
I am following a tutorial series (Link to Video) in which I am learning to create a comments section to a web page. I am using XAMPP as it is what the guy in the video is using. I have finished writing the code which sends the data (name, time, message) to the database and when I go to try it out nothing happens. I checked the database and there is nothing
This is the code:
index.php
<?php
date_default_timezone_set('Europe/London');
include 'dbh.inc.php';
include 'comments.inc.php';
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
echo"<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
<textarea name='message'></textarea> <br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>
</body>
</html>
comments.inc.php
<?php
function setComments($conn) {
if (isset($_POST['commentSubmit'])) {
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSTERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
$result = mysql_query(- $sql);
}
}
dbh.inc.php
<?php
$conn = mysqli_connect('localhost', 'root', '', 'commentsection');
if (!$conn) {
die("Connection Faild: ".mysql_connect_error());
}
Please help me.
thank you
Change
<?php
echo"<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
<textarea name='message'></textarea> <br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>
To:
<?php
// action empty send the post data to the this fila again
// setComments function have a condition to work only when POST data is present
setComments($conn);
echo"<form method='POST' action=''>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('D-m-y H:i:s')."'>
<textarea name='message'></textarea> <br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>";
?>
And
This:
$sql = "INSTERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
$result = mysql_query(- $sql);
}
To:
// INSERT is the correct sintaxis
$sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
$result = mysql_query($sql);
}
Finally
$conn = mysqli_connect('localhost', 'root', '', 'commentsection');
To:
// mysqli_connect have diferent parameters
$conn = mysql_connect('localhost', 'root', '', 'commentsection');
Tested and working
Related
I'm trying to write a program that is able to insert into three different tables, but when I execute the first button, the Insert button reloads the page and doesn't execute the query I want it too. How would I get a button that executes a query and doesn't reload the page. Right now the code will reload the page instead of submitting the query and running the code. I want to be able to have a button that runs php code without reloading the entire page, and will execute the query
<html>
<body>
<form action="#" method="post">
<input type="submit" class="button" name="department" value="Department" />
<input type="submit" class="button" name="location" value="Locations" />
<input type="submit" class="button" name="container" value="Container" />
</form>
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['department'])){
?>
<form action="#" method="post">
Office Address:<input type ='text' name='addIn'><br>
Contact Info:<input type='text' name='conIn'><br>
Office Hours:<input type='text' name='offIn'><br>
Department Name:<input type='text' name='nameIn'><br>
<button name="submit">Insert</button>
</form>
<?php
if(isset($_POST['submit'])){$con=#mysqli_connect('localhost', 'user',
'pass', 'RecyclingDB') or die("Connection error.");
$address = $_POST['addIn'];
$contact = $_POST['conIn'];
$hours = $_POST['offIn'];
$department = $_POST['nameIn'];
$insert = "INSERT INTO Department(OfficeAddress, ContactInfo,
OfficeHours, DepartmentName) VALUES('$address', '$contact',
'$hours', '$department')";
$rs = mysqli_query($con, $insert) or die ("Insert error try again.");
}
}
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['location'])){
?>
<form action="#" method="post">
Location Address:<input type ='text' name='locIn'><br>
County:<input type='text' name='couIn'><br>
Type of Location:<input type='text' name='typeIn'><br>
Hours:<input type='text' name='hoursIn'><br>
Contact Info:<input type='text' name='conIn'><br>
Department Address:<input type='text' name='depIn'><br>
Map Link:<input type='text' name='mapIn'><br>
<button name="submit">Insert</button>
</form>
<?php
include_once 'insert.php';
if(isset($_POST['submit'])){
$con=#mysqli_connect('localhost', 'user', 'pass',
'RecyclingDB') or die("Connection error.");
$address = $_POST['locIn'];
$county = $_POST['couIn'];
$type = $_POST['typeIn'];
$hours = $_POST['offIn'];
$contact = $_POST['conIn'];
$depAdd = $_POST['depIn'];
$link = $_POST['mapIn'];
echo"$address, $county, $type. $hours, $contact, $depAdd,
$link<br>";
$insert = "INSERT INTO RecyclingLocation(Address, County,
TypeOfLocation, Hours, ContactInfo, DepartmentAddress,
MapLink) VALUES('$address', '$county', '$type', '$hours',
'$contact', '$depAdd', '$link')";
echo"<br>$insert";
$rs = mysqli_query($con, $insert) or die ("insert error.");
if(mysqli_affected_rows($rs)){
echo"<br>Record Inserted successfully";
}else{
echo"<br>Record unable to insert, Try Again";
}
}
}
?>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['container'])){
?>
<form action="#" method="post">
Size of Container:<input type ='text' name='sizeIn'><br>
Material:<input type='text' name='matIn'><br>
Number of Containers:<input type='text' name='numIn'><br>
Location Address:<input type='text' name='addIn'><br>
Cost:<input type='text' name='costIn'><br>
<button name="submit">Insert</button>
</form>
<?php
if(isset($_POST['submit'])){
$con=#mysqli_connect('localhost', 'user', 'pass',
'RecyclingDB') or die("Connection error.");
$size = $_POST['sizeIn'];
$material = $_POST['matIn'];
$number = $_POST['numIn'];
$location = $_POST['addIn'];
$cost = $_POST['costIn'];
echo"$size, $material, $number, $location, $cost<br>";
$insert="INSERT INTO Container(SizeOfContainer, Material,
NumberOfContainers, LocationAddress, Cost) VALUES('$size',
'$material', '$number', '$location', '$cost')";
echo"<br>$insert";
$rs = mysqli_query($con, $insert) or die ("Insert error.");
if(mysqli_affected_rows($rs)){
echo"<br>Record Inserted successfully";
}else{
echo"<br>Container inserted scuccessfullY";
}
}
}
?>
</body>
</html>
I've been up for 12 hours trying to get data to send to my database - I connect to the server no problem when I run comments.db.php, but I cannot get the form data to show up in phpmyadmin. I have tried changing the code to backticks in the comments.inc.php file. I can click submit fine and there are no errors, but nothing is showing up in the table I created.
This is the beginning of my index file
<?php
date_default_timezone_set('US/Eastern');
include 'comments.dbh.php:';
include 'comments.inc.php';
?>
Here is the form in the index file
<?php
echo "<form method='POST' action='".setComments($mysqli)."'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message'></textarea><br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>"
?>
Here is the comments.inc.php file
function setComments($mysqli) {
if (isset($_POST['commentSubmit'])) {
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$mysqli = "INSERT INTO comments (uid, date, message) VALUES ('".$uid."', '".$date."', '".$message."')";
$result = mysqli_query($mysqli,$sqli);
}
mysqli_close();
}
I tried changing the above to the following but that didn't work either. I also tried backticks and nothing.
$mysqls = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
Here is my comments.dbh.php file
<?php
$username=""; - These are filled out but I left them blank
$password="";
$database="london34_commentsection";
$mysqli = new mysqli("localhost", $username, $password, $database);
$mysqli->select_db($database) or die( "Unable to select database");
?>
This is my phpMyAdmin Screenshot listing my table
If anyone could offer any guidance I would greatly appreciate it. Like I said, ive gone through a lot of the posts on the topic on here and just cannot figure it out.
First of all your function is not returning any value so it's useless
I edited the code and did this
index file form
<?php
echo "<form method='POST' action='comments.inc.php'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message'></textarea><br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>"
?>
make sure all your files are in the same folder
comments.inc.php
if (isset($_POST['commentSubmit'])) {
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$mysqli = "INSERT INTO comments (uid, date, message) VALUES ('".$uid."', '".$date."', '".$message."')";
$result = mysqli_query($sqli,$mysqli);
}
and I don't know, where did you defined $sqli?comments.dbh.php file
<?php
$username="root";
$password="";
$database="london34_comments";
$sqli = mysqli_connect('localhost',$username,$password,$database);
?>
let me know the errors in the comments.
So when i put the information into my registration form that information wont go to the phpmyadmin database...This is the code:
This is the index.php file:
<?php
date_default_timezone_set('Europe/Copenhagen');
include 'dbh.inc.php';
include 'comments.inc.php';
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<!--Ovo je za login -->
<?php
echo "<form method='POST' action='".getLogin($conn)."'>
<input type='text' name='uid'>
<input type='password' name='pwd'>
<button type='submit' name='loginSubmit'>Login</button>
</form>";
echo "<form method='POST' action='".userLogout()."'>
<button type='submit' name='logoutSubmit'>Log out</button>
</form>";
if (isset($_SESSION['id'])) {
echo "You are logged in";
} else {
echo "You are NOT logged in!";
}
?>
<!-- Ovo je za register -->
<?php
echo "<form action='signup.php' method='POST'>
<input type='text' name='uid' placeholder='Username'><br>
<input type='password' name='pwd' placeholder='Password'><br>
<button type='submit'>SIGN UP</button>
</form>";
?>
<br><br>
<!-- OVDE SE ZAVRSAVA -->
<iframe src="https://www.youtube.com/embed/N6sB0-jKS6c" frameborder="0" allowfullscreen></iframe>
<?php
echo "<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='uid' value='Anonymous'>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
<textarea name='message'></textarea><br>
<button type='submit' name='commentSubmit'>Comment</button>
</form>";
getComments($conn);
?>
</body>
</html>
This is the comments.inc.php file
<?php
function setComments($conn) {
if (isset($_POST['commentSubmit'])){
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
$result = mysqli_query($conn, $sql);
}
}
function getComments($conn) {
$sql = "SELECT * FROM comments";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()){
echo "<div class='comment-box'><p>";
echo $row['uid']."<br>";
echo $row['date']."<br>";
echo nl2br($row['message']);
echo "</p>
<form class='delete-form' method='POST' action='".deleteComments($conn)."'>
<input type='hidden' name='cid' value='".$row['cid']."'>
<button type='submit' name='commentDelete'>Delete</button>
</form>
<form class='edit-form' method='POST' action='editcomment.php'>
<input type='hidden' name='cid' value='".$row['cid']."'>
<input type='hidden' name='uid' value='".$row['uid']."'>
<input type='hidden' name='date' value='".$row['date']."'>
<input type='hidden' name='message' value='".$row['message']."'>
<button>Edit</button>
</form>
</div>";
}
}
function editComments($conn) {
if (isset($_POST['commentSubmit'])){
$cid = $_POST['cid'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "UPDATE comments SET message='$message' WHERE cid='$cid'";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
}
}
function deleteComments($conn) {
if (isset($_POST['commentDelete'])){
$cid = $_POST['cid'];
$sql = "DELETE FROM comments WHERE cid='$cid'";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
}
}
function getLogin($conn) {
if (isset($_POST['loginSubmit'])) {
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM user WHERE uid='$uid' AND pwd='$pwd'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
if ($row = $result->fetch_assoc()){
$_SESSION['id'] = $row['id'];
header("Location: index.php?loginsuccess");
exit();
}
}else{
header("Location: index.php?loginfailed");
exit();
}
}
}
function userLogout() {
if (isset($_POST['logoutSubmit'])) {
session_start();
session_destroy();
header("Location: index.php");
exit();
}
}
This is the signup.php file
<?php
include 'dbh.php';
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "INSERT INTO user (uid, pwd)
VALUES ('$uid', '$pwd')";
$result = mysqli_query($conn, $sql);
header("Location: index.php");
The dbh.php file just for connecting the database
<?php
$conn = mysqli_connect("localhost", "root", "", "logintest");
if (!$conn){
die("Connection failed: ".mysqli_connect_error());
}
//
<?php
date_default_timezone_set('Europe/Copenhagen');
include 'dbh.inc.php';
include 'comments.inc.php';
?>
And the editcomment.php file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<?php
$cid = $_POST['cid'];
$uid = $_POST['uid'];
$date = $_POST['date'];
$message = $_POST['message'];
echo "<form method='POST' action='".editComments($conn)."'>
<input type='hidden' name='cid' value='".$cid."'>
<input type='hidden' name='uid' value='".$uid."'>
<input type='hidden' name='date' value='".$date."'>
<textarea name='message'>".$message."</textarea><br>
<button type='submit' name='commentSubmit'>Edit</button>
</form>";
?>
</body>
</html>
Ok so I think the problem may lie in the index.php file where the register form is placed or in the signup.php file but I do not know how to fix It.
I've designed a form to insert data to a database on localhost.
<form action='' method='post'>
<input type='submit' name='CRUD' value='New Data'>
<br><br>
<input type='submit' name='CRUD' value='Retrieve Data'>
<br>
<hr>
</form>
<?php
error_reporting(0);
$x = $_POST['CRUD'];
if ($x == "New Data") {
require 'part1.php';
}
?>
I then made a form to insert the data on another file.
<form method='post'>
<label for='site'>Name: </label>
<input type='text' name='site'>
<br><br>
<label for='date'>Date: </label>
<input type='date' name='time'>
<br><br>
<label for='page'>Web URL: </label>
<input type='url' name='page'>
<br><br>
<label for='desc'>Description: </label>
<input type='text' name='desc'>
<br><br>
<input type='submit' name='finish' value='Go'><input type="reset">
</form>
<?php
if ( !empty( $_POST) ){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "assignment5";
$resource = $_POST['site'];
$date = $_POST['time'];
$url = $_POST['page'];
$explain = $_POST['desc'];
// Create connection
$conn = new mysqli('localhost', 'root', $password, $dbname) or
die("Unable to connect");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO thedata (date, Name, URL, Description)
VALUES ('$date', '$resource', '$url', '$explain')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$conn->close();
}
?>
On there own they work as intended but what I need is to have both forms on the same page. Doing this gives an error where default data is inserted and not the form's inputs.
If you want to put the 2 forms in the same page , you have to give each form a submit button .. be aware to use the same submit button to the same forms
I've created this registration form and i want to insert a text file like CV in mysql database named st_login including the table named login.In the following code i've only created a form for registration purposes and want to insert also an option allowing the user to insert a text file (CV)into the registration form and this text file to be saved into that database using Php.
This is my php code:
<html >
<head>
<title></title>
</head>
<body>
<?php
print ("<form action='register.php' method='post'>
<p>Name
<input type='text' name='firstname' />
</p>
<p>Surname
<input type='text' name='lastname' />
</p>
<p>Username
<input type='text' name='username' />
</p>
<p>Password
<input type='password' name='password' />
<p/>
<input type='submit' value='Register'/>
</form>");
extract ($_POST);
if( !($database=mysql_connect("localhost","root",""))||!(mysql_select_db("st_login",$database)) )
print("Could not connect");
if(isset($_POST['firstname'] )&&isset($_POST['lastname'])&&isset($_POST['username'])&&isset($_POST['password']) ){
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$username=$_POST['username'];
$password=$_POST['password'];
$query = "INSERT INTO login (firstname, lastname, username,password) VALUES ('$firstname', '$lastname', '$username','$password')";
}
if ( !empty($firstname)&&!empty($lastname)&&!empty($username) &&!empty($password) )
{
if(!($result=mysql_query($query,$database)))
{
print("Could not execute query");
die (mysql_error());//ose error
}
else echo "You have been registered successfully";
}
else echo "Fill in all the blank fields";
mysql_close($database);
?>
</body>
</html>