Mizar Posted April 2, 2020 Share Posted April 2, 2020 Hi Guys, actually I'm doing hard with this code. the fact is that Im inserting data (name, phone number, nss) and im doing a select to get the id number where nss is like $nss (. I don`t know why I can`t receive data, i think the fact is that all the code is inside an if statement) But that is why im here guys. In adittion I tryed to get the max id in order to print into mail function. But I couldn't . This is my code... ************************************************************************************************************ //Database connection done perfectly $mysqli = mysqli_connect( "****", "***", "***", "***"); ************************************************************************************************************ // Im working with bots and the intent received is this, in addition i get variables correctly. if (intent_recibido('registro')){ $name = obtener_variables()['name']; $lastname = obtener_variables()['lastname']; $gender = obtener_variables()['gender']; $day_of_birth = obtener_variables()['day_of_birth']; $email = obtener_variables()['email']; $address = obtener_variables()['address']; $phone = obtener_variables()['phone']; $nss = obtener_variables()['nss']; ************************************************************************************************************ //here im inserting the variables in the correct order. The database is getting the values correctly too. $sql = ("INSERT INTO pacient (name,lastname,gender,day_of_birth,email,address,phone,nss) VALUES ('$name','$lastname','$gender','$day_of_birth','$email','$address','$phone','$nss') "); ************************************************************************************************************ // this is the answer of the bot, which tell us that it has sent an email to confirm account. enviar_texto("$name $lastname, le hemos enviado por correo un mail para confirmar su registro, el valor de nss es $nss y su id es $id"); ************************************************************************************************************ //here is the section when im going to select the id from the table, where the nss number is equal to the nss variable number that i've putted (but idk if the insert is done at this point.... $result = $mysqli->query("SELECT `id` FROM `pacient` WHERE `nss` = $nss "); $finalId = mysqli_fetch_assoc($result); $id = $finalId['id']; ************************************************************************************************************//in this section we are sending a mail with the information that i've putted. For example i'm getting the mail, the name and lastname correctly from my function (which gets the parameters and turn the information in readable characters to php) $to = $email; $cc = "email@email.com"; $subject = "Registro AlergoPED"; $message = "Estimado $name $lastname, gracias por registrarte en AlergoPed, su numero de identificacion es $id "; $headers = 'From: webmaster@darystortas.com' . "\r\n" . 'Reply-To: webmaster@darystortas.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail($to, $subject, $message, $headers); } ************************************CODE DONE *********************************************************** So guys, Idk what is happening here, I tryed lot of things but when im get the email I dont get the id value..... I think is because the insert hasn't been completed while the if is open..... In adition I wanto to ask you guys if somebody knows how to get correctly the data from this code... Firstly I was trying to get the max id from my table and then plus 1 to increase the number. and in that order put the id by myself or atleast get the value in that way. I tryed closely, but i finish getting the array value.. I going to show my code... function obtener_Id(){ global $mysqli; $resultado_oId = $mysqli ->query("SELECT TOP 1 'id' FROM 'pacient' ORDER BY RowID DESC ") $id_sacada = mysqli_fetch_assoc($resultado_oId); $id_enLaMano = $id_sacada['id']; return $id_enLaMano; //$id_dinamica = $id_enLaMano + 1; //return $id_dinamica; } Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 2, 2020 Share Posted April 2, 2020 First please use the code icon (<>) and select PHP so it formats your code to make is easier to read. Second you probably should be using PHPMailer for your email. It is much easier to use and gives you better control. Finally to debug this you need to make sure you have errors enabled but your insert does not contain the field 'id'. Where do you add that to the record? Quote Link to comment Share on other sites More sharing options...
Barand Posted April 2, 2020 Share Posted April 2, 2020 After inserting a record, call last_insert_id() method to get its id. Faster than querying. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.