Radio button and check-box values saved aginast different Ids - php

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);

Related

Insert INTO with 3 tables

$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 a same table of sql, the first value is from another table and other value is argv[2]?

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);

(Php)Retrieve primary key of previous table and insert into current table

Im new to php scripting.. I want to know if there is any mistake in my php script that i want to fetch income_id (primary key in income table) from income table and insert it into expenses table(as foreign key)...I able to add all data into expenses table except the income_id..
<?php
//Importing our db connection script
require_once('dbConnect.php');
if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$id = $_POST['id'];
$income_id = $_POST['income_id'];
$category = $_POST['category'];
$amount = $_POST['amount'];
$date = date('Y-m-d');
$sql = "SELECT income_id from `income` where id='".$id."'";
$result = mysqli_query($con, $sql);
$rows = mysqli_fetch_array($result);
//Creating an sql query
$sql = "INSERT INTO expenses (income_id,category,amount,date) VALUES ('$rows[income_id]','$category','$amount','$date')";
//Executing query to database
if(mysqli_query($con,$sql)){
echo 'Added Successfully';
}else{
echo 'Could Not Add';
}
//Closing the database
mysqli_close($con);
}
I would suggest joining both queries:
INSERT INTO expenses (income_id, category, amount, date)
VALUES ((SELECT income_id FROM `income` WHERE id='$id'), '$category', '$amount', '$date')

add mysql row if userID do not exist in table

i'm sending a post request to this code:
$name = (string)$_POST['name'];
$id = (int)$_POST['id'];
$query = "INSERT INTO Users (name, userID) VALUES ('$name', '$id')";
$result = mysqli_query($link,$query);
Which works fine and it adds a row to the table. How do i check wether the userID all ready exist in one of the following rows?
Do it like this
$query = "SELECT COUNT(*) FROM Users WHERE userID = '$id'";
$result = mysqli_query($link,$query);
if ( mysqli_fetch_assoc($result) ) {
$message = "Already exists";
} else {
$query = "INSERT INTO Users (name, userID) VALUES ('$name', '$id')";
$result = mysqli_query($link,$query);
}
Try this
$name = (string)$_POST['name'];
$id = (int)$_POST['id'];
$res = mysqli_query($link, "SELECT * FROM Users WHERE userID = '$id' LIMIT 1 ");
if($row = mysqli_fetch_assoc($res))
{
echo "this user id is already exists";
}
else
{
$query = "INSERT INTO Users (name, userID) VALUES ('$name', '$id')";
$result = mysqli_query($link,$query);
echo "record inserted successfully ";
}
REMEMBER : always use LIMIT 1 when you trying to get exactly one result.
IF you have properly set 'id' as primary key or unique key in your table, you can use the modifier IGNORE in your query you don't get an error when you try to isert a duplicate.
Doing this will result in the row only being inserted if the value of the primary key wasn't already in the table.
$query = "INSERT IGNORE INTO Users (name, userID) VALUES ('$name', '$id')";
IF you haven't set a primary key in your table you will have to do a SELECT query to find out if a row with that id is already in your table.
Make the UserID an Unique Key.
If it already exists, your code will throw an error and the row will not be insterted.
alter table Users
add unique index Unique_user (userID (8000))
Before inserting the values to the table check whether the following user id exists in the table or not
You can do it in this way
$name = $_POST['name'];
$id = $_POST['id'];
$sql = "SELECT * FROM Users WHERE userID = '$id'";
$res= mysqli_query($sql);
$num = mysqli_num_rows($res);
if($num == 0)
{
$query2 = "INSERT INTO Users (name, userID) VALUES ('$name', '$id')";
$result2 = mysqli_query($query2);
echo "record inserted successfully ";
}
else
{
echo "Record Failed !!";
}

How to insert the same UUID in two rows of two different tables

I try next script:
// Insert data into mysql
$qry="INSERT INTO $tbl_name1 (ID, REFERENCE, CODE, NAME) VALUES (UUID(), '$REFERENCE', '$CODE', '$NAME')";
$result=mysql_query($qry);
$qry2="INSERT INTO $tbl_name2 (PRODUCT) VALUES ('$ID')"; <--- Here is a problem
$result=mysql_query($qry2)
I do not know how two insert the same UUID in two tables simultanoiusly. Please help me!
I will appreciate much your support!
DONE!!!
THE WORKING SCRIPT:
$q = "SELECT UUID() AS uid";
$res = mysql_query($q) or die('q error: '.mysql_error());
$row = mysql_fetch_assoc($res);
// Insert data into mysql
$qry="INSERT INTO $tbl_name1 (ID, REFERENCE, CODE, NAME) VALUES ('".$row['uid']."', '$REFERENCE', '$CODE', '$NAME')";
$result=mysql_query($qry) or die('err 034r '.mysql_error());
$qry2="INSERT INTO $tbl_name2 (PRODUCT) VALUES ('".$row['uid']."')";
$result=mysql_query($qry2) or die('gg2345 '.mysql_error());
Just do SELECT UUID() before you send the INSERTs and put the values into the statements in PHP. Something like this (untested):
$result = mysql_query("SELECT UUID() AS UUID") or die('SQL error: ' . mysql_error());
$row = mysql_fetch_assoc($result);
$UUID = $row["UUID"];
$qry="INSERT INTO $tbl_name1 (ID, REFERENCE, CODE, NAME) VALUES ('$UUID', '$REFERENCE', '$CODE', '$NAME')";
$result=mysql_query($qry);
$qry2="INSERT INTO $tbl_name2 (PRODUCT) VALUES ('$UUID ')"; <--- Here is a problem
$result=mysql_query($qry2)
Another way would be the use of a user-defined variable (see SQL Fiddle):
SET #UUID = (SELECT UUID() AS UUID);
INSERT INTO test1 VALUES(#UUID, "foo");
INSERT INTO test1 VALUES(#UUID, "bar");
Assuming the ID is the table Unique Index you could add before $qry2:
$ID = mysql_insert_id();

Categories