dekip Posted June 13, 2022 Share Posted June 13, 2022 I have to insert $z into DB. And $z is consisted from: $x = 5; $y = "123hg5"; With $z = $x.$y; I get only 5. And I need it to be 5123hg5. Quote Link to comment https://forums.phpfreaks.com/topic/314920-php-concatenate/ Share on other sites More sharing options...
Barand Posted June 13, 2022 Share Posted June 13, 2022 I get $z = "5123hg5" so there has to be something else going on. Show us your db update code your table structure definition Quote Link to comment https://forums.phpfreaks.com/topic/314920-php-concatenate/#findComment-1597229 Share on other sites More sharing options...
dekip Posted June 13, 2022 Author Share Posted June 13, 2022 Obviously not enough of data. My bad Idea is to have a device that will send some random string via GET. Say: 123gh5 On server I query DB, check last ID, increment to one more and insert that incremented ID into DB. But also put together this ID and this string and insert that, too. Let's call it newID. I hope it is not too confusing. This ID in DB is INT type, and newID is VARCHAR. But on server, it just doesn't pass $z = $x.$y;. It doesn't pass to inserting query. Say, this is GET request: somesite.com/index.php?upit=id&espID=12a456?id=12a456 And on server: if ($_SERVER["REQUEST_METHOD"] == "GET") { $action = $_GET["upit"]; if ($action == "id"){ $espID = $_GET["espID"]; $sql = mysqli_query($con,"SELECT * FROM autoSetup ORDER BY deviceID DESC LIMIT 1"); while($row = mysqli_fetch_array($sql,MYSQLI_ASSOC)){ $newID = $row["deviceID"] + 1; } echo $newID; $IDID = $newID.$espID; mysqli_query($con,"INSERT INTO autoSetup (deviceID, espID, UID, date) VALUES ('$newID', '$espID',$IDID, now())"); } } You can see here it is a part of a ESP MCU system I build. So deviceID is INT, espID and UID are VARCHAR. Quote Link to comment https://forums.phpfreaks.com/topic/314920-php-concatenate/#findComment-1597230 Share on other sites More sharing options...
mac_gyver Posted June 13, 2022 Share Posted June 13, 2022 1 hour ago, dekip said: check last ID, increment to one more and insert that incremented ID into DB don't do that. it is NOT concurrent safe. when more than one instance of your code is requested, each instance will get the same starting value, increment it, and produce duplicate values. you should instead use an auto-increment column. you should also NOT use a loop to fetch data from a query that will at most return one row. just directly fetch the data. 1 hour ago, dekip said: But also put together this ID and this string and insert that, too. don't do this either. this is storing derived/duplicate data. instead, concatenate this when the data is queried for. 1 hour ago, dekip said: UID are VARCHAR then why isn't the value inside of single-quotes in the query? you should use a post method form when performing an action on the server, so that a search engine indexing your site won't trigger the actions. this code is also open to sql injection. use a prepared query when supplying external, unknown, dynamic values to a query when it is executed. there's no validation logic, that would prevent empty values from being used, and no error handling for the database statements, that would handle duplicate user data in the insert query. Quote Link to comment https://forums.phpfreaks.com/topic/314920-php-concatenate/#findComment-1597231 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.