How to put the output query from mySQL to php int variable - php

I want to do a query to get the last id (int) in a table to create a new row with that last id + 1 but actually this just put all rows with the same id
my code:
<?php
$servername = "localhost";
$user = "root";
$pass = "dbpass";
$dbname = "site";
$mail = $_POST['mail'];
$password = $_POST['password'];
// Create connection
$conn = mysqli_connect($servername, $user, $pass, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sqlID = "SELECT MAX(id) FROM `login`;";
if ($result = mysqli_query($conn, $sqlID)) {
$id = mysqli_fetch_row($result);
}
settype($id, "int");
$id = $id + 1;
$sql = "INSERT INTO login (`id`,`mail`,`password`)
VALUES ('".$id."','".$mail."','".$password."');";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>

mysqli_fetch_row returns always an array, also if there is only 1 element. So the MAX(id) in in $row[0].
Fixing this, you also don't need to use settype.

If your id is autoincrement, change this:
$sql = "INSERT INTO login (`id`,`mail`,`password`)
VALUES ('".$id."','".$mail."','".$password."');";
to:
$sql = "INSERT INTO login (`mail`,`password`)
VALUES ('".$mail."','".$password."');";
Then get rid of all code from $sqlID to $id + 1; (for tidyness)

Related

how to make previous and next button in php

I'm fetching my songs from MySQL database and I'll be adding more song in the futures so I can't hard coded the max number so I'll be really appreciated if I can get any help or suggestion how to do it.
you can use the following query to get the total records in DB
$query = 'SELECT count(*) as total FROM song';
for checking if the max is reached
<? if ($page*$recordsPerPage < $total) : ?>
Next
complete example for hiding the next
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!isset($_GET['id'])) {
$id = 1;
} else {
$id = (int)$_GET['id'];
}
$recordsPerPage = 10 ;
// $query = 'SELECT * FROM song WHERE 1 LIMIT ' . (($id - 1) * $recordsPerPage) . ' ' . $recordsPerPage;
//Here need to handle the songs data retreive
$sql = "select count(*) as total FROM songs";
$result = $conn->query($sql);
$data = $result->fetch_assoc();
if ($id*$recordsPerPage < (int)$data["total"])
echo "<a href='page/details.php?id=". ($id+1) ."'>Next</a>";
$conn->close();

MYSQL create a line with every new user

I have a little problem
I want to create a script, that creates a new line in the table, if there is a new user and in the line, change the "points" columme to zero(0)
This is my current code:
<?php
header('Content-Type: text/html; charset=Windows-1250');
$firstName = $_POST['firstname'];
$servername = "db.mysql-01.gsp-europe.net";
$username = "xxxxxxxxxx";
$password = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";
if ($conn->query($sql) === TRUE) {
echo "Thingz created successfully";
} else {
echo "Error doing sum thingz: " . $conn->error;
}
$conn->close();
?>
What i need in the cube: When there new user_id ($firstName) appear, create new line with this user name, and change the "points" columme from "null" into Zero(0)
Thanks for yout time, I appreciate it
If I understand well you want to check if the user exists or not. If user is new create new line with the user with 0 points and if exist increse points with 1.
<?php
header('Content-Type: text/html; charset=Windows-1250');
if(isset($_POST['firstname'])){
$firstName = $_POST['firstname'];
$servername = "db.mysql-01.gsp-europe.net";
$username = "xxxxxxxxxx";
$password = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// check if the user exist
$check = "SELECT * FROM `member_profile` WHERE user_id = '$firstName'";
$result = mysqli_query($conn,$check) or die(mysqli_error($conn));
$rows = mysqli_num_rows($result);
//if exist increse points with 1
if($rows>=1){
$sql = "UPDATE `member_profile` SET points = points + 1 WHERE user_id = '$firstName'";
if ($conn->query($sql) === TRUE) {
echo "Thingz created successfully";
} else {
echo "Error doing sum thingz: " . $conn->error;
}
}
//if don't exist create user with points 0
if($rows==0)
{
$query = "INSERT into `member_profile` (user_id, points) VALUES ( '$firstName' ,'0')";
$result = mysqli_query($conn,$query)or die(mysqli_error($conn));
$conn->close();
}
}
?>
Remember, I gave you an idea, the code is prone to sql inject

