Submit Button To Update Query - php

I have researched a lot and I think my error is something super simple or very hard, either way, this is my first question on StackOverflow.
So when I run this:
$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";
it updates my database just fine but now I try to put this under a submit button and it won't work?
<?php
$link = mysqli_connect("localhost", "root", "TESTTEST", "sales");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
mysqli_query($link, $query);
if ($_POST['update'])
{
echo 'Updating...';
$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";
echo '<br>Successfully Updated';
} else {
echo 'Unsuccessful';
}
?>
It echoes successfully updated then I check back to the database and nothing changed... Hopefully, you could help me! Thank you for reading, James.
First PHP tag is there just not showing on blockquote.

move your mysqli_query under query like this.
$link = mysqli_connect("localhost", "root", "TESTTEST", "sales");
if (mysqli_connect_error()){
die("Could not connect to database");
}
if ($_POST['update']) {
echo 'Updating...';
$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = 1";
mysqli_query($link, $query);
echo '<br>Successfully Updated';
} else{
echo 'Unsuccessful';
}

firstly
$query="UPDATE `sales` SET `sales` = '40' WHERE `sales`.`id` = '1'; ";
with single quotes
Then the mysqli_query($link, $query); part.

Related

PHP/Mysqli check before update

Hey I am completely new to PHP/MySqli, I would like to check before update if Scanstatus field of given ID is already "Scanned". if its already Scanned display a message as "Already Scanned" else Update.
Below code only update and doesn't check if already exists.
<?php
$id = $_POST['id'];
$connection = mysqli_connect("localhost", "username", "passwd","dbname");
if(mysqli_connect_errno())
{
echo "failed to connect " . mysqli_connect_error();
}
if(isset($_POST['Submit']))
{
$query = "UPDATE `sales` SET `ScanStatus` = 'Scanned' WHERE `id` = $id";
$result = mysqli_query($connection,$query);
if (!$result) {
die('Error' . mysqli_error($connection));
}
else
{
echo "Successfully updated";
}
}
?>
Try following php code
$id = $_POST['id'];
$connection = mysqli_connect("localhost", "username", "passwd","dbname");
if(mysqli_connect_errno())
{
echo "failed to connect " . mysqli_connect_error();
}
else{
if(isset($_POST['Submit']))
{
$sql = "UPDATE sales SET ScanStatus = 'Scanned' WHERE id = '$id'";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
echo "Successfully updated";
} else {
die('Error' . mysqli_error($connection));
}
}
}
When click SUBMIT button "isset($_POST['Submit'])" will be true and direct into if statement. then run sql command and is if there is any result that sql query affected go into next if statement after true the condition "$result->num_rows > 0" then echo "Successfully updated".
As I understand, you want to update only if the ScanStatus field is not Scanned
So you can modify you existing query like this without the need to fetch the record:
$query = "UPDATE `sales` SET `ScanStatus` = 'Scanned' WHERE `id` = $id AND `ScanStatus` != 'Scanned'";
Just change the query to above and use:
if(mysqli_affected_rows($result) > 0 ){
Here is a full code:
<?php
$connection = mysqli_connect("localhost", "username", "passwd","dbname");
if(mysqli_connect_errno()) {
echo "failed to connect " . mysqli_connect_error();
}
if(isset($_POST['Submit'])) {
$id = $_POST['id'];
$query = "UPDATE `sales` SET `ScanStatus` = 'Scanned' WHERE `id` = $id AND `ScanStatus` != 'Scanned'";
$result = mysqli_query($connection, $query);
if(mysqli_affected_rows($connection) > 0 ){
echo "Successfully updated";
}
else {
echo 'Already Scanned';
}
}
You can use this code:
Use if(mysqli_affected_rows($mysqli) > 0 ) or no comparison at all.
Replace your code with this:
<?php
$id = $_POST['id'];
$connection = mysqli_connect("localhost", "username", "passwd","dbname");
if(mysqli_connect_errno())
{
echo "failed to connect " . mysqli_connect_error();
}
if(isset($_POST['Submit']))
{
$my_query = mysqli_query($connection, "SELECT * FROM `sales` WHERE `id` = ". $id . " AND `ScanStatus` = 'Scanned'");
if(mysqli_num_rows($my_query) > 0){
echo "Already ScanStatus is Scanned";
}
else{
$query = "UPDATE `sales` SET `ScanStatus` = 'Scanned' WHERE `id` = ".$id;
//echo $query;die;
$result = mysqli_query($connection, $query);
if(mysqli_affected_rows($connection) > 0 ){
echo "Successfully updated";
/* get new updated data */
$new_query = mysqli_query($connection, "SELECT * FROM `sales` WHERE `id` = '$id' LIMIT 1");
$new_info = mysqli_fetch_array($new_query);
echo "<pre>"; print_r($new_info);
}
else
{
echo "Not updated";
}
}
}
?>

MySQL Entry to Database Not Working

I'm going through a course on MySQL, and I'm learning how to make a user entry bit of code (email and password) where the info in the script will be put into the database on phpMyAdmin. I can't seem to get it to work? My code doesn't have any errors when I put it through an error checker. I'm also completely new to PHP and MySQL. I know it can find the database, because I can update existing data.
<?php
$link = mysqli_connect("host", "username", "password", "username");
if (mysqli_connect_error()) {
die ("There was an error connecting to the database");
}
$query = "INSERT INTO `users` (`email`, `password`) VALUES('email', 'password')";
mysqli_query($link, $query);
$query = "SELECT * FROM users";
if ($result = mysqli_query($link, $query)) {
$row = mysqli_fetch_array($result);
echo "Your email is ".$row[1]." and your password is ".$row[2];
}
?>
Created a refined version. Check it.
<?php
$link = mysqli_connect("host", "username", "password", "username");
if (mysqli_connect_error()) {
die ("There was an error connecting to the database");
}
$query = "INSERT INTO `users` (`email`, `password`) VALUES('email', 'password')";
$result = mysqli_query($link, $query);
if($result != false)
{
echo "The record has been successfully inserted.<br>";
}
else
{
echo "Error Occured in the INSERT query.<br>Error : ".mysqli_error($link);
}
$query = "SELECT * FROM users";
$result = mysqli_query($link, $query);
if($result != false)
{
echo mysqli_num_rows($result)." Records found.<br>";
while($rows = mysqli_fetch_array($result))
{
echo $rows["email"]."<br>";
}
}
else
{
echo "Error Occured in the SELECT query.<br>Error : ".mysqli_error($link);
}
mysqli_close($link);
?>
Update
It turns out I didn't set the auto_increment setting, therefore making the way I set up my database incorrect! He set up another database in the tutorials I was going through, and I found out that as he did it. Thank you everyone for the effort to help me solve my problem!
Why you don't try receiving them with php?
And simply make
$email= $POST['email']
$password= $POST['password']
And change the query to
$query = "INSERT INTO `users` (`email`, `password`) VALUES(" .$email. ", ". $password.")";

PHP Mysqli error: mysqli_query() expects parameter 1 to be mysqli, boolean given

this is not a question, I need your help. I read similar threads but I couldn't debug the problem in my code. Could you plz give the correct solution?
<?php
/*variable declaration*/
$host="localhost";
$user="root";
$pass="";
$dbname='mydatabase';
/*connection to mysql server*/
$connect = mysqli_connect($host,$user,$pass);
/*selecting database*/
$selectdb=mysqli_select_db($connect,$dbname);
if(!$selectdb){
echo 'Failed to connect. Wrong username or database.';
}else{
echo 'Connection successful.';
}
/*creating task*/
$query = "SELECT 'Name', 'Password' FROM 'db' ORDER BY 'id'";
if(mysqli_query($selectdb,$query)){
echo 'Success';
}else{
echo '<br>Failed';
}
?>
enter image description here
Modify
$query = "SELECT 'Name', 'Password' FROM 'db' ORDER BY 'id'";
To
$query = "SELECT `Name`, `Password` FROM db ORDER BY 'id'";
You just cannot use the quotes on column name on the query, either use backticks or nothing at all.
if(mysqli_query($connect,$query)){...
In this you need to use the $connect and not the $selectdb.
<?php
/*variable declaration*/
$host="localhost";
$user="root";
$pass="";
$dbname='mydatabase';
$connect = mysqli_connect($host,$user,$pass,$dbname);
/*I avoided this line & used $connect inside if() statement which worked perfectly*/
//$selectdb = mysqli_select_db($connect,$dbname);
if(!$connect){
echo 'Failed to connect. Wrong username or database.';
}else{
echo 'Connection successful.';
}
/*creating task*/
$query = "SELECT `Name`, `Password` FROM `db` ORDER BY `id`";
if($query_run = mysqli_query($connect,$query)){
echo 'Success';
}else{
echo '<br> Failed';
}

Why query not working in from my php

I have query, and i have database. When I query in dbforge(mysql) i got result. When from php, i got nothing. Can't understand why? If I write query simple like "select * from table where id = 1", its working.
Below my code:
<?php
$conn = mysqli_connect("localhost", "username", "pass", "db");
ini_set('max_execution_time', 300);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($conn, "SET CHARACTER SET utf8 ");
$result = mysqli_query($conn, "Select distinct level_two FROM reportls._report_sales_report Where level_one = 'Музыка'");
if($result == false)
{
die('INVALID QUERY: '.mysqli_error($conn));
echo "error";
}
while ($data = mysqli_fetch_row($result))
{
if(!empty($data[0]))
$sendBack = $sendBack."<option value=\"$data[0]\">$data[0]</option>";
}
echo ($sendBack);
$sendBack = '';
mysqli_free_result($result);
?>
This code return nothing. But if I paste my query into my dbforge, it returns rows.
Could you help me, or write some hits? What i wrote not correct?

undefined variable php updating mysql data [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
This is the code for attempting to do a update on mysql data errors stating undefined variable
mysql_connect ("localhost", "root", "");
mysql_select_db("supplierdetails");
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
//Run a query
$result = mysql_query ("SELECT * FROM users WHERE id= '$id'");
while ($row = mysql_fetch_array($result))
{
$username=$row['username'];
$password=$row['password'];
}
$query = "UPDATE users SET username = '$username', password = '$password' WHERE id = '$id'";
$result = #mysql_query($query);
//Check whether the query was successful or not
if($result) {
header("message= Users Updated");
}else {
die("Query failed");
}
?>
You miss the $id value?
And can use echo to debug or check script result, not header
http://php.net/manual/en/function.header.php
Please be more specific with regards to which variable is undefined.
In the code you've posted $username and $password are only set if $result returns a result, if it doesn't then your while loop will not run and therefore $username and $password will never be set.
Also $id doesn't look as if that has been set either, unless this has been set outside of the code which you have included in your question.
Hope this helps :)
you used 2 connect no need to do while and you forgot $id
$con = mysql_connect("localhost", "root", "");
mysql_select_db("supplierdetails");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$id = $_POST['id'];
$username=$_POST['username'];
$password=$_POST['password'];
$query = "UPDATE users SET username = '".$username."', password = '".$password."' WHERE id = '".$id."'";
$result = mysql_query($query);
//Check whether the query was successful or not
if($result) {
echo "message= Users Updated";
}else {
die("Query failed");
}
?>

Categories