Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-07-2011, 11:56 AM   PM User | #1
Artyboy2011
New Coder

 
Join Date: Nov 2011
Location: London
Posts: 31
Thanks: 5
Thanked 1 Time in 1 Post
Artyboy2011 is an unknown quantity at this point
Insert to database when submit button is clicked

I need to insert user session to a database when the submit button is clicked.
So far i have the insert query set up, i just need to know how to run that when the button is clicked.

Thanks
Artyboy2011 is offline   Reply With Quote
Old 12-07-2011, 12:39 PM   PM User | #2
LSCare
Regular Coder

 
Join Date: Jun 2011
Location: Swindon, England
Posts: 124
Thanks: 3
Thanked 9 Times in 9 Posts
LSCare is an unknown quantity at this point
On the page that is the action in the form (<form action="thispage.php">...) place your code.

If it is in the page in which the form is in then use the isset() function e.g.

PHP Code:
if(isset($_POST['fieldname']){script
Otherwise it will only be triggered when the form calls the action so the need for that security is less.

If this doesn't make any sense... Please can you post your HTML and PHP script.
__________________
Xipux Design (Currently Unavailable)
Laurence S Care
LSCare is offline   Reply With Quote
Old 12-07-2011, 12:41 PM   PM User | #3
Artyboy2011
New Coder

 
Join Date: Nov 2011
Location: London
Posts: 31
Thanks: 5
Thanked 1 Time in 1 Post
Artyboy2011 is an unknown quantity at this point
ive got this so far. i just want an add button, not a whole form.

PHP Code:
<form name="freinds_request" method="post" action="friendrequest.php">
<input type="submit" name="Submit" id="'$username'" value="Login">
</form>

<?php
if(isset($friends_request)){
    
$sendfriend $mysql_fetch_assoc("INSERT INTO users(friend_request) WHERE name='$friends_name' VALUES('$username')");
    while(isset(
$friends_request)){
        echo 
'Friends request sent!';
        }
    }
?>
i guess i could have a button that acts like a like to another page (say friendsrequest.php) and in friends request.php it automatically runs the query. That idea is open to vulnerabilitys thoyugh because then anyone could type in friendsrequest.php?id=whatever and add them as a friend.

any better ways?

Last edited by Artyboy2011; 12-07-2011 at 12:44 PM.. Reason: had an idea for a solution
Artyboy2011 is offline   Reply With Quote
Old 12-07-2011, 05:05 PM   PM User | #4
BluePanther
Senior Coder

 
Join Date: Jul 2011
Posts: 1,226
Thanks: 3
Thanked 171 Times in 171 Posts
BluePanther is on a distinguished road
To check if a form is submitted, you can check for empty($_POST).

Form items need to be named, as the names become indexes in the sent POST array.

PHP Code:
<?php
if(empty($_POST)){
?>
    <form name="freinds_request" method="post" action="friendrequest.php">
        <input type="text" name="username" /> 
        <input type="submit" name="Submit" id="'$username'" value="Login"> 
    </form>
<?php
}
else{
    
$username $_POST['username'];
    
// IMPORTANT FUNCTION - secures user input in sql queries against SQL injection.
    
$username mysql_real_escape_string($username);
    
// mysql_fetch_* functions are not for executing queries - they're for fetching results from a query
    // You need to use this instead. Or die will kill the script on error, and output the value in mysql_error().
    
$result mysql_query("INSERT INTO `table` (`username`) VALUES ('$username')") or die(mysql_error());
    echo 
'Username inserted'

?>
BluePanther is offline   Reply With Quote
Old 12-08-2011, 11:55 AM   PM User | #5
Artyboy2011
New Coder

 
Join Date: Nov 2011
Location: London
Posts: 31
Thanks: 5
Thanked 1 Time in 1 Post
Artyboy2011 is an unknown quantity at this point
i understand that, but i need to have 2 variables ($userid-created as a session and $friendid-the page will be friends.php?id=$friendid) posted from 1 submit button.
is there anyway to do this?

Quote:
Originally Posted by BluePanther View Post
To check if a form is submitted, you can check for empty($_POST).

Form items need to be named, as the names become indexes in the sent POST array.

PHP Code:
<?php
if(empty($_POST)){
?>
    <form name="freinds_request" method="post" action="friendrequest.php">
        <input type="text" name="username" /> 
        <input type="submit" name="Submit" id="'$username'" value="Login"> 
    </form>
<?php
}
else{
    
$username $_POST['username'];
    
// IMPORTANT FUNCTION - secures user input in sql queries against SQL injection.
    
$username mysql_real_escape_string($username);
    
// mysql_fetch_* functions are not for executing queries - they're for fetching results from a query
    // You need to use this instead. Or die will kill the script on error, and output the value in mysql_error().
    
$result mysql_query("INSERT INTO `table` (`username`) VALUES ('$username')") or die(mysql_error());
    echo 
'Username inserted'

?>
Artyboy2011 is offline   Reply With Quote
Old 12-08-2011, 11:57 AM   PM User | #6
LSCare
Regular Coder

 
Join Date: Jun 2011
Location: Swindon, England
Posts: 124
Thanks: 3
Thanked 9 Times in 9 Posts
LSCare is an unknown quantity at this point
Yes.. Add another input to the form.

Code:
<form name="freinds_request" method="post" action="friendrequest.php"> 
  <input type="text" name="username" />
  <input type="..." name="..." />
  <input type="submit" name="Submit" id="'$username'" value="Login">  
</form>
Then both will be sent with the submit button (the names of each input must be different though).
__________________
Xipux Design (Currently Unavailable)
Laurence S Care
LSCare is offline   Reply With Quote
Old 12-08-2011, 06:33 PM   PM User | #7
Artyboy2011
New Coder

 
Join Date: Nov 2011
Location: London
Posts: 31
Thanks: 5
Thanked 1 Time in 1 Post
Artyboy2011 is an unknown quantity at this point
Quote:
Originally Posted by LSCare View Post
Yes.. Add another input to the form.
Thats exactly my problem, i need it to all be sent in 1 form. At the moment i have it set up like this.

site.com/addfriend.php?id=FRIENDSIDHERE
PHP Code:
<?php
session_start
();
$username $_SESSION['myusername'];
$friendid $_GET['friendid'];
   if(isset(
$_SESSION['$username']) && isset($_GET['friendid'])){
      
$sendfriend $mysql_query("INSERT INTO users(friend_request) WHERE id='$friendid' VALUES('$username')");
    while(
$row mysql_fetch_assoc($sendfriend)){
        echo 
"'$username' has requested to be friends with '$friendid'";
}
}
?>
This doesn't seem to be working though.

NOTE: I WILL BE MAKING THE SCRIPT MORE SECURE ONCE IT IS ALL WORKING.
Artyboy2011 is offline   Reply With Quote
Reply

Bookmarks

Tags
database, friends, insert, php, sql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:46 AM.


Advertisement
Log in to turn off these ads.