database wont update after clicking submit button - php

After connecting to the database, here is the simplest code that should update my database after clicking the submit button :
<?php
if (isset($_POST['update'])){
$sql = "UPDATE accounts SET download='Yes' WHERE id=15 ";
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method="post" >
<input type="submit" value="Update " name="update">
</form>
</body>
</html>
I dont know what am i doing wrong.

You first need to connect to a database.
$con = mysqli_connect("localhost","my_user","my_password","my_db");
Check connection if successful
if (mysqli_connect_errno()){
die( "Failed to connect to MySQL: " . mysqli_connect_error() );
}
If successful execute your query now.
$sql = "UPDATE accounts SET download='Yes' WHERE id=15 ";
$result = mysqli_query($con,$sql);
Check if query succeeded.
if( $result ) {
echo "Update successful!";
}
else {
echo "Updated Failed!";
}

Try to use error and exception handling codes, it will be helpful to fix the errors

Related

HTML SQL server connection using PHP

I have a HTML form and I want to use the results to filter the data available in a SQL file. I'm trying to connect to a local MySQL server but It doesn't work, i don't achieve to enter in the table() function.
I use the following code:
<form id="form_id" action="food_values.php" method="post" name="myform">
...
<input onclick="table" type="button" value="Submit">
</form>
The php file contains the following code:
<html>
<body>
<?php
function table(){
echo ("INSIDE");
$con = mysql_connect("localhost","root"," ");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo "CONNECTED";
}
}
?>
</body>
</html>
It doesn't show anything... Do you know where is the error?
You can not execute a PHP-Function with "onclick".
Execute PHP function with onClick
With a form, you load the whole .php-file if you click on the submit.
The following code should work for you:
<html>
<body>
<?php
echo ("INSIDE");
$con = mysql_connect("localhost","root"," ");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
echo "CONNECTED";
}
?>
</body>
</html>
And the HTML
<form id="form_id" action="food_values.php" method="post" name="myform">
...
<input type="submit" value="Submit">
</form>
Change
<input onclick="table" type="button" value="Submit">
into
<input type="submit" name="submit" value="Submit">
As the action in form takes care of where to direct the data in php.
and in 'food_values.php' page you dont have to define function as its not javascript.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
This will connect the database.
And for validation of the form follow the SQL commands. For futher detail visit
https://www.w3schools.com/php/php_form_validation.asp

Pre Complete HTML Form with PHP and MYSQL

I am creating a simple page which updates a single record tempKey=1, single field reqdTemp MySQL dBase. I have the form working fine; it updates the record, then returns to the initial form ready for the user to change the temperature again.
Q: I would like the form to be pre-populated with the existing information from the database so the user sees the current required temperature about to be changed. I'm not sure where to start!!
The form, updateTemperature.php, is this:
<html>
<body>
<h1>RPi BBQ - Set Temperature</h1>
<form action="insert.php" method="post">
<p>Set Temperature: <input type="text" name="setTemp" /></p><br><br>
<input type="submit" value="Set Temperature" />
</form>
</body>
</html>
The post script, insert.php is this:
<?php
require_once 'login.php';
$con=mysqli_connect($hh,$un,$pw,$db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo 'Connected successfully';
$sql = "UPDATE PiBQ_Temp SET reqdTemp = '$_POST[setTemp]' WHERE tempKey = 1";
mysqli_query($con,$sql);
echo "1 record added";
header ('location: PiBQ_Temp2.php');
mysql_close($con)
?>
To pre-populate the form, query the database for the current value and set that in the returned HTML. So your updateTemperature.php could become something like this:
<?php
require_once 'login.php';
$con=mysqli_connect($hh,$un,$pw,$db);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo 'Connected successfully';
$currentTemp = 100; // some default
$sql = "SELECT reqdTemp FROM PiBQ_Temp WHERE tempKey = 1";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$currentTemp = $row['reqdTemp'];
}
mysql_close($con);
?>
<html>
<body>
<h1>RPi BBQ - Set Temperature</h1>
<form action="insert.php" method="post">
<p>Set Temperature: <input type="text" name="setTemp" value="<?= $currentTemp ?>" /></p><br><br>
<input type="submit" value="Set Temperature" />
</form>
</body>
</html>

When i check my database there are no new entries. What am i doing wrong?

