Alexhoward Posted March 11, 2008 Share Posted March 11, 2008 Hi, i'm so close but it doesn't work! what i would like to do is press the button, and it inserts the username from their cookie, and the link (full text) into a sql table... This code displays my links nicely on the page,in a grid, in their own seperate forms. see - http://www.everyonlinestore.co.uk/change_password.php (ignore the name) Code1 <?php $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 = $row['link']; if($c%5 == 0) echo "<tr height=\"100px\">"; // If the counter has ticked 6 times, start a new row. echo "<td><FORM action=addlinks.php method=post><p align=center>$user2</p><p align=center>rating script here</p><p align=center><input type=submit></form></p>"; 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 ?> end of code1 Then this bit of php should insert them into the table. however the variables are undefined...? probably becaused they're not named in code1, but i can't name them inside the " "'s I'm so stuck...! code2 <?php // 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()); // check if the link has already been added to username $mysite_username = $_COOKIE["mysite_username"]; $check = "SELECT link FROM memberlinks WHERE username = '$mysite_username'"; $qry = mysql_query($check) or die ("Could not match data because ".mysql_error()); $num_rows = mysql_num_rows($qry); if ($num_rows != 0) { echo "you have already added that link.<br>"; echo "<a href=Change_password.php>Back</a>"; exit; } // check username entered if (!$mysite_username) { echo "You did not enter a Username.<br>"; echo "<a href=change_password.php>Please Try again</a>"; exit; } // check link entered if (!$user2) { echo "You did not enter a link.<br>"; echo "<a href=change_password.php>Please Try again</a>"; exit; } else { // insert the data $insert = mysql_query("insert into memberlinks values ('".$_POST[$_COOKIE[mysite_username]]."', '".$_POST['$user2']."')") or die("Could not insert data because ".mysql_error()); // print a success message echo $_COOKIE["mysite_username"]; echo "Your link has been added!<br>"; echo "Now you can <a href=change_password.php>add another link</a>"; } ?> end of code2 Thanks in advance for all your help I'd love to get this sorted out, and no-one i know does PHP.... Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/ Share on other sites More sharing options...
MadTechie Posted March 11, 2008 Share Posted March 11, 2008 your form doesn't have any form elements except a button so $_POST['user2'] won't be passed (i assume you mean $_POST['user2'] and not $_POST['$user2']); also $_POST[$_COOKIE[mysite_username]] should be probably be $_COOKIE['mysite_username'] thats after a quick look.. Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489638 Share on other sites More sharing options...
Alexhoward Posted March 11, 2008 Author Share Posted March 11, 2008 hi, thats the problem. i can't name the feilds with this script (or don't know how to would be more accurate), like you would a signup script for example Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489733 Share on other sites More sharing options...
lemmin Posted March 11, 2008 Share Posted March 11, 2008 I'm not entirely sure what you are trying to do, but try adding this to your echo statement that writes the form: <input type=HIDDEN name='user2' value='$user2'> Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489736 Share on other sites More sharing options...
Alexhoward Posted March 11, 2008 Author Share Posted March 11, 2008 Thanks well...it's not hidden basically this is what i'm trying to do: When you click the button i want it to insert your username from the cookie, and the link into an sql table e.g. username link_id johnsmith link Thanks Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489766 Share on other sites More sharing options...
lemmin Posted March 11, 2008 Share Posted March 11, 2008 Did you try it? It doesn't need to hide anything you have, it just sends the $user2 variable through POST since you are trying to get it from there anyway. Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489770 Share on other sites More sharing options...
Alexhoward Posted March 11, 2008 Author Share Posted March 11, 2008 http://www.everyonlinestore.co.uk/change_password.php this is how it came out it's odd that after you state <..... value='$user2'> it's no longer enclosed in the < >. it stops and displays the '> have a look cheers Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489773 Share on other sites More sharing options...
lemmin Posted March 11, 2008 Share Posted March 11, 2008 Something is messed up in the quote scheme. Try chaning the apostrophes to escaped quotes \" and see if it fixes it. Otherwise, post the code for that line so I can see what might be wrong. Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489776 Share on other sites More sharing options...
Alexhoward Posted March 11, 2008 Author Share Posted March 11, 2008 Hey, thanks for helping me out. i really appreciate it! Here's the entire code. it's a bit scrappy and hard to look at. i've also put the username bit in with the same method <?php $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 = $row['link']; if($c%5 == 0) echo "<tr height=\"100px\">"; // If the counter has ticked 6 times, start a new row. echo "<td><FORM action='addlinks.php' method='post'><p><input type=HIDDEN name='user' value='mysite_username'></p><p><input type=HIDDEN name='user2' value='$user2'></p><p align=center>rating script here</p><p align=center><input type=submit></form></p>"; 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/95634-why-doesnt-my-code-work-please-help/#findComment-489781 Share on other sites More sharing options...
lemmin Posted March 11, 2008 Share Posted March 11, 2008 I don't see anything wrong in the code so the variable $user2 must hold a string with an apostrophe in it. Changing the apostrophes around $user2 into escaped quotes (\") should fix that. Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489791 Share on other sites More sharing options...
Alexhoward Posted March 11, 2008 Author Share Posted March 11, 2008 hmm... this is what the links look like in the table <a href="http://www.dpbolvw.net/click-2804964-5590692" target="_blank" onmouseover="window.status='http://www.argos.co.uk';return true;" onmouseout="window.status=' ';return true;"><img src="http://www.awltovhc.com/image-2804964-5590692" width="120" height="60" alt="Argos.co.uk 120x60" border="0"/></a> is it the </a> on the end...? are you saying like: \"$user2"\ Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489797 Share on other sites More sharing options...
Alexhoward Posted March 11, 2008 Author Share Posted March 11, 2008 ok took the <a> and </a> off the link and it works but now it's now NOT inserting the entire link into the sql table any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/95634-why-doesnt-my-code-work-please-help/#findComment-489813 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.