PHP inserting vales into MYSQL
01-20-2009, 10:52 AM
|
PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
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
Last edited by millwalll; 01-20-2009 at 10:53 AM..
|
|
|
|
01-20-2009, 11:06 AM
|
Re: PHP inserting vales into MYSQL
|
Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
|
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).
Last edited by anderswc; 01-20-2009 at 12:25 PM..
|
|
|
|
01-20-2009, 11:21 AM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
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
Last edited by millwalll; 01-20-2009 at 11:22 AM..
|
|
|
|
01-20-2009, 11:37 AM
|
Re: PHP inserting vales into MYSQL
|
Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
|
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.
|
|
|
|
01-20-2009, 11:46 AM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
trying your code I get this error
Parse error: parse error in /users/start07/jpr23/public_html/Handelform.php on line 31
|
|
|
|
01-20-2009, 11:49 AM
|
Re: PHP inserting vales into MYSQL
|
Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
|
and line 31 is?
|
|
|
|
01-20-2009, 11:53 AM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
30echo 'failure';
31}else{
32echo 'failure';
33}
I thougth there was a brace missing when i added one it moved the error to 35
|
|
|
|
01-20-2009, 12:05 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
|
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'; }
Last edited by anderswc; 01-20-2009 at 12:26 PM..
|
|
|
|
01-20-2009, 12:10 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
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)){
|
|
|
|
01-20-2009, 12:25 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
|
dang it, typo. should be $connection, not $connetion 
|
|
|
|
01-20-2009, 12:27 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
it comes up with failure does that mean problem with my connection ??
|
|
|
|
01-20-2009, 12:27 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
|
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)
|
|
|
|
01-20-2009, 12:36 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
how do I close the Database is it mysql_close($connection); and all i get now is failure
|
|
|
|
01-20-2009, 12:54 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
|
yes, it's the mysql_close call.
try adding this to see what went wrong.
PHP Code:
echo mysql_error();
|
|
|
|
01-20-2009, 01:20 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
adding it to where !
|
|
|
|
01-20-2009, 01:27 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 132
Name: Will Anderson
Location: Terre Haute, IN
|
the end of the code
|
|
|
|
01-20-2009, 01:40 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
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
|
|
|
|
01-20-2009, 03:01 PM
|
Re: PHP inserting vales into MYSQL
|
Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
|
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..
|
|
|
|
01-21-2009, 05:13 AM
|
Re: PHP inserting vales into MYSQL
|
Posts: 630
Name: James
Location: KENT
|
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
|
|
|
|
01-21-2009, 06:47 AM
|
Re: PHP inserting vales into MYSQL
|
Posts: 3,176
Name: Thierry
Location: I'm the uber Spaminator !
|
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 just after the mysql_query(), what is it's value ?
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
|
« Reply to PHP inserting vales into MYSQL
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
|
|
|