_DarkLink_ Posted August 8, 2009 Share Posted August 8, 2009 Hello, I would really appreciate if you guys would have a look at this piece of code. <?php if(isset($_POST['add'])) { $self = $_SERVER['PHP_SELF']; //the $self variable equals this file $ipaddress = ("$_SERVER[REMOTE_ADDR]"); //the $ipaddress var equals users IP //connect $connect = mysql_connect($host,$username,$password) or die('<p class="error">Unable to connect to the database server at this time.</p>'); mysql_select_db($database,$connect) or die('<p class="error">Unable to connect to the database at this time.</p>'); //fetch data $data = htmlspecialchars($_POST['list']); $comment = mysql_real_escape_string($_POST['comment']); $data_lines = explode( "\r\n", $data ); $comment_lines = explode("\r\n", $comment); for($i=0;$i<count($data_lines);$i++) { $data_fields = explode( ",", $data_lines[$i]); $time = time(); $queryb = "INSERT INTO coords SET tag='{$data_fields[0]}', guild='{$data_fields[1]}', name='{$data_fields[2]}', base='{$data_fields[3]}', econ='{$data_fields[5]}', maxecon='{$data_fields[6]}', location='{$data_fields[4]}', comment='{$comment_lines[$i]}', ipaddress='$ipaddress' , date='$time';"; // if it succeeds, display message if (mysql_query($queryb)) { echo('<p class="success">Successful posting of ['.$data_fields[3].']!</p>'); } else { echo('<p class="error">Error could not post ['.$data_fields[3].'] to database!</p>'); } }//end for loop }//end if $_POST['add'] statement ?> As you can see, it gets data received from a form's submission and explodes them into lines. For $data, it explodes it again for each comma. It then inserts everything for each comma, in a new column and for each line, on a new row. Now for $comment, it should be inserting it on a new row for each line, which isn't doing it, it just inserts the whole comment into one single row. Been looking and testing at it for a few days now. The comment column is a varchar of 100 of length. So basically, it does everything i need it to do except inserting $comment on a new row for every line. Any help is appreciated. Thank you in advance. Link to comment https://forums.phpfreaks.com/topic/169409-solved-inserting-an-array-into-an-sql-table/ Share on other sites More sharing options...
Bjom Posted August 8, 2009 Share Posted August 8, 2009 $comment_lines = explode("\r\n", $comment); insert a var_dump($comment_lines); after the line above. What do you see? Does the explode work alright? It could be that the linebreak is different in the $comment... only \r only \n - quite a few things possible Link to comment https://forums.phpfreaks.com/topic/169409-solved-inserting-an-array-into-an-sql-table/#findComment-893825 Share on other sites More sharing options...
_DarkLink_ Posted August 9, 2009 Author Share Posted August 9, 2009 Hey is what the array contains: array(1) { [0]=> string(16) "QWERTY\r\nAZERTY" } It does not seem to be separated into two arrays. Thank you, more help is appreciated. Link to comment https://forums.phpfreaks.com/topic/169409-solved-inserting-an-array-into-an-sql-table/#findComment-893841 Share on other sites More sharing options...
Bjom Posted August 9, 2009 Share Posted August 9, 2009 the mysql_real_escape_string breaks it, since it adds backslashes. Try this: $comment_lines = explode("\\r\\n", $comment); if this won't work come back and I'll have a different solution too, but this one is easiest and most efficient. Link to comment https://forums.phpfreaks.com/topic/169409-solved-inserting-an-array-into-an-sql-table/#findComment-894047 Share on other sites More sharing options...
_DarkLink_ Posted August 9, 2009 Author Share Posted August 9, 2009 Oh my, it worked! I can't believe it, I have been wasting too much time on this little part of my project. I don't know how I may thank you enough for this. [EDIT]How do I thank someone?[EDIT] Thank you for putting your time in this and helping me. Link to comment https://forums.phpfreaks.com/topic/169409-solved-inserting-an-array-into-an-sql-table/#findComment-894072 Share on other sites More sharing options...
Bjom Posted August 9, 2009 Share Posted August 9, 2009 How do I thank someone? By helping out others on this forum. (And in my case by having a look at my thread in "beta-testing", if you like. Any feedback welcome, no matter if on code or on documentation.) Link to comment https://forums.phpfreaks.com/topic/169409-solved-inserting-an-array-into-an-sql-table/#findComment-894087 Share on other sites More sharing options...
_DarkLink_ Posted August 9, 2009 Author Share Posted August 9, 2009 Meh, I was trying to find that "Thank You" button somewhere. But anyway, will do what you said. Link to comment https://forums.phpfreaks.com/topic/169409-solved-inserting-an-array-into-an-sql-table/#findComment-894089 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.