MadTechie Posted March 12, 2008 Share Posted March 12, 2008 refer back to this post you need to compare the same way you set Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-490441 Share on other sites More sharing options...
Alexhoward Posted March 12, 2008 Author Share Posted March 12, 2008 oh ok, i've changed it, but now it says "you've already added that link" everytime.... <?php //check to see if link has already been added to username $match = "SELECT link FROM memberlinks WHERE username = ('user') and link = ('user2')"; $qry = mysql_query($match) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows <= 0) { echo "You've already added that link.<br>"; echo "<a href=change_password.php>choose another</a>"; exit; } else {...... then it posts ?> Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-490445 Share on other sites More sharing options...
phpSensei Posted March 12, 2008 Share Posted March 12, 2008 try if ($num_rows > 0) { echo "You've already added that link.<br>"; echo "<a href=change_password.php>choose another</a>"; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-490476 Share on other sites More sharing options...
Alexhoward Posted March 12, 2008 Author Share Posted March 12, 2008 That will allow you to keep adding the same link Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-490489 Share on other sites More sharing options...
phpSensei Posted March 12, 2008 Share Posted March 12, 2008 That will allow you to keep adding the same link Sorry, need to keep up with what your trying to accomplish here. Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-490490 Share on other sites More sharing options...
Alexhoward Posted March 12, 2008 Author Share Posted March 12, 2008 This code words in my login script, where it checks the usename against the password... just not here...??? Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-490502 Share on other sites More sharing options...
MadTechie Posted March 12, 2008 Share Posted March 12, 2008 Okay this is wrong.. it should look something like this (sorry i am busy as hell today) oh ok, i've changed it, but now it says "you've already added that link" everytime.... <?php //check to see if link has already been added to username $user = mysql_real_escape_string($_POST['user']); $user2 = htmlspecialchars($_POST['user2'], ENT_QUOTES); //$match = "SELECT link FROM memberlinks WHERE username = ('user') and link = ('user2')"; $match = "SELECT link FROM memberlinks WHERE username = '$user' and link = '$user2';"; $qry = mysql_query($match) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows <= 0) { echo "You've already added that link.<br>"; echo "<a href=change_password.php>choose another</a>"; exit; } else {...... then it posts ?> Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-490557 Share on other sites More sharing options...
Alexhoward Posted March 12, 2008 Author Share Posted March 12, 2008 ah, thanks for getting back to me mate, but i've just worked it out! the script i took from my login page was checking if the two already exsisted, and giving an error if they didn't. but i wanted this to work the other way around. sorry it accured to me a bit late it needed to be: $num_rows != 0 was about to post to say i'd worked it out. I'm now off for the day, but will be back online at around 9am tomorrow with a table question. It relates to this, but different, so should we check this one as SOLVED! Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-490569 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 Hello, I'm back again ok two more questions before this script it perfect, well, 3, but i'll worry about the rating script later 1) this script makes the table depending on how many links there are. it puts each cell next to one another, then drops down to a new row when the set number has ben reached. However, i'd like to arrange them a bit better than this, say 25% each. and leave a gap between rows...? 2) When you press the "Add Link" submit button, i'd like it to change to "Link Added". But also i'd like them to check it the user has already added the link and if so display "Link Added" Does this make sense? Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491110 Share on other sites More sharing options...
MadTechie Posted March 13, 2008 Share Posted March 13, 2008 Okay 1. Erm.. 25% each ? of what the page width ? it sounds like a CSS/HTML issuse.. 2. if the field `Link` is Empty then change the value ie <?php if(empty($user3)) { $button = "<input type='submit' value='Add Link'>"; }else{ $button = "Link Added"; } echo "<td><FORM action='addlinks.php' method='post'> <p><input type=HIDDEN name='user' value='$mysite_username'></p> <p align=center><input type='HIDDEN' name='user2' value='$user2'></p> <p align=center>'$user3'</p> <p align=center>rating script here</p> <p align=center>$button</p></form>"; SideNote: maybe an idea to start a new thread for the Rating Script and in that thread include a link to this one, Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491135 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 Hello, you're well helpful! thanks for the script, however it doesn't check if the user has added the link. it's checking if the link exsists in the links table, which is does as that's where we pulling the links from...? i need it to check in the memberlinks table where the username with the link attached exsist. is that right...? cheers mate! Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491146 Share on other sites More sharing options...
MadTechie Posted March 13, 2008 Share Posted March 13, 2008 I need more info.. i don't know how your linking the tables (what fields) but if your using the links tables then all the entries are going to be linked try a Join ie SELECT *, Linkid as isLinked FROM `members` LEFT JOIN `Links` on members,linkid = Link.id if(empty($row['isLinked'])) Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491150 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 sorry, i'm not being very helpful am i basically all the links get pulled from the links table and displayed on the page. when the user selects a link to be added from their page it post onto the memberlnks table saving the user name from the cookie and the link (username, link) then on their page it brings back the link from memberlinks where the username = their username so i need to check if they have already added it, and if they have to display "Link Added" and if not display the submit button Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491166 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 I've tried doing this, but it's still not working? can you see where i'm going wrong...? <?php $sql2 = "SELECT link FROM memberlinks WHERE username = 'mysql_username' and link = 'user2'"; $result2 = mysql_query($sql2) or die(" Could not add style facts"); if(empty($result2)) { $button = "<input type='submit' value='Add Link'>"; }else{ $button = "Link Added"; } ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491174 Share on other sites More sharing options...
MadTechie Posted March 13, 2008 Share Posted March 13, 2008 Coding notes below <?php //i assume this just check if a link exists! (i think you mean $user2) $sql2 = "SELECT link FROM memberlinks WHERE username = 'mysql_username' and link = 'user2'"; $result2 = mysql_query($sql2) or die(" Could not add style facts"); if (mysql_num_rows($result2) == 0)//entry not in database so ask for one { $button = "<input type='submit' value='Add Link'>"; $link = ""; }else{ $button = "Link Added"; //the code below is kinda pointless, as we know that the link is user2 or $user2 $row = mysql_fetch_assoc($result2); //pull data (as we found something) $link = $row['link']; } //debug echo "$link $button"; ?> EDIT: please note the NEW comments Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491185 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 I've got it! <?php $sql2 = "SELECT link FROM memberlinks WHERE username = '$mysite_username' and link = '$user2'"; $result2 = mysql_query($sql2) or die(" Could not add style facts"); $num_rows = mysql_num_rows($result2); if ($num_rows != 0) { $button = "Link Added"; }else{ $button = "<input type='submit' value='Add Link'>"; } ?> but when you come back they haven't changed until you refresh the page Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491189 Share on other sites More sharing options...
MadTechie Posted March 13, 2008 Share Posted March 13, 2008 this code is proably being run after the pulling of data, move it up to above the get data, or post the page and i'll try to show you Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491192 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 Thanks alot, if i move it above the get data part, will it mean that the $user2 variable is no longer defined...? here's the entire script: <?php $mysite_username = $_COOKIE["mysite_username"]; $conn = mysql_connect($server, $db_user, $db_pass) or die ("could not connect to mysql"); #Connect to mysql $rs = mysql_select_db( $database, $conn ) or die ("Could not select database"); #select database $sql = "Select link FROM links"; //pull the users from the table $result= mysql_query($sql) or die(" Could not add style facts"); echo "<table align=center border=1>"; // display the users in table $c = 0; while($row = mysql_fetch_array($result)) { $user2 = htmlspecialchars($row['link'], ENT_QUOTES); $user3 = $row['link']; //$user2 = $row['link']; if($c%5 == 0) echo "<tr height=\"200px\">"; // If the counter has ticked 6 times, start a new row. $sql2 = "SELECT link FROM memberlinks WHERE username = '$mysite_username' and link = '$user2'"; $result2 = mysql_query($sql2) or die(" Could not add style facts"); //$qry2 = mysql_query($result2) //or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($result2); if ($num_rows != 0) { //if(empty($result2)) //{ $button = "Link Added"; }else{ $button = "<input type='submit' value='Add Link'>"; } //<input type=submit> echo "<td><FORM action='addlinks.php' method='post'><p><input type=HIDDEN name='user' value='$mysite_username'></p><p align=center><input type=HIDDEN name='user2' value='$user2'></p><p align=center>$user3</p><p align=center>rating script here</p><p align=center>$button</p></form>"; if($c%5 == 4) echo "</tr>"; // If we're drawing the 6th pic, end this row. $c++; } if($c%5 != 4) echo "</tr>"; // If there isn't a number of pics divisible by 6, end the row echo "</table>"; // end the table ?> Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491201 Share on other sites More sharing options...
MadTechie Posted March 13, 2008 Share Posted March 13, 2008 i'm kinda confused.. am i correct in saying then they add a link they then need to refresh the page for it to change from "add a link" to "link added", if so you need to have add link sql statement before the code below Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491211 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 Yea, they add a link. it goes to addlink.php and says "you link has been added" then there's a link to go back to the page of links and the buttons the same until you refresh the page what's this statment you mentioned...? Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491258 Share on other sites More sharing options...
MadTechie Posted March 13, 2008 Share Posted March 13, 2008 how do they go back ? back button ?, the addlink.php file create the entry in the database if thats not too long can you post it Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491269 Share on other sites More sharing options...
MadTechie Posted March 13, 2008 Share Posted March 13, 2008 Sorry work calls.. review this updated version <?php //only use when submitted if(isset($_POST['submit']) && $_POST['submit'] == 'Add Link') { //you could just add the code from the addlink.php here but i don't know that code include "addlinks.php"; } $mysite_username = $_COOKIE["mysite_username"]; $conn = mysql_connect($server, $db_user, $db_pass) or die ("could not connect to mysql"); #Connect to mysql $rs = mysql_select_db( $database, $conn ) or die ("Could not select database"); #select database $sql = "Select link FROM links"; //pull the users from the table $result= mysql_query($sql) or die(" Could not add style facts"); echo "<table align=center border=1>"; // display the users in table $c = 0; while($row = mysql_fetch_array($result)) { $user2 = htmlspecialchars($row['link'], ENT_QUOTES); $user3 = $row['link']; //$user2 = $row['link']; if($c%5 == 0) echo "<tr height=\"200px\">"; // If the counter has ticked 6 times, start a new row. $sql2 = "SELECT link FROM memberlinks WHERE username = '$mysite_username' and link = '$user2'"; $result2 = mysql_query($sql2) or die(" Could not add style facts"); //$qry2 = mysql_query($result2) //or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($result2); if ($num_rows != 0) { //if(empty($result2)) //{ $button = "Link Added"; }else{ $button = "<input type='submit' value='Add Link'>"; } //<input type=submit> //UPDATEED: removed the action (action='addlinks.php') as we call ourself echo "<td><FORM method='post'><p><input type=HIDDEN name='user' value='$mysite_username'></p><p align=center><input type=HIDDEN name='user2' value='$user2'></p><p align=center>$user3</p><p align=center>rating script here</p><p align=center>$button</p></form>"; if($c%5 == 4) echo "</tr>"; // If we're drawing the 6th pic, end this row. $c++; } if($c%5 != 4) echo "</tr>"; // If there isn't a number of pics divisible by 6, end the row echo "</table>"; // end the table ?> Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491275 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 Sure, no there's a link to the change_pasword.php page <?php //check to see if link has already been added to username $user = mysql_real_escape_string($_POST['user']); $user2 = htmlspecialchars($_POST['user2'], ENT_QUOTES); $match = "SELECT link FROM memberlinks WHERE username = '$user' and link = '$user2'"; $qry = mysql_query($match) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "You've already added that link.<br>"; echo "<a href=change_password.php>choose another</a>"; exit; } else { // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); // insert the data $user = mysql_real_escape_string($_POST['user']); $user2 = htmlspecialchars($_POST['user2'], ENT_QUOTES); $insert = mysql_query("insert into memberlinks values ('$user', '$user2')") or die("Could not insert data because ".mysql_error()); // print a success message echo "Your link has been added!<br>"; echo "Now you can <a href=change_password.php>add another link</a>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491277 Share on other sites More sharing options...
MadTechie Posted March 13, 2008 Share Posted March 13, 2008 i may of made a mistake this was done very quickly <?php if(isset($_POST['submit']) && isset($_POST['user']) && isset($_POST['user2']))//updated { //check to see if link has already been added to username $user = mysql_real_escape_string($_POST['user']); $user2 = htmlspecialchars($_POST['user2'], ENT_QUOTES); $match = "SELECT link FROM memberlinks WHERE username = '$user' and link = '$user2'"; $qry = mysql_query($match) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "You've already added that link.<br>"; echo "<a href=change_password.php>choose another</a>"; //exit; //removed }else { // connect to the mysql server $link = mysql_connect($server, $db_user, $db_pass) or die ("Could not connect to mysql because ".mysql_error()); // select the database mysql_select_db($database) or die ("Could not select database because ".mysql_error()); // insert the data $user = mysql_real_escape_string($_POST['user']); $user2 = htmlspecialchars($_POST['user2'], ENT_QUOTES); $insert = mysql_query("insert into memberlinks values ('$user', '$user2')") or die("Could not insert data because ".mysql_error()); // print a success message echo "Your link has been added!<br>"; //unneeded below //echo "Now you can <a href=change_password.php>add another link</a>"; } } $mysite_username = $_COOKIE["mysite_username"]; $conn = mysql_connect($server, $db_user, $db_pass) or die ("could not connect to mysql"); #Connect to mysql $rs = mysql_select_db( $database, $conn ) or die ("Could not select database"); #select database $sql = "Select link FROM links"; //pull the users from the table $result= mysql_query($sql) or die(" Could not add style facts"); echo "<table align=center border=1>"; // display the users in table $c = 0; while($row = mysql_fetch_array($result)) { $user2 = htmlspecialchars($row['link'], ENT_QUOTES); $user3 = $row['link']; //$user2 = $row['link']; if($c%5 == 0) echo "<tr height=\"200px\">"; // If the counter has ticked 6 times, start a new row. $sql2 = "SELECT link FROM memberlinks WHERE username = '$mysite_username' and link = '$user2'"; $result2 = mysql_query($sql2) or die(" Could not add style facts"); //$qry2 = mysql_query($result2) //or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($result2); if ($num_rows != 0) { //if(empty($result2)) //{ $button = "Link Added"; }else{ $button = "<input type='submit' value='Add Link'>"; } //<input type=submit> //UPDATEED: removed the action (action='addlinks.php') as we call ourself echo "<td><FORM method='post'><p><input type=HIDDEN name='user' value='$mysite_username'></p><p align=center><input type=HIDDEN name='user2' value='$user2'></p><p align=center>$user3</p><p align=center>rating script here</p><p align=center>$button</p></form>"; if($c%5 == 4) echo "</tr>"; // If we're drawing the 6th pic, end this row. $c++; } if($c%5 != 4) echo "</tr>"; // If there isn't a number of pics divisible by 6, end the row echo "</table>"; // end the table ?> Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491284 Share on other sites More sharing options...
Alexhoward Posted March 13, 2008 Author Share Posted March 13, 2008 Alright cool, so this will do it without leaving the page Sweet!! unfortunatly... it doesn't do anything...? the page refreshes, but it doesn't add anything into the table i have just lifted the entire code...is that wrong? Quote Link to comment https://forums.phpfreaks.com/topic/95713-insert-html-code-into-sql-table-via-php-form/page/2/#findComment-491294 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.