-2

i have an customers i want to add IP address to these customers i used loop $i=254 to add ip address automatically but it's give 1.1.2.254 to all the customers how can i fixing it

i need to make loop if the 1.1.2.254 is existing for customer 1 !! use the next 1.1.2.253 for customer 2

$i=254
        $res = MySQL_query("INSERT INTO `rm_users`(`username`, `staticipcpe`) VALUES ('$username', '1.1.2.$i');

$i--
Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Please add more details to your question by editing. There is no loop in the given code. Also, that query looks widely open for SQL injection – Nico Haase Jan 16 '21 at 20:48
  • _Please_ tell me you're not actually using `mysql_query` in 2021? For the love of all you hold dear, don't use the `mysql_*` functions!** They are old and broken, were deprecated in PHP 5.5, and completely removed in PHP 7.0 (which is so old it [no longer even receives active support](http://php.net/supported-versions.php)). Use [`PDO`](https://secure.php.net/manual/en/book.pdo.php) or [`mysqli_*`](https://secure.php.net/manual/en/book.mysqli.php) with _prepared statements_ and _parameter binding_ instead. See http://stackoverflow.com/q/12859942/354577. – ChrisGPT was on strike Jan 17 '21 at 00:43
  • No, every period I update the site’s data, and because the data used on the site are many, I cannot modify them all at once ... The project that they currently use is the 2013 project, and I developed it for that @Chris – Alexander Muhannad Jan 17 '21 at 02:28

3 Answers3

0

use ip unique in db to insert and failure if ip is exiting

now using for($i=254;$i > 0;$i--){ $res = mysql_query("INSERT INTO `rm_users`(`username`, `staticipcpe`) VALUES ('$username', '1.1.2.$i')"); }

and it's working good

-1

The question is a little incomplete and incorrect, but I think you have an IP address from the input or elsewhere. You can search this address with a query.

SELECT EXISTS(SELECT * FROM table1 WHERE ...)

this will return you a true or false value. You can browse the detailed document and similar question

  1. Best way to test if a row exists in a MySQL table
  2. https://dev.mysql.com/doc/refman/8.0/en/exists-and-not-exists-subqueries.html
  • I have an customer i want to add an ip address to these customer but some customer have an ip i use $i=254 to add an ip automatically but it's always add same ip to all customer how can i fixing – Alexander Muhannad Jan 15 '21 at 23:04
  • is this a php code and form implementation correct – Oğuzhan Filiz Jan 15 '21 at 23:14
  • The code is too long and I can't add it all, so I just shortened the point I needed , the code is working good the proplem is the loop give the same IP to All clients – Alexander Muhannad Jan 15 '21 at 23:20
  • oh my god i got it now I am so sorry. mysql_num_rows ($ res); With the number, we can reduce the number until it reaches 0, but this will tire you a lot. I'm searching for a better solution – Oğuzhan Filiz Jan 15 '21 at 23:44
  • To shorten the query time, you can take the last staticipcpe record in the database, shred the content and get the number after the last point, and reduce it one and make your new i value, but that's still not the best – Oğuzhan Filiz Jan 15 '21 at 23:54
  • Can you give me a simple form and thanx for help me – Alexander Muhannad Jan 16 '21 at 00:00
  • i take the last staticipcpe recored it's 1.1.2.254 if i use 1.1.2.254 -1 it's give me 0.1 – Alexander Muhannad Jan 16 '21 at 00:01
  • You need to shred ip when you get it, for example 1.1.1.255 when it comes, break it up with the explode function and get 255, then assign 255 to a variable and reduce it then use this value when saving to the database – Oğuzhan Filiz Jan 16 '21 at 00:13
-2
for($i=254;$i > 0;$i--){
   $res = mysql_query("INSERT INTO `rm_users`(`username`, `staticipcpe`) VALUES ('$username', '1.1.2.$i')");
}

this will do the trick!!!. This loop will insert upto 1.1.2.1

Karthick
  • 281
  • 2
  • 7
  • thanx , but if an existing ip it's add same , i don't need to add same ip i need to skip the ip if it's exising in the database – Alexander Muhannad Jan 16 '21 at 07:20
  • 1
    This will not do any trick - please do never recommend insecure code – Nico Haase Jan 16 '21 at 20:48
  • @NicoHaase how can i make trick if the ip is existing use the next please – Alexander Muhannad Jan 16 '21 at 22:28
  • there are two ways to achieve the result that expected by OP @NicoHaase, @Alexander Muhannad you can create an Unique constraint using those two columns `username`, `staticipcpe` and can use insert on duplicate key update, or you can write and select query to count the records for this username and ip before the insert – Karthick Jan 17 '21 at 03:37