I am trying to run a query that takes value from one table and uses it as condition to fetch value or execute action on another table

I am trying to take the value of the topay column where torecieve equals to current session user id and use it to perform operation on the user table.
But it throws a syntax error
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bazze2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$merge = "SELECT topay FROM merge WHERE torecieve=$_SESSION[id]";
$sql = "UPDATE user SET topay2='10000000' WHERE 'id'=$merge";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Use a prepared query, and use a join.
$sql = "UPDATE user AS u
JOIN merge AS m ON u.id = m.topay
SET u.topay2 = '10000000'
WHERE m.toreceive = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $_SESSION['id']);
if ($stmt->execute()) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $stmt->error;
}

update table if exist

In my table I check values is there or not if it's there I need to increase qty but it's not working. What mistake I done?
$servername = "localhost";
$username = "username";
$password = "psw";
$dbname = "database";
$myemail ='my#gmail.com';
$image = '1';
$user = 'user';
$device = '1';
$product = '2';
$qty = '100';
$status = '1';
$orderno = '2';
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
$sql = "INSERT INTO tablemname(email,image_url,user_id,device_id,product_id,qty,status,order_no) VALUES ('$myemail','$image','$user','$device','$product','$qty','$status','$orderno') ON DUPLICATE KEY UPDATE qty=1";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
I think you want to add +1 to qty :
$sql = "INSERT INTO tablemname(email,image_url,user_id,device_id,product_id,qty,status,order_no)
VALUES ('$myemail','$image','$user','$device','$product','$qty','$status','$orderno')
ON DUPLICATE KEY UPDATE qty= qty+1";
A warning, this work only on a value that should be unique. So when i see your table, I am not sure it will work...

Why insert query does not work?

my code returns success, but it does not insert the record to the table "bekuldottkerdesek". What is causing the problem?
Even select command fails, when I start to echo some records from the table.
This is how the table looks: imgur
<?php
$servername = "***";
$username = "***";
$password = "***";
$dbname = "***";
$conn = new mysqli($servername, $username, $password, $dbname);
date_default_timezone_set('Europe/Bucharest');
$current_date = date("Y-m-d H:i:s");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$check="SELECT * FROM bekuldottkerdesek WHERE kerdes = '$_POST[kerdes]'";
$res = mysqli_query($conn,$check);
if($res->num_rows){
header("Location: /bekuld.php?hiba=1");
}
else
{
$var1 = isset($_POST['at']) ? 1 : 0;
$var2 = isset($_POST['bt']) ? 1 : 0;
$var3 = isset($_POST['ct']) ? 1 : 0;
$sql = "INSERT INTO bekuldottkerdesek (datum, kerdes, a, b, c, at, bt, ct)
VALUES ('$current_date', '$_POST[kerdes]', '$_POST[a]', '$_POST[b]', '$_POST[c]', $at, $bt, $ct)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
header("Location: /bekuld.php?hiba=2");
}
$conn->close();
?>
change the insert statment to ,error passing data from $_POST
$sql = "INSERT INTO bekuldottkerdesek (datum, kerdes, a, b, c, at, bt, ct)
VALUES ('$current_date', '{$_POST['kerdes']}', '{$_POST['a']}', '{$_POST['b']}', '{$_POST['c']}', $at, $bt, $ct)";
and change this statment
$check="SELECT * FROM bekuldottkerdesek WHERE kerdes = '$_POST[kerdes]'";
to
$check="SELECT * FROM bekuldottkerdesek WHERE kerdes = '{$_POST['kerdes']}'";

Categories