0
I am developing an app with Cordova, is running, but when I enter the order session, displays the whole list of order want only customer use (the clause WHERE
) although it did not insert cliente_Id
, thus;
<?php
$con = mysqli_connect("localhost","root","","root") or die("connection error");
header("Access-Control-Allow-Origin: *");
session_start();
if(isset($_POST['login']))
{
$username = $_POST['username'];
$cliente_email= $_POST['cliente_email'];
$cliente_senha= $_POST['cliente_senha'];
$login = mysqli_num_rows(mysqli_query($con, "SELECT * FROM `logins` WHERE `cliente_senha`='$cliente_senha' AND `cliente_email`='$cliente_email' AND
`username`='$username'"));
if($login != 0){
echo "ok";
$_SESSION['username'] = $username;
}else{
echo "error";
}
}
mysqli_close($con);
Displays the list:
$data=array();
$q=mysqli_query($con,"SELECT * FROM `dados_clientes` ORDER BY
`dados_clientes`.`id` DESC LIMIT 1");
while ($row=mysqli_fetch_object($q)){
$data[]=$row;
}
echo json_encode($data);
Right here I picked up some tips using server side and client, beauty use in login, I enter quietly with sessionStorage
, now that the server side also has to use $_SESSION
, but most of the tips Start()
are in php pages what is not the case in html, how to startar this $_SESSION
php on the client side, as if they were talking via ajax these Sessions. If they can point a way, the rest will be easy.
<script src="js/jq.js"></script>
<script>
$(document).ready(function() {
var url = "auth.php";
$("#loginButton").click(function(){
var username=$("#username").val();
var password=$("#password").val();
var dataString =
"&username=" +username+
"&password=" +password +
"&login=";
if(
$.trim(username).length>0 &
$.trim(password).length>0
)
{
$.ajax({
type: "POST",
url: url,
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function(){ $("#login").html('Connectando...');},
success: function(data){
if(data=="sucesso")
{
localStorage.login="true";
localStorage.username=username;
window.location.href = "index.html";
}
else if(data="falha")
{
alert("Login errado");
$("#login").html('Login..');
}
}
});
}return false;
});
});
</script>
if(isset($_POST['login']))
{
$username = $_POST['username'];
$password = $_POST['password'];
$login = mysqli_num_rows(mysqli_query($con, "SELECT * FROM `logins` WHERE
`username`='$username' AND `password`='$password'"));
if($login != 0){
echo "success";
}else{
$row = mysqli_fetch_assoc($login);
if(is_array($row) && !empty($row)) {
$validuser = $row['username'];
$_SESSION['valid'] = $validuser;
$_SESSION['name'] = $row['name'];
$_SESSION['id'] = $row['id'];
} else {
echo "Invalid username or password.";
echo "<br/>";
echo "<a href='login.php'>Go back</a>";
}
if(isset($_SESSION['valid'])) {
echo "success";
}
}
}
mysqli_close($con);
Well anyway here’s my current structure,
And below the related table.
<?php
$con = mysqli_connect("localhost","root","","root") or die("connection
error");
header('Access-Control-Allow-Origin: *');
session_start();
if(!isset($_SESSION['valid'])) {
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$qty = $_POST['qty'];
$price = $_POST['price'];
$loginId = $_SESSION['id'];
// checking empty fields
if(empty($name) || empty($qty) || empty($price)) {
if(empty($name)) {
echo "<font color='red'>Name field is empty.</font><br/>";
}
if(empty($qty)) {
echo "<font color='red'>Quantity field is empty.</font><br/>";
}
if(empty($price)) {
echo "<font color='red'>Price field is empty.</font><br/>";
}
echo "<br/><a href='javascript:self.history.back();'>Go Back</a>";
} else {
$result = mysqli_query($con, "INSERT INTO products (`name`, `qty`, `price`, `login_id`) VALUES ('$name','$qty','$price', '$loginId')");
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='view.php'>View Result</a>";
}
}
}
?>
Database Structure
CREATE TABLE `login` (
`id` int(9) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `products` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(100) NOT NULL,
`qty` int(5) NOT NULL,
`price` decimal(10,2) NOT NULL,
`login_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT FK_products_1
FOREIGN KEY (login_id) REFERENCES login(id)
ON UPDATE CASCADE ON DELETE CASCADE
) ENGINE=InnoDB;
Hello guys, after several searches I’m still the same, so I decided to edit a video to make my situation clearer, you can see here [explains][1] [1]:
The example is basic and identical to the original design. Yes guys one thing I forgot to show in the video was if I aggregate the tables there yes the table 'course_details' would work it enters the data normally. but it makes no sense.
When logging in, you have to take the client ID and save to sessionStorage or localStorage. Then, each ajax request will use that client’s id. In PHP, the query to the client’s data is wrong.
– Guilherme Lirio
Also put your database structure, otherwise it is difficult to know how to mount the query.
– Guilherme Lirio
Okay, initially I want to apologize for maybe violating some stack rule by updating the code, about the hints, like the ID? can help me in the query? I developed a test site and it worked but when I went to ajax, no funf.
– input jobs
You didn’t break the rules, you just didn’t ask a specific question. I need your database structure, first of all.
– Guilherme Lirio
Okay my dear Gui as the ID? or be like point to the client id in Sesssion or Storage location, how to do and, the query I believe to have fixed, explaining that I have a related ORDERS table _fk and it is this table that I need to point to "1" client as it shows everything she has, when I list and need only one info
– input jobs
My dear William sorry the delay I was for a while isolated, and so the delay I added the structure as requested... A hug
– input jobs
In your login request, besides saving the username, save also the user ID. With this ID, you will create a query in your PHP using the WHERE of the user ID.
– Guilherme Lirio
Opa my dear, so I already did, even I can log in and recognize the user’s Yes, only in the VIEW no list and does not insert in the table"products", then I made a test with the tables without being related, well, it turns out that register normally the modified section
$q=$con->query("SELECT * FROM products WHERE login_id=".$_SESSION['id']." ");
The conclusion I reached was that when FK related "products" no funf after logged in, I will have to resort to another strategy and understand why. Thanks for your attention. Thank you very much.– input jobs
Hello folks the code is correct? someone tested, to see if I forgot something?
– input jobs
Hello person, I found the flaw, the related tables do not work in AJAX, only if it is in pure PHP, someone knows some method of relational tables using Ajax. I’m researching until today and nothing I find, my project is running manually, ta dificil. thanks!!
– input jobs
Look, relational tables work yes sia ajax, but sending to a PHP backend.
– Guilherme Lirio