<!DOCTYPE html>
<html>
<head>
<title>Sign up page</title>
</head>
<body>
<form method="post" action="signup_redirect.php">
username: <input type="text" name="username" placeholder="username"><br>
Password: <input type="password" name="password" placeholder="password"><br>
<input type="submit" value="Create Account">
</form>
// The user would enter the desired username and password.
<?php
if(isset($_POST['submit'])) {
$dbCon = mysqli_connect("localhost", "root", "", "test");
if (mysqli_connect_errno()) {
echo "Failed to connect" .mysqli_connect_error();
}
$sql = "INSERT INTO test (username, password)
VALUES ('".$_POST['username']."','".$_POST['password']."')";
}//Takes the values that the user has submitted and inserts them into my table called "test".
?>
</body>
</html>
I have a form which would take the users desired details then if the submit button is pressed it runs through the rest of the php code.I also have the sql statement which is meant to add data but when i check my database there are no new entries.
$sql = "INSERT INTO test (username, password)
VALUES ('".$_POST['username']."','".$_POST['password']."')";
After that run query
$sql_qry=mysqli_query($dbCon, $sql);
add
if (mysqli_query($dbCon, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($dbCon);
}
after, $sql query you have written

Input data does not get stored in database, but no error

I need to get a user's comment and store in the database table column 1 and display the entered comment in different table. The code works fine with no errors, but the comment does not get stored in the database.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "escalation";
$conn = new mysqli($server, $username, $password, $database);
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
if (isset($_POST['Submit'])) $Comment = isset($_POST['Comment']) ? $_POST['Comment'] : '';
$sql = "INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res = $conn->query($sql);
if ($res) {
echo "Successful";
echo "<br />";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
</body>
</html>
try this.Notice the opening and closing bracket.
<head>
<title></title>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "escalation";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['Submit'])){
$Comment=isset($_POST['Comment']) ?$_POST['Comment']:'';
$sql="INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res=$conn->query($sql);
if($res){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}else {
echo "ERROR";
}
}
?>
</body>
</html>
Another info and a sort of advice though it does not concern to your question is please use prepared statement that will help prevent sql injection.
You can read php manual about mysqli prepared statement here .
You might also want to check PDO prepared statement click here.
You might also want to check this this full helper class for your crud operation that i personally created.That also uses PDO prepared statement.
Hope that helps somebody.
Try this:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method='post' action=''>
<input type="text" name='Comment'/>
<input type="Submit" value="Submit" name="Submit" />
</form>
<?php
$server="localhost";
$username="root";
$password="";
$database="escalation";
$conn = new mysqli($server, $username, $password, $database);
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
if(isset($_POST['Submit'])){
$Comment=isset($_POST['Comment']) ?$_POST['Comment']:'';
$sql="INSERT INTO css(Dis_Cmt)VALUES('$Comment')";
$res=$conn->query($sql);
if($res){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
}
?>
</body>
</html>
Did you check if the $_POST is giving the output you want. If yes ,then try the mysql command on the commandline and see if it shows any error ... Your command should work, i tried it in my commandline, it worked.
Either you havent set the correct data types and lengths of values in the database table or try this:
A safe command which always works is
"INSERT INTO `css`(`Dis_Cmt`)VALUES('.$Comment.')"
First thing I notice:
if ($conn->connect_error) {
die("connection failed:" . $conn->connect_error);
}
should be
(!$conn)
Put a echo before the INSERT. If its show up, the problem maybe the
query or the table field;
Insert any value in css table using the
phpmyadmin for testing;
Use prepared statement to avoid sql
injection. Below, a example.
$Comment = $_POST['Comment'];
$sql = "INSERT INTO css (Dis_Cmt) VALUES (?)";
$statement = $conn->prepare($sql);
$statement->bind_param('s', $Comment);
if($statement){
echo "Successful";
echo "<BR>";
echo "<a href='Uploadphp.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
$statement->close();
Good luck!

Inserting data into a MYSQL database

I am having trouble inserting data into the database 'justrated'. Once the user has entered their business name it should create a new entry in the table 'businesses'. For some reason I cannot get it so that the data is entered in the table. Any advice is gladly appreciated.
CODE:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form>
<input type="text" name="BusinessName" method="POST">
<input type="Submit" value="submit" name="submit" method="POST">
</form>
<?php
if (isset($_POST["submit"])){
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('".$_POST['BusinessName']."' )";
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
</body>
</html>
One of your problems is that $_POST['BusinessName'] is empty because the form was submitted using a GET request, not a POST request. The method=POST attribute goes on the <form> element. Eg:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit">
</form>
Also, you should escape the data properly before you insert it into the database:
$sql = "INSERT INTO businesses (BusinessName)
VALUES ('" . $conn->real_escape_string ($_POST['BusinessName']) . "' )";
Furthermore, in these two lines:
mysql_query($sql);
if ($conn->query($sql) === TRUE) {
you try to execute the same query twice using both the MySQL and MySQLi extension. You should remove the first line.
HTML Code
<form method="post" action="test1.php">
<input type="text" name="BusinessName" >
<input type="Submit" value="submit" name="submit" >
</form>
PHP Code
if (isset($_POST["submit"]))
{
//create connection
$conn = new mysqli("localhost", "root", "", "justrated");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."' )";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
Don't mix the mysql & mysqli....
Html:
<form method="POST">
<input type="text" name="BusinessName">
<input type="Submit" value="submit" name="submit" >
</form>
Php:
Use
$conn->query($sql); not mysql_query()
hello please check this one i hope this working for you
$sql = "INSERT INTO businesses (`BusinessName`)
VALUES ('".$_POST['BusinessName']."' )";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

Categories