I want to execute this request but always I get problems when I change string with variable.
$sql = "INSERT INTO iotdata.cardinfo (Latitude) VALUES ('".$latsave."')" where id = .$id;
$sql = "INSERT INTO iotdata.cardinfo (Latitude) VALUES ('".$latsave."')" where id = .$id;
after "where" they consider it like string not a reserved word.
try this sir
$sql = "INSERT INTO iotdata.cardinfo (Latitude) VALUES ('".$latsave."') where id = '".$id."'";
Try this:
$sql = "INSERT INTO iotdata.cardinfo (Latitude) VALUES ('".$latsave."') where id = ".$id;
Replace
$sql = "INSERT INTO iotdata.cardinfo (Latitude) VALUES ('".$latsave."')" where id = .$id;
To
$sql = "INSERT INTO iotdata.cardinfo (Latitude) VALUES ('".$latsave."') where id =" .$id;
Related
$sql3 = "INSERT INTO users_addresses (ua_user_id,ua_address_id) VALUES ('','')";
I am new in php and my hint is to link 2 tables id's in in another one called users_addresses.When a user is registered in my database i want the user_id and address_id to clone in users_addresses(ua_user_id,ua_address_id)
My tables
$sql = "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone)
VALUES ('{$_SESSION['userinfo']['fname']}', '{$_SESSION['userinfo']['mname']}', '{$_SESSION['userinfo']['lname']}', '{$_SESSION['userinfo']['login']}', '{$_SESSION['userinfo']['email']}', '{$_SESSION['userinfo']['phone']}')";
$sql1 = "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country)
VALUES ('{$_SESSION['addressinfo']['adr1']}', '{$_SESSION['addressinfo']['adr2']}', '{$_SESSION['addressinfo']['zip']}', '{$_SESSION['addressinfo']['city']}', '{$_SESSION['addressinfo']['provinciq']}', '{$_SESSION['addressinfo']['durjava']}')";
$sql2 = "INSERT INTO notes (note_text)
VALUES ('{$_SESSION['noteinfo']['note']}')";
These are my others SQL codes for adding session's data in DB.
Just need get user_id from first sql. If you are using mysqli function, do this
// run your first sql: insert user
mysqli_query($con, $sql);
$user_id = mysqli_insert_id($con); // or mysqli::$insert_id
Next, you have $user_id variable with user id.
$sql1 = "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country)
VALUES ($'{$_SESSION['addressinfo']['adr1']}', '{$_SESSION['addressinfo']['adr2']}', '{$_SESSION['addressinfo']['zip']}', '{$_SESSION['addressinfo']['city']}', '{$_SESSION['addressinfo']['provinciq']}', '{$_SESSION['addressinfo']['durjava']}')";
mysqli_query($con, $sql);
$address_id = mysqli_insert_id($con); // or mysqli::$insert_id
$sql3 = "INSERT INTO users_addresses (ua_user_id, ua_address_id) VALUES ($user_id, $address_id)";
mysqli_query($con, $sql);
Use mysqli_insert_id() to get the unique ID of the insert table, this example uses Procedural style:
<?php
include 'connection.php';
......
$InsertSQL = "INSERT INTO users (user_fname,user_mname,user_lname,user_login,user_email,user_phone)
VALUES ('{$_SESSION['userinfo']['fname']}',
'{$_SESSION['userinfo']['mname']}',
'{$_SESSION['userinfo']['lname']}',
'{$_SESSION['userinfo']['login']}',
'{$_SESSION['userinfo']['email']}',
'{$_SESSION['userinfo']['phone']}')";
$ResultSQL = mysqli_query($conn, $InsertSQL) or die(mysqli_error($conn)); // <-- execute your query
$UserID = mysqli_insert_id($conn); // <-- get the UserID
$InsertSQL = "INSERT INTO addresses (address_line_1,address_line_2,address_zip,address_city,address_province,address_country)
VALUES ('{$_SESSION['addressinfo']['adr1']}',
'{$_SESSION['addressinfo']['adr2']}',
'{$_SESSION['addressinfo']['zip']}',
'{$_SESSION['addressinfo']['city']}',
'{$_SESSION['addressinfo']['provinciq']}',
'{$_SESSION['addressinfo']['durjava']}')";
$ResultSQL = mysqli_query($conn, $InsertSQL) or die(mysqli_error($conn)); // <-- execute your query
$AddressID = mysqli_insert_id($conn); // <-- get the AddressID
$InsertSQL = "INSERT INTO user_addresses (ua_user_id,ua_address_id)
VALUES ($UserID,$AddressID)"; // <-- INSERT INTO user_address
$ResultSQL = mysqli_query($conn, $InsertSQL) or die(mysqli_error($conn)); // <-- execute your query
?>
You should also look into SQL Injection vulnerability, check out prepared statements.
Hope that helps.
I want to insert two values into the same table of SQL. The first value comes from another table and the other value is argv[2].
This is the code that I am using but it does not work.
for($i=0;$i<=feof($getdata);$i++){
if (filter_var($data[$i][1], FILTER_VALIDATE_EMAIL)){
//echo $data[$i][1];
$email=$data[$i][1];
$type=$argv[2];
$name=$data[$i][0];
$sql ="INSERT INTO promo_user (name,email) VALUES ('$name','$email')";
mysqli_query($conn,$sql);
$uid = mysqli_insert_id($conn);
$sql ="INSERT INTO promo_type set uid =$uid";
mysqli_query($conn,$sql);
$sql = "INSERT INTO promo_type (type) values ('$type')";
mysqli_query($conn,$sql);
}
}
mysqli_close($conn);
i am trying to get radio button and check box values against the same id but both values are stored in two different ids.
Where is problem?
Help me
$sql1 ="INSERT INTO student (name,fathername)
VALUES ('$name','$fathername')";
$sql2 = "SELECT last_insert_id() as id";
$res1 = mysqli_query($conn, $sql1); //here you insert
$res2 = mysqli_query($conn, $sql2); //here you fetch the ID you inserted
$id = mysqli_fetch_array($res2)['id'];
$sql3 = "INSERT INTO information (user_id,email)
VALUES ('$id','$email')"; //here you use that said ID in your second query
$res3 = mysqli_query($conn, $sql3); //aaand you insert
Now the problem starts from here, both values are stored against different ids.
//For insertion multiple values of checkbox
$sql6="INSERT INTO information (checkbox) VALUES ('" . $checkBox . "')";
$res6 = mysqli_query($conn, $sql6);
//For insertion radio button value
$sql7 ="INSERT INTO information (radio) VALUES ('" . $gender . "')";
$res7 = mysqli_query($conn, $sql7);
if i tried to insert values like this ('$id','" . $checkBox . "') and ('$id','" . $gender . "'), it returns null values in database.
If you run multiple INSERT query it will insert with different ids. No need to use INSERT multiple queries for a single row.
Solution:
$query = "INSERT INTO information (user_id,email,check box,radio)
VALUES ($id,'$email','$checkbox','$gender')";
mysqli_query($conn,$query);
UPDATE:
You also have an issue to getting last INSERT id. You can not fetch record as:
$id = mysqli_fetch_array($res2)['id'];
You can also get last insert Id by using PHP as:
$res1 = mysqli_query($conn, $sql1);
$id = mysqli_insert_id($conn); // use after ist query.
Your Modified Code:
$sql1 ="INSERT INTO student (name,fathername) VALUES ('$name','$fathername')";
$res1 = mysqli_query($conn, $sql1);
$id = mysqli_insert_id($conn); // will return last insert id
$query = "INSERT INTO information (user_id,email,check box,radio) VALUES ($id,'$email','$checkbox','$gender')";
mysqli_query($conn,$query);
Right now, this is what I have:
$query = "INSERT INTO COMMENTS VALUES ('$user', '$comment', '$star')";
mssql_query($query, $connection);
$commentIDQuery = "SELECT SCOPE_IDENTITY() AS ins_id";
$CI = mssql_query ($commentIDQuery, $connection);
$commentID = mssql_fetch_row($CI);
$idQuery = "SELECT recipeid FROM t_recipe WHERE recipename = '$recipeName'";
$RID = mssql_query($idQuery, $connection);
$recipeID = mssql_fetch_row($RID);
$rcQuery = "INSERT INTO COMMENT_RECIPE VALUES ('$commentID[0]', '$recipeID[0]')";
mssql_query($rcQuery, $connection);
So how would I get that ins_id?
It adds it to the first table, which is comments, but not the relation table.
Using sql server 2008
What about this......
$query = "DECLARE #NewID INT
INSERT INTO COMMENTS VALUES ('$user', '$comment', '$star');
SELECT #NewID = SCOPE_IDENTITY();
INSERT INTO COMMENTS_RECIPE VALUES (#NewID, '$recipeid')";
$stmt = sqlsrv_query($conn,$query);
i'm new in php and mysql
i have a problem
i have 2 tables
<?php
$insert = mysql_query("INSERT INTO request (date, type_request, subject, customer)
VALUES (NOW(), '".$type."', '".$subject."', '".$username."')");
$fk = mysql_query("insert into feedback (id_request) select id_request from request where id_request = last_insert_id ");
?>
i've been doing that but still cannot fill the id_request in table feedback
the structure of table is like this
Table Request
id_request auto_increment not_null,-->PK
date,
type_request,
subject,
customer
Table Feedback
id_feedback auto_increment not_null,
id_request,---FK
feedback_user
can anyone give suggest how to update the foreign key
Regards
In your code
$fk = mysql_query("insert into feedback (id_request) select id_request from request where id_request = last_insert_id ");
replace last_insert_id with LAST_INSERT_ID()
since its a MySQL function and not a field.
I know I will get flamed for this, but this is how I would do it:
<?php
$date = date('Y-m-d H:i:s');
$req_query = 'INSERT INTO request (date, type_request, subject, customer) '.
"VALUES ('$date', '$type', '$subject', '$username')";
$req_result = mysql_query($req_query);
$fk_query = 'SELECT MAX(id) id FROM request '.
"WHERE date = '$date' AND type_request = '$type' ".
"AND subject = '$subject' AND customer = '$username'";
$fk_result = mysql_query($fk_query);
$fk_row = mysql_fetch_assoc($fk_result);
$fk = $fk_row['id'];
$fb_result = mysql_query("INSERT INTO feedback (id_request) VALUES($fk)");
?>