Select Queries are working properly but insert commands are not saving data in database, i think that i have not done transaction correctly or it could be roll backing transactions due to improper use of syntax
session_start();
if (isset($_COOKIE['username'])) {
$_SESSION['role'] = $_COOKIE['role'];
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['rid'] = $_COOKIE['rid'];
}
if (!isset($_SESSION['username'])) {
header('location: login.php');
}
$servername = "localhost";
$username = "johnalla_Mohsin";
$password = "Mohsin1982";
$database = "johnalla_m_billing";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
date_default_timezone_set('Asia/Karachi');
$currentdate = date("Y-m-d");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (isset($_COOKIE['username'])) {
$_SESSION['role'] = $_COOKIE['role'];
$_SESSION['username'] = $_COOKIE['username'];
$_SESSION['rid'] = $_COOKIE['rid'];
}
if (isset($_POST['invoice_no'])) {
$conn->autocommit(FALSE);
$result1 = $conn->query("select sum(cost * quantity) as total from temp_entries");
if ($result1->num_rows > 0) {
// output data of each row
while ($row1 = $result1->fetch_assoc()) {
$total = $row1["total"];
}
if ($conn->query("insert into bill (c_name, date, total) values ('$_POST[name]', '$_POST[date]', '$total')") === TRUE) {
$last_id = $conn->insert_id;
$result2 = $conn->query("select * from temp_entries");
if ($result2->num_rows > 0) {
// output data of each row
while ($row2 = $result2->fetch_assoc()) {
$entries[] = $row2;
$conn->query("insert into bill_entries (bill_id, item_name, description, cost, quantity) "
. "values ('$last_id', '$row2[item_name]', '$row2[description]', '$row2[cost]', '$row2[quantity])");
}
if ($conn->query("truncate table temp_entries") === TRUE) {
$conn->commit();
}
}
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
Related
I'm trying to create an upvote system where, after checking to see if you're logged in and after getting your userid, it checks if the table has your userid with the postid already in it and if it does then it means it was already upvoted. I just want to know what's wrong in my code, this is being used for learning, I don't need any complex thing.
Code:
if (isset($_GET['upvote'])) {
if ($_SESSION["loggedin"] == true) {
$upvoteid = $_GET["upvote"];
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id FROM users WHERE username=".$_SESSION["loggedinusername"];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$userid = $row["id"];
}
$sql2 = "SELECT userid, postid FROM upvotedposts WHERE userid='".$userid."' AND postid='".$upvoteid."'";
$result2 = $conn->query($sql);
if (!$result2->numrows > 0) {
$sql = "UPDATE posts SET upvotes = upvotes + 1 WHERE id = ".$upvoteid;
if ($conn->query($sql) === TRUE) {
echo "Sucessfully upvoted";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
} else {
echo "Failed;
}
$conn->close();
}
}
When I do click the upvote button, it simply does nothing. The issue here is that as far as I know it looks like it would work but I may be forgetting something that I am unaware of or incorrectly using something.
You are missing a closing "
echo "Failed;
should be:
echo "Failed";
I m trying to get the existing "usercouponinhand" value from "userpaytoget" table and add it to received 'id' value as from "GET" tag. Then update it to the same "userpaytoget" table "usercouponinhand" column.
But unfortunately i see this "Unexpected =" error on the line result = $conn->query($sql);
The code follows:
<?php include('usercoupondelete.php'); ?>
<?php
$servername = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "";
$mobile = $_SESSION['mobile'];
$date = date('M-d,Y H:i:s');
$date2 = date('M-d,Y');
$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM userpaytoget WHERE mobile = '$mobile' ";
result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$usercouponinhand = $row["usercouponinhand"];
$couponvalue = $_GET["id"];
$totalvalue = $couponvalue + $usercouponinhand ;
$sql2 = "UPDATE userpaytoget SET usercouponinhand = '$totalvalue', date = '$date', date2 = '$date2'
WHERE mobile = '$mobile'";
if ($conn->query($sql2) === TRUE) {
echo '';
}
else {
echo "ERROR" . $sql2 . "<br>" . $conn->error;
}
}
} else {
echo "None";
}
$conn->close();
?>
Any Help is greatly appreciated..
You are missing a $
It should be $result = $conn->query($sql);
Phpmyadmin:
Phpmyadmin Image example
if (mysql_query("SELECT setup FROM users") === 1) {
echo "One";
} else if (mysql_query("SELECT setup FROM users") === 0) {
echo "Zero";
}
On register. As defined: 0.
If table shows 0, echo Zero.
Else if table shows ID 1, echo One.
How is this done?
Solution:
$setup = mysql_query("SELECT setup FROM users");
$row = mysql_fetch_assoc($setup);
if ($row['setup'] == 0) {
echo "Zero.";
} else {
echo "One!";
}
I think you need this
First of all use mysqli
//$connection = your mysqli connection. Dont use mysql
Then properly write query if you need admin row
$query = "SELECT `setup` FROM `users` WHERE `user_id`=1 AND `username` = 'admin'";
Then execute query
$result = mysqli_query($connection, $query);
Then search for $setup value
while($row = mysqli_fetch_assoc($result)){
$setup = $row['setup'];
}
Then echo what you need
if ($setup == 1) {
echo "One";
} else if ($setup == 0) {
echo "Zero";
}
See the docs. The 'Example (MySQLi Procedural)' example is similar to what you want.
Snippet:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Changing
$sql = "SELECT id, firstname, lastname FROM MyGuests";
to
$sql = "SELECT setup FROM users";
then
if (mysqli_num_rows($result) > 0) {
to your various if checks should suffice. Not the cleanest. Seems like you could just print out the value of setup? Good luck!
I need some help finding my error on the enclosed code. When I run either of the two queries using the if ($conn->query($sql) === TRUE) { method each works correctly. But when I try to combine them with the if ($conn->multi_query($sql) === TRUE) { method. No records are uploaded. What am I doing wrong here.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connection made...";
$payload_dump = $_POST['payload'];
echo $payload_dump;
$payload_array = json_decode($payload_dump,true);
if(is_array($payload_array)){
foreach($payload_array as $row){
//get the data_payload details
$device = $row['device'];
$type = $row['data_type'];
$zone = $row['zone'];
$sample = $row['sample'];
$count = $row['count'];
$time = $row['date_time'];
$epoch = $row['epoch_stamp'];
$sql = "INSERT INTO data(device, type, zone, sample, count, date_time, epoch_stamp) VALUES('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch');";
$sql . = "UPDATE data SET date_time = FROM_UNIXTIME(epoch_stamp);";
if ($conn->multi_query($sql) === TRUE) {
//if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
$conn->close();
?>
... and yes I realize this code is not secure but it's ok for my testing purposes.
Intrinsically the code below is the same until we get to the loop where we build up an array of queries to be executed and execute the multi_query() once at the end once we leave the loop. I have removed some of the comments and statements that echo out info at the start for brevity. I hope this looks ok and works....
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";
$conn = new mysqli($servername, $username, $password, $dbname);
if( $conn->connect_error ) die("Connection failed: " . $conn->connect_error);
$payload_dump = $_POST['payload'];
$payload_array = json_decode($payload_dump,true);
if( is_array( $payload_array ) ){
$queries=array();
foreach( $payload_array as $row ){
//get the data_payload details
$device = $row['device'];
$type = $row['data_type'];
$zone = $row['zone'];
$sample = $row['sample'];
$count = $row['count'];
$time = $row['date_time'];
$epoch = $row['epoch_stamp'];
/*note: we do not need to add the semi-colon here as it gets added later when we implode the array */
$queries[]="INSERT INTO `data` ( `device`, `type`, `zone`, `sample`, `count`, `date_time`, `epoch_stamp` ) VALUES ('$device', '$type', '$zone', '$sample', '$count', '$time', '$epoch')";
}
/*
Previously the below query was being execute on every iteration
~ because $epoch is now the last one encountered in the array,
the value that is updated in ALL records is as it would have been
previously.
*/
$queries[]="UPDATE `data` SET `date_time` = from_unixtime( $epoch );";
$sql=implode( ';', $queries );
if ( $conn->multi_query( $sql ) === TRUE ) {
echo "New records created and updated successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
I seem to be having trouble getting any response from this script whatsoever. No error messages, nothing...As you can see, what I am trying to do is insert into table "yum", echo the last entry, then use that last entry to insert in a separate table. What am I doing wrong and why aren't there any error messages?
<?php
$b= $_GET["sto"];
$pb= $_GET["usto"];
$sql = "INSERT INTO yum(vara, varb)
VALUES('" . $b . "', '" . $pb . "')";
$hostname_Database = "blocked";
$database_Database = "blocked";
$username_Database = "blocked";
$password_Database = "blocked";
$mysqli = new mysqli($hostname_Database, $username_Database, $password_Database, $database_Database);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = $mysqli->query($sql);
if ($result) {
echo "ha";
$sql = $mysqli->query("SELECT vara FROM yum ORDER BY vara DESC LIMIT 1");
if($sql === FALSE) {
echo "ahahahahah";
die(mysql_error()); // TODO: better error handling
}
while($row = mysql_fetch_array($sql))
{
echo $row['vara'];
}
//$row = $result->fetch_assoc();
$sql = "INSERT INTO disco(varb, vara)
VALUES( '" . $item . "', {$row['vara']})";
$result = $mysqli->query($sql);
if ($result) {
etc...
}
}
?>
You are using
while($row = mysql_fetch_array($sql))
{
echo $row['vara'];
}
Here when $row becomes null for last entry so use foreach but as you are getting only one row so instead of all the while loop just use
$row = mysql_fetch_array($sql);
Also either use just mysql_* or mysqli_*
Here is the full code
$mysqli = mysqli_connect($hostname_Database, $username_Database, $password_Database, $database_Database);
if (mysqli_connect_errno($mysqli)) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$result = $mysqli_query($con,$sql);
if ($result) {
echo "ha";
$sql = $mysqli_query("SELECT vara FROM yum ORDER BY vara DESC LIMIT 1");