Reply
PHP inserting vales into MYSQL
Old 01-20-2009, 10:52 AM PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
Hi all, I need some help I have a mysql tables and want to take some values from a form and place them into the database. This is the code I have so far I am pretty new to php so sorry for any school boi errors. :P

Code:
<?php
include("application_top.php");
//validate the form
if (!empty($_REQUEST['Email'])){
$Email = $_REQUEST['Email'];	
} else {
$Email = NULL;
echo '<p class="error"> You forgot to enter a Email!</p>';
}
$Movies = $_REQUEST['Movies'];
$Score = $_REQUEST['Score'];
$Score1 = $_REQUEST['Score1'];
$Score2 = $_REQUEST['Score2'];
$Score3 = $_REQUEST['Score3'];

INSERT INTO Survey VALUES ('$Email','$Movies','$Score','$Score1','$Score2','$Score');

?>

any help would be amazing thanks all
__________________
Www.ebookcabi.net

Last edited by millwalll; 01-20-2009 at 10:53 AM..
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
 
When You Register, These Ads Go Away!
Old 01-20-2009, 11:06 AM Re: PHP inserting vales into MYSQL
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
Instead of that last line of code, you need something like this.

PHP Code:
$connection mysql_connect('localhost','username','password');
if(
$connection){
if(
mysql_select_db('database_name'$connection)){
//keep people from doing SQL injection attacks.
$Email addslashes($Email);
$Movies addslashes($Movies);
$Score addslashes($Score);
$Score1 addslashes($Score1);
$Score2 addslashes($Score2);
$Score3 addslashes($Score3);

$query "INSERT INTO Survey VALUES ('$Email','$Movies','$Score','$Score1','$Score2','$Score');";
if(
mysql_query($query$connection)){
echo 
'success';
}else{
echo 
'failure';
}
}else{
echo 
'failure';
}else{
echo 
'failure';
}
}else{
echo 
'failure';

you can avoid all the failure issues if you put that in a function and return on success, but meh.

EDIT: oh, and be sure to replace 'username', 'password', 'database_name', and possibly 'localhost' with correct values (ask your webhost if you're not sure).
__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions

Last edited by anderswc; 01-20-2009 at 12:25 PM..
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 11:21 AM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
Hi done that but when i go to sql and say select * from Survey it does not show any results

The thing is I am conecting to a server thought putty as localhost dont know if that has anythink to do with it
__________________
Www.ebookcabi.net

Last edited by millwalll; 01-20-2009 at 11:22 AM..
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-20-2009, 11:37 AM Re: PHP inserting vales into MYSQL
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
if you're connection with putty as localhost, but your server is not on your own computer, then of course you won't get any results.

You should use phpmyadmin to view the results online.

Try running the code I gave you (inside your own script) and see what it prints out. if it prints success, there should be values. if it prints failure, there is a problem.
__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 11:46 AM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
trying your code I get this error

Parse error: parse error in /users/start07/jpr23/public_html/Handelform.php on line 31
__________________
Www.ebookcabi.net
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-20-2009, 11:49 AM Re: PHP inserting vales into MYSQL
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
and line 31 is?
__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 11:53 AM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
30echo 'failure';
31}else{
32echo 'failure';
33}

I thougth there was a brace missing when i added one it moved the error to 35
__________________
Www.ebookcabi.net
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-20-2009, 12:05 PM Re: PHP inserting vales into MYSQL
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
sorry, I think I had one too many elses in there. try this.

PHP Code:
$connection mysql_connect('localhost','username','password');
if(
$connection){
    if(
mysql_select_db('database_name'$connection)){
        
//keep people from doing SQL injection attacks.
        
$Email addslashes($Email);
        
$Movies addslashes($Movies);
        
$Score addslashes($Score);
        
$Score1 addslashes($Score1);
        
$Score2 addslashes($Score2);
        
$Score3 addslashes($Score3);
        
        
$query "INSERT INTO Survey VALUES ('$Email','$Movies','$Score','$Score1','$Score2','$Score');";
        if(
mysql_query($query$connection)){
            echo 
'success';
        } else{
            echo 
'failure';
        }
    } else{
        echo 
'failure';
    }
    
mysql_close($connection);
} else{
    echo 
'failure';

__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions

Last edited by anderswc; 01-20-2009 at 12:26 PM..
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 12:10 PM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in /users/start07/jpr23/public_html/Handelform.php on line 14
failure

14if(mysql_select_db('m08jpr23', $connetion)){
__________________
Www.ebookcabi.net
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-20-2009, 12:25 PM Re: PHP inserting vales into MYSQL
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
dang it, typo. should be $connection, not $connetion
__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 12:27 PM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
it comes up with failure does that mean problem with my connection ??
__________________
Www.ebookcabi.net
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-20-2009, 12:27 PM Re: PHP inserting vales into MYSQL
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
Oh! also, I forgot to close the connection. It will be closed by default at the end of the script, but it's usually a good idea to close it when you're done with it.

You should now see the function call toward the end of the second piece of code I posted. (4th to last line)
__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 12:36 PM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
how do I close the Database is it mysql_close($connection); and all i get now is failure
__________________
Www.ebookcabi.net
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-20-2009, 12:54 PM Re: PHP inserting vales into MYSQL
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
yes, it's the mysql_close call.

try adding this to see what went wrong.

PHP Code:
echo mysql_error(); 
__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 01:20 PM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
adding it to where !
__________________
Www.ebookcabi.net
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-20-2009, 01:27 PM Re: PHP inserting vales into MYSQL
anderswc's Avatar
Super Talker

Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
Trades: 0
the end of the code
__________________
Will Anderson
It's An Anderson | Twitter | Anderson Web Solutions
anderswc is offline
Reply With Quote
View Public Profile Visit anderswc's homepage!
 
Old 01-20-2009, 01:40 PM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
this is what I have so far

Code:
<?php
$connection = mysql_connect('host','username','password')or die(mysql_error());
if($connection){
if(mysql_select_db('m08jpr23', $connection)){

$Email = addslashes($Email);
$Movies = addslashes($Movies);
$Score = addslashes($Score);
$Score1 = addslashes($Score1);
$Score2 = addslashes($Score2);
$Score3 = addslashes($Score3);

$query = "INSERT INTO Survey VALUES ('$Email','$Movies','$Score','$Score1','$Score2','$Score')";
if(mysql_query($query, $connection)){
 echo 'success';
        } else{
            echo 'failure';
        }
    } else{
        echo 'failure';
    }
    mysql_close($connection);
} else{
    echo 'failure';
}  
echo mysql_error();  
?>
and I just get failure
__________________
Www.ebookcabi.net
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-20-2009, 03:01 PM Re: PHP inserting vales into MYSQL
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Personally, I'd write it that way:
PHP Code:
<?php
$connection 
mysql_connect('host','username','password')or die(mysql_error());
if(
$connection===FALSE){
    echo 
mysql_error();
    die();
}
if(
mysql_select_db('m08jpr23'$connection)===FALSE){
    echo 
mysql_error();
    die();
}

$Email addslashes($Email);
$Movies addslashes($Movies);
$Score addslashes($Score);
$Score1 addslashes($Score1);
$Score2 addslashes($Score2);
$Score3 addslashes($Score3);

$query "INSERT INTO Survey VALUES ('$Email','$Movies','$Score','$Score1','$Score2','$Score')";
$ret=mysql_query($query$connection);

if(
$ret===FALSE){
    echo 
'failure\n<br/>';
    echo 
mysql_error();
}
else{
    echo 
'success';
}
mysql_close($connection);
?>
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 01-20-2009 at 03:02 PM..
tripy is online now
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 01-21-2009, 05:13 AM Re: PHP inserting vales into MYSQL
millwalll's Avatar
Webmaster Talker

Posts: 630
Name: James
Location: KENT
Trades: 2
Hi all, tried all that still it wont load the information into the database could it be because of the contents I am trying to load into the database?

here is all my code
form code
Code:
<form name="Register" method="post" action="Handelform.php">
              <label>
              <fieldset>
              <legend> Please Enter your information all fields are required:</legend>
              
              <div align="left">
                <p>Email
                  <input type="text" name="Email" id="Email" size="25" maxlength="60">                
                  </p>
                <p>
                  <label>
                  <select name="Movies" size="1" id="Movies">
                    <option>Indian Jones TOD</option>
                    <option>Bad Boys 2</option>
                    <option>Pulp Fiction</option>
                  </select>
                  </label>
                  </p>
                <p>Action:
                  <label>
                  <select name="Score" size="1" id="Score">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                  </select>
                  </label>
                  </p>
                <p>Visual Effects:
                  <select name="Score1" size="1" id="Score1">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                  </select>
                </p>
                <p>Story Line:
                  <select name="Score2" size="1" id="Score2">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                  </select>
                </p>
                <p>Overall Score:
                  <select name="Score3" size="1" id="Score3">
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                    <option>10</option>
                  </select>
                  <br>
                  </p>
              </div>
              <div align="left"></div>
<div align="left"><label><br>
                            <br>
                            <input type="submit" name="Submit" id="Submit" value="Submit">
                            </label>
                            <br>
                            </div>
                          </label>
                          </fieldset>
            </form>            </td>
and my php code that handel this
Code:
<?php
$db = mysql_connect("host","username","password");
$result = mysql_select_db("m08jpr23", $db);

$Movies = $_REQUEST['Movies'];
$Score = $_REQUEST['Score'];
$Score1 = $_REQUEST['Score1'];
$Score2 = $_REQUEST['Score2'];
$Score3 = $_REQUEST['Score3'];

$query = "INSERT INTO Survey VALUES ('$Email','$Movies','$Score','$Score1','$Score2','$Score')";
?>
and finally my code for my SQL

Code:
create table Survey( 
SurveyId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Email VARCHAR(30),
Movies VARCHAR(25),
Score INT (5),
Score1 INT (5),
Score2 INT (5),
Score3 INT (5)
);
any help would be amazing thanks in advance
__________________
Www.ebookcabi.net
millwalll is offline
Reply With Quote
View Public Profile Visit millwalll's homepage!
 
Old 01-21-2009, 06:47 AM Re: PHP inserting vales into MYSQL
tripy's Avatar
Do not try this at home!

Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
still it wont load the information into the database
Could you be more descriptive ?
Have you got an error?
Does it gives you "success" and you don't see nothing in the db ?
You don't have errors, but the datas are not in the db ?
If you try to add an
PHP Code:
echo $query
just after the mysql_query(), what is it's value ?
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is online now
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to PHP inserting vales into MYSQL

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.19773 seconds with 13 queries