I'm trying to add a php button on to my site, however it doesn't seem to be working.
The delete php script is;
if(isset($_POST["delete"])) {
$delquery = "DELETE FROM emails WHERE ID=$_POST["delete"]";
mysqli_query($connection, $delquery);
}
And the form looks like which is on the same file;
<form action="email-response.php" method="post">
<input type="hidden" name="hidden" value="<?php echo $row['ID']; ?>">
<input type="submit" name="delete" value="delete">
</form>
However whenever I clicked the delete button nothing is happening.
In relation to the reply saying that my $connection function is wrong, here is the function however its working as it is fetching my information for my posts.
define("DB_SERVER", "myservername");
define("DB_USER", "myusername"); //username
define("DB_PASS", "mypassword"); //password
define("DB_NAME", "mydbname"); // database name
$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
} else {
echo "connected";
}
The element you wish to delete is named hidden and not delete.
That is what your submit button is named as (delete).
Change your query to this:
if(isset($_POST["delete"]) && !empty($_POST["hidden"])){
$id = mysqli_real_escape_string($connection, $_POST["hidden"]);
}
$delquery = "DELETE FROM emails WHERE ID='$id'";
mysqli_query($connection, $delquery) or die(mysqli_error($connection));
if(mysqli_affected_rows($connection)){
echo "It was really successful.";
}
isset($_POST["delete"]) is to check if the submit button was clicked.
Using mysqli_affected_rows() will show you if your query was truly successful.
This is a function I've grown to use more often.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
Sidenote: Using your present method, leaves you open to SQL injection.
Use mysqli with prepared statements, or PDO with prepared statements, they're much safer.
Your connection is good.
Here is the solution which will work and is simple:
Your form:
<form action="#" method="post">
<input type="hidden" name="to_delete" value="<?php echo $row['ID']; ?>">
<input type="submit" name="delete" value="delete">
</form>
Your delete script:
if(isset($_POST["delete"]))
{
$delquery = "DELETE FROM user WHERE ID=".$_POST['to_delete']."";
mysqli_query($connection, $delquery);
}
Related
I want it so that when the user types into the textarea/input and clicks save changes, the information they input has been added and saved into the database. Below is my code:
$name = $_SESSION['u_name'];
$uid = $_SESSION['u_uid'];
$id = $_SESSION['u_id'];
$con = mysqli_connect("localhost", "root", "pass123", "db_name");
if ($con->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "<script type='text/javascript'>alert('connection failed. try again');</script>";
}
$remind1 = $_POST['remind1'];
$remind2 = $_POST['remind2'];
$remind3 = $_POST['remind3'];
$remind4 = $_POST['remind4'];
$remind5 = $_POST['remind5'];
if (isset($_POST['updBtn'])){
$sql = "UPDATE reminders SET remindone='$remind1' WHERE username='$uid'";
if ($con->query($sql) === TRUE) {
echo "<script type='text/javascript'>alert('Updated successfully');</script>";
}else{
echo "<script type='text/javascript'>alert('error while updating. try again');</script>";
}
}
Below is the corresponding HTML:
<form action="body.php" method="post">
<input type="submit" class="sideBtn" value="Save Changes" name="updBtn"><br>
<input type="text" class="event" name="remind1"><br>
<input type="text" class="event" name="remind2"><br>
<input type="text" class="event" name="remind3"><br>
<textarea class="event" name="remind4"></textarea><br>
<textarea class="event" name="remind5"></textarea><br>
</form>
Ideally what would happen, is that whatever the user types into the textarea/input is updated in the database, then they can access and later tweak the text if they need to.
I have been able to pinpoint that my problem is somewhere along the $_POST variables in my PHP as, if I were to substitute the aforementioned variable with a string as such:
$sql = "UPDATE reminders SET remindone='hello' WHERE username='$uid'";
...it works perfectly. But with when using the POST variable, it does not work.
How can I fix this mistake of mine and make it so that the user is able to post text into the database? Is the $_POST variable required here or is there another method to achieve this?
I want to go from a HTML form to a SQL database using PHP.
Here is my current code:
PHP:
if(isset($_POST['submitIpG']))
{
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO GmodServers (ipaddress)
VALUES ($_POST['submitIpG'])";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
echo "<br />\n";
}
As you can see it's hard coded with a specific IP. However, I want it to use an IP from the user input in my form.
HTML:
<form method="post">
<input type="value" name="submitIpG" value=""/>
<input type="submit" name="submitIpG" value="ADD"/>
</form>
How do I do this? I've tried things such as:
$sql = "INSERT INTO GmodServers (ipaddress)
VALUES ('$_POST['submitIpB']')";
Without success.
Thanks!
You should set the action php script in form html also your inputs should be like
<form method="post" action="your_php_file.php">
<input type="text" name="submitIpG" placeholder="the ip input"/>
<input type="submit" name="submit" value="ADD"/>
</form>
and your_php_file.php should be like
$sql = "INSERT INTO GmodServers (ipaddress)
VALUES ('$_POST['submitIpG']')";
But the most important part is DO NOT USE sql queries like that. Please consider using prepared statement.
There is a good and simple example at w3school. this is the link you may want. https://www.w3schools.com/php/php_mysql_prepared_statements.asp
Hello guys i need some help.I connected to database from server and can insert some info like $sql = "INSERT INTO Posts (Text_Post) VALUES ('Sample Text')";. Now I want to save on click text from <input type="text" /> to database. Can you tell me what i am doing wrong.
<?php
$servername = "google.com";
$username = "google";
$password = "google";
$dbname = "google";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Submit'])) {
$sql = "INSERT INTO Posts (Text_Post) VALUES ('".$_POST['text']."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>anonim</title>
</head>
<body>
<form name="form" action="" method="post">
<input type="text" name="text" id="text" value="Salut" /=>
<input type="submit" id="Submit" />
</form>
</body>
</html>
You're missing the name tag on your submit. When data is POST'ed to the server, it uses the name tag.
<input type="submit" id="submit" name="Submit">
Remember to watch your Capitals also - (since you're checking if Submit is SET then you need to POST the submit).
You could just do:
if(isset($_POST['text'])) {
Also, going off the comments: I'd suggest taking a look at this link because you're prone to SQL Injections.
when we are going to post a form using POST or GET. we should always give name to all our fieds so we get get them just using $_POST['name'] or $_GET['name']. In Your case just give a name to your submit tag and check whether data is submitted or not.
replace
<input type="submit" id="Submit" />
with
<input type="submit" id="submit" name="submit">
and check it like
if(isset($_POST['submit'])) {// it will only check where form is posted or not
// your code...
}
I have two different directories on my wampserver, this code works on one, but not on the other, I don't understand why.
PHP
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
$msg = "";
if (!isset($_SESSION['username'])) {
header('Location: login.php');
die();
}
if(isset($_SESSION['username'])) {
if (isset($_POST['submit']))
{
$className = $_POST['className'];
$classColour = $_POST['classColour'];
include_once("connection.php");
$sql = "INSERT INTO class (className, classColour) VALUE ('$className', '$classColour')";
mysqli_query($dbConnection, $sql);
$msg = "New class '" . $className . "' added.";
} else {
$msg = "No class added yet.";
}
}
?>
HTML
<form method="post" action="add_class.php">
<input type="text" name="className" placeholder="Class" />
<input type="text" name="classColour" placeholder="Colour" />
<div><input type="submit" name="submit" value="Add" class="btn butn-orange"/></div>
</form>
This is in the file "add_class.php" and I've tried many different things, putting single quotes (`) around the table columns in the $sql but still, it won't work. I've tried adjusting the names in the table, which had underscores but now have camelCasing, still made no difference. This code works perfectly in another directory, can someone please tell me why this is happening and possibly propose a solution? Thank you in advance.
P.S My connection works because I inserted a new row via phpmyadmin and looped through the database printing every existing "className" and it worked, I just can't insert from the php script.
connection.php
<?php
$dbConnection = mysqli_connect("localhost","root", "", "main");
if(mysqli_connect_errno())
{
echo "Failed to connect" . mysqli_connect_error();
}
?>
When asked if you've had assigned the sessions, you replied I assigned this when the user logs in. Moving forward from that, let's assume the assigned session as one written below:
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
$_SESSION['username'] = "HawqasKaPujaari";
$msg = "";
// this fails, as session is already set.
if (!isset($_SESSION['username'])) {
header('Location: login.php');
die();
}
if(isset($_SESSION['username'])) {
if (isset($_POST['submit']))
{
$className = $_POST['className'];
$classColour = $_POST['classColour'];
include_once("connection.php");
$sql = "INSERT INTO class (className, classColour) VALUES ('$className', '$classColour')";
mysqli_query($dbConnection, $sql);
echo $msg = "New class '" . $className . "' added.";
} else {
$msg = "No class added yet.";
}
}
?>
<form method="post" action="">
<input type="text" name="className" placeholder="Class" />
<input type="text" name="classColour" placeholder="Colour" />
<div><input type="submit" name="submit" value="Add" class="btn butn-orange"/></div>
</form>
Note: The only changes I made were to change the action="" empty and changed VALUE to VALUES in your query.
connection.php:
<?php
$dbConnection = mysqli_connect("localhost","root", "", "main");
if(mysqli_connect_errno())
{
echo "Failed to connect" . mysqli_connect_error();
}
?>
As the above posted code seemed to be correct and was bugging me so I thought of testing it myself by creating the database/tables and it seemed to work properly without any errors. I have posted the relevant pictures below:
Note: Make sure you have the connection.php file in the same
directory as the add_class.php.
My PHP code to POST to my SQL on Godaddy's hosting is not working for some reason.. I added debug statements but I'm just not sure why it's not working. It's driving me crazy.
Here's my file named "homepage.php":
<?php
if (isset($_POST['submitted'])) {
include('mysql_connection.php');
$entry = $_POST['entry'];
$sql = "INSERT INTO posts (typed) VALUES ('$entry')";
if (!mysqli_query($dbcon, $sql)) {
die('Error inserting text.');
}
$newentry = "One entry added to the database.";
}
?>
HTML
<html>
<head> </head>
<body>
<center>
<form method="post" action="homepage.php">
<input type="hidden" name="submitted" value="true" />
<input type="text" name="entry" maxlength="200" />
<br></br>
<input type="submit" value="insert" />
</form>
</center>
<?php echo $newentry?>
</body>
</html>
And my database is named "subpost-db" with the table "posts" and a column named "typed" with VARCHAR values.
My SQL connection file is named "mysql-connection.php" and here's the code:
<?php
DEFINE ('DB_USER', 'xxxxxxxxxx');
DEFINE ('DB_PSWD', 'xxxxxxxxxx');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'subpost-db');
$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);
if (!$dbcon) {
die('Error connecting to the requested database. ');
}
?>
By the way, when I go to mysql-connection.php on my website, the debug message does not pop up.
When I click "insert" after typing something on my form, it reloads homepage.php, where my form is, and only displays the text that says "Error inserting text." but I'm not sure what the problem is.
You are getting an error from your query so add the correct reporting to the test after the code that issues the query to the database and it will tell you what is wrong.
<?php
// from Fred-ii- comment
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_POST['submitted'])) {
include('mysql_connection.php');
$entry = $_POST['entry'];
$sql = "INSERT INTO posts (typed) VALUES ('$entry')";
if (!mysqli_query($dbcon, $sql)) {
die('Error inserting text. ' . mysqli_error($dbcon) ); //<-- changed line
}
$newentry = "One entry added to the database.";
}
?>