0

Hello. I am kind of new to PHP and mysql, and I am wondering on how I would go about coding up a system that would allow me to save multiple values to one row in a mysql database, and later read each separately. I am coding up a mac address logging program that works with a ubuntu gateway system with php, and am wondering on how to go about this. So in example if the value in the mysql row was

00-00-00-00-00-00

and the script reads a mac address of

11-11-11-11-11-11

it would change the mysql row to like

00-00-00-00-00-00;11-11-11-11-11-11

and later in php, I would like to be able to not only call up and see how many mac addresses are logged and what they are, but also make it so it does not log the same mac address twice. Thanks if you can help, and sorry if I did a bad job at explaining this, im very new with mysql. Thanks

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by ddggttff3
0

What is your reason for saving everything to one row? It would be simpler to save each one as a separate record. Then, the counting and checking for duplicates is quite straightforward. If you append everything to one record, then you have to read that record every time, break up the content and check against each one in the program. You will be using the database like a flat file and losing out on its capabilities.

0

the reason is this also goes with a account system, and there are different account types. Each account type can hold a different amount of MAC addresses, and this would make it easier to keep all accounts in one table.

0

OK, well I got somthing started, but it doesnt seem to work just right... it logs the mac address, and posts it, but it wont add new MAC addresses, it will just over right. Heres the snippet.

$read just logs how many mac addresses are saved.

if (isset($_COOKIE['usermac']))
        {
            $cmda = "select * from users where username = '$_SESSION[username]'";
            $cmmda = mysql_query($cmda);
            $reada = mysql_fetch_array($cmmda);
            $readmacs = $read['mac_addresses'];
            $readmacnum = $read['mac_num'];
            if (strpos($readmacs, $_COOKIE['usermac']) === false)
            {
                $savemac = $_COOKIE['usermac'];
                $newmacs = $readmacs . ";" . $savemac;
                $newmacnum = $readmacnum +1;
                $cmdb = "UPDATE users SET mac_addresses = '$newmacs', mac_num = '$newmacnum' where username = '$_SESSION[username]'";
                $cmmdb = mysql_query($cmdb);
            }
        }

Thanks

0

well got it fixed myself. The code above has some typos causing it not to work right. Heres what works for me:

if (isset($_COOKIE['usermac']))
        {
            $cmda = "select * from users where username = '$_SESSION[username]'";
            $cmmda = mysql_query($cmda);
            $reada = mysql_fetch_array($cmmda);
            $readmacs = $reada['mac_addresses'];
            $readmacnum = $reada['mac_num'];
            if (strpos($readmacs, $_COOKIE['usermac']) === false)
            {
                $savemac = $_COOKIE['usermac'];
                $newmacs = $readmacs . ";" . $savemac;
                $newmacnum = $readmacnum +1;
                $cmdb = "UPDATE users SET mac_addresses = '$newmacs', mac_num = '$newmacnum' where username = '$_SESSION[username]'";
                $cmmdb = mysql_query($cmdb);
            }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.