Jump to content

insert HTML code into sql table via php form


Alexhoward

Recommended Posts

  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

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
?>

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

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!

 

;D

Link to comment
Share on other sites

Hello,

 

I'm back again :D

 

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?

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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']))

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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


?>

Link to comment
Share on other sites

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


?>

Link to comment
Share on other sites

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>"; 

}
?>

Link to comment
Share on other sites

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


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.