I have a table where user inserts data and the user id should be automatically added to the table which he is accessing.
$sql = "INSERT INTO leadinform (lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid) VALUES ('$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate') SELECT admin.uid FROM admin where admin.username = '$user_check' ";
This is the code I am using but it doesnt seem to work.
where am I wrong ?
Use this:
$sql = "INSERT INTO leadinform
(lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid)
SELECT '$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate', admin.uid
FROM admin
WHERE admin.username = '$user_check'"
And please check insert-select.
try this
$sql = "INSERT INTO leadinform (lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid)
VALUES ('$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate',(SELECT admin.uid FROM admin where admin.username = '$user_check' limit 1)) ";
Check this:
$sql_query = "INSERT INTO leadinform (lead_compname,lead_add,lead_city,lead_compno,lead_cp,lead_cpd,lead_cpno,lead_cpemail,prodtype,prodmodel,value,clodate,uid)
VALUES ('$leadname','$leadadd','$leadcity','$leadcompno','$leadcpname','$leadcpdesig', '$leadcpno','$leadcpmail','$prodtype', '$model','$value','$clodate',(SELECT admin.uid FROM admin where admin.username = '$user_check' limit 1)) ";
Related
I have been trying to update a field in database using php, everytime I run the script there is no effect on the table. Here's how my code looks :
$sql="UPDATE users set sentMsg = $msg+1 where username = '$username' ";
$result = $link->query($sql);
where $link is the connection variable which is working fine with other queries.
Here's the table structure.
The $result variable is returning true.
I am unable to understand where the actual problem is.
Try this
$sql="UPDATE `users` SET `sentMsg` =".($msg+1)." WHERE `username` ='".$username."'";
$result = mysqli_query($link,$sql);
Thats what i could make out of your code till now.
try this;
$newCount = $msg+1;
$sql="UPDATE users set sentMsg = $newCount where username = '$username' ";
$result = $link->query($sql);
or
$sql="UPDATE users set sentMsg = ".($msg + 1)." where username = '$username' ";
$result = $link->query($sql);
So I am trying to write a php script that records one vote increment per user. Voters a are presented with a list of prospective candidates then clicks once on their name.
Now on attempt of a second time i hope to get a notification allowing this action. Please help.
<?php
if(isset($_POST['vote']))
{
$sql3='INSERT INTO sessions (memberID, postid, email, voted) VALUES ("","", "", 1;)';
$result3 = mysqli_query($con, $sql3);
}
// $count = mysqli_num_rows($result2);
$candidate_name = null;
$vote = $_POST['vote'];
mysqli_query($con, "UPDATE tbCandidates SET candidate_votes=candidate_votes+1 WHERE position_id='$vote'");
// $count = mysqli_num_rows($result2);
if ( $count<1) {
//$sql3='INSERT INTO sessions (memberID, postid, voted) VALUES ("", memberbers.memberID,"1")';
//$result = mysqlI_query("select id from Users where username ='".$_SESSION['email']."'");
//$result = mysqli_query($con, $sql3);
// $count = mysqli_num_rows($result2);
} else {
echo"You have voted already";
}
Put a "unique" flag on your memberID column in your SQL table. In that case when you try to insert a new row with the same memberID, it will not work.
Then catch the SQL write fail, and display a message if it gets to it.
In my project, I have two tables. They are login and employee table.
Employee table contains name, NIC etc. and login table has NIC, password.
NIC is username of the system. I wanted to know how to retrieve logger name from the session. This is the code I have added to the system, but it shows error as systax error. Could anyone help me to solve this?
<?php
$sql = 'SELECT tbl_employee.Fname
FROM tbl_login , tbl_employee
WHERE tbl_employee.NIC = tbl_login .$_SESSION['username'] ';
?>
You don't need to refer to tbl_login (you already have the NIC in your $_SESSION['username'] var ) and if NIC is a string you should enclose it in quotes
<?php
$sql = 'SELECT tbl_employee.Fname
FROM tbl_employee
WHERE tbl_employee.NIC = "' . $_SESSION['username'] . '"';
?>
Try this. This should work.
You can capture the session in toa variable
$sess = $_SESSION['username'];
$sql = "SELECT tbl_employee.Fname
FROM tbl_login , tbl_employee
WHERE tbl_employee.NIC = { 'tbl_login' .$sess } ";
Or, If you dont want to use one more variable,
$sql = "SELECT tbl_employee.Fname
FROM tbl_login , tbl_employee
WHERE tbl_employee.NIC = tbl_login" .$_SESSION['username'];
$query = "SELECT username,userid FROM user WHERE username = 'admin' ";
$result=$conn->store_result();
$result1=$conn->store_result();
Can I store same result in two diffrent variable? $result and $result1?
Use this
$query = "SELECT username,userid FROM user WHERE username = 'admin' ";
$result=$conn->store_result();
$result1=$result;
Hello I have 3 fields on input form which are set via POST method to external php
$id=$_POST['id'];
$nombre=$_POST['nombre'];
$cedula=$_POST['cedula'];
where I would like to make a search option depending on which field have data inside it or if a user put data in all 3 or in only 2 fields to search from the input fields which are not NULL fields in the same table where there is a result.
my sql query is something like that $sql = "SELECT * FROM users WHERE userID = $id AND nombre = $nombre AND cedula = $cedula) ";
obviosly which is not working, what should I do to make it work. Do I need to change only the query or I need to put something before it to check first what is not NULL. Thanks
Firstly, your SQL statement should be updated to have enclosed ' (commas) around string values.
So, modify it to:
$sql = "SELECT * FROM users WHERE userID = '$id' AND nombre = '$nombre' AND pass = '$pass'";
// ----------------------------------------^---^--------------^-------^------------^-----^
Second thing is that you should search a field only when it has a value otherwise, it of no use.
So, your modified PHP code should be:
$sql = "SELECT * FROM users WHERE 1 ";
if (!empty($id)) {
$sql .= " AND userID = '$id' ";
}
if (!empty($nombre)) {
$sql .= " AND nombre= '$nombre' ";
}
if (!empty($pass)) {
$sql .= " AND pass= '$pass' ";
}
And your Database will be searched for the fields only if they have data filled in the form.
Try to add quote:
$sql = "SELECT * FROM users WHERE userID = ".$id." AND nombre = ".$nombre." AND pass = '".$pass."' ";
Yes, you will need to put a check before which will ignore the fields which are null.
Also, you would need to put the $variable inside single quotes ' if they are VARCHAR or CHAR types.