I have a code
$hasil = "INSERT INTO pendaftaran VALUES ('$no_ai','$tgl_daftar','$kode_dokter','$kode_pasien','$petugas_jaga','$bayar','$status_ambil','$catatan')";
$sql = mysql_query($hasil);
foreach($_POST['cek'] as $selected){
foreach($_POST['cek'] as $selek){
$res = mysql_query("SELECT kode_cek FROM master_cek where kode_item='$selected'");
while($koko=mysql_fetch_array($res)){
print_r($koko);
$result = "INSERT INTO detail_daftar VALUES ('',$no_ai','$res','0','','','$selected')";
$sql = mysql_query($result);
print_r($result);
}
}
}
The Result is..
Resource id #5 INSERT INTO detail_daftar VALUES ('',20150206015','Resource id #5','0','','','1011')Resource id #5INSERT INTO detail_daftar VALUES ('',20150206015','Resource id #5','0','','','1011')Resource id #5INSERT INTO detail_daftar VALUES ('',20150206015','Resource id #5','0','','','1011')
I am very confused with the resource id. Please help me thank you :)
It because when query is executed using PHP MySql driver it gives you resource id from where you need to fetch actual result.
If you need to get last insert id then you will have to make one more function call
print_r(mysql_insert_id());
foreach($_POST['cek'] as $selected){
foreach($_POST['cek'] as $selek){
$res = mysql_query("SELECT kode_cek FROM master_cek where kode_item='$selected'");
while($koko=mysql_fetch_array($res)){
print_r($koko);
$result = "INSERT INTO detail_daftar VALUES ('',$no_ai','$koko['kode_cek']','0','','','$selected')";
$sql = mysql_query($result);
print_r($result);
}
}
}
You are trying to insert $res change that to $koko['kode_cek']
Insert query doesn't return array. To get inserted id of executed query you have to call last inserted id function which will return you Id of last inserted record in DB.
Below function will help to get ID of inserted record.
`mysql_insert_id();`
Related
i'm working on php and i want insert something in my sql data-base and get the row's inserted primary key in the same time so i've done something like this to insert
$SQL = "INSERT INTO `saisie` (`sid`, `reference`) VALUES (NULL,?)";
$set = $db->prepare($SQL);
$result = $set->execute(array($refCode));
but i don't know what to do to get the sid of that inserted row
you could use this to get the last inserted ID
$id = mysqli_insert_id($db); or
$id = $db->mysqli_insert_id(); or
$id = $db->lastInsertId();
I have this php file to deal with my sql, I want to make many statement on one record in the database
for examole I have this query :
$query = mysql_query("SELECT bloodGroup,quantity,bank_id FROM medical_bank_notification WHERE seen=1");
I want to make all the records which were selected in the $query to have the field seen=0 after it has been selected, so I thought that I have to know all the IDs from the first query and then write another query:
$sql2 = "INSERT INTO medical_bank_notification (seen) VALUES (0) WHERE ID=_????_";
use mysqli_multi_query($con,$query)
$query = "INSERT INTO table1 (column1,column2,column3)
VALUES (value1,value2,value3);";
$query .= "INSERT INTO table2 (column1,column2,column3)
VALUES (value1,value2,value3);";
//excute query
if(mysqli_multi_query($con,$query))
{
while (mysqli_next_result($con) && mysqli_more_results($con))
{
if ($resSet = mysqli_store_result($con)) { mysqli_free_result($resSet); }
if (mysqli_more_results($con)){ }
}
echo 'success';
}
I want to get the result of an insert query like this :
$query = "INSERT INTO articles (Title,Description) VALUES ('{$title}','{$description}')";
$result = mysqli_query($connection, $query);
When I insert this article , I want his ID to use it in an other table ! the $result gives a true or false !
You can get the ID of the inserted row by calling the function mysqli_insert_id() directly after your mysqli_query().
$query = "INSERT INTO articles (Title,Description) VALUES ('{$title}','{$description}')";
$result = mysqli_query($connection, $query);
$id = mysqli_insert_id($connection);
use mysqli_insert_id ()
$insertId = mysqli_insert_id ( $connection );
Use this it return the value of your table fiele which is (peimary key) in this table
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
I am trying to get last autoincrement id from INSERT/UPDATE query, i am trying this way, but its not working, it just echo id=0 every time.
PHP
require_once('conn.php');
$temp = 'temp';
$query = "INSERT INTO temp (temp) VALUES('$temp')";
$result = mysqli_query($conn, $query) or trigger_error(mysqli_error($conn), E_USER_ERROR);
$id = mysqli_insert_id($conn);
echo 'id = '.$id;
Please see and suggest any possible way to do this.
Try changing the query to.
INSERT INTO `temp` (`temp`) VALUES('$temp')
I have this function in a Code Igniter model that creates a new "video."
// Creates a new video.
public function newVideo($title, $description) {
$this->db->query("INSERT INTO videos VALUES (NULL, '$title', '$description')");
return // id of this new row
}
How do I obtain the ID of this new row in my MySQL table? I could get the last row and add 1, but there could be concurrency bugs I believe.
Very simple answer doesn't really need more than 30 chars does it?
$this->db->insert_id();
maybe you need something like this
$this->db->insert_id();
Try something like that :
$lastID = -1; //That's where it'll be stored
$query = "SELECT LAST_INSERT_ID()";
$result = mysql_query($query);
if ($result)
{
$row = mysql_fetch_row($result);
$lastID = $row[0];
}
Code for an insert is:
$sql = "INSERT INTO videos (title) VALUES(NULL, '$title', '$description')";
$this->db->query($sql);
More information on queries available at: http://codeigniter.com/user_guide/database/queries.html
Another workaround is that you can have a look into videos table in your database, and make you video id as autonumber, this will automatically generate an id for your video and then you can try this:
//on your model
$sql = "INSERT INTO videos (title,description) VALUES('$title', '$description')";
$this->db->query($sql);
//retrieve new video id
$this->db->insert_id()