/    Sign up×
Community /Pin to ProfileBookmark

MySQL INSERT INTO query problem

im having problems getting a mysql query to add data to a table.

[code=php]
<?php
$db = mysql_connect(“localhost”,”autowar”,”password”) or
die(“Unable to connect to the server. Please try again later.”);
mysql_select_db(“autowar_uk_db”,$db);
$query=”INSERT INTO details (uname,pword,email) VALUES ($uname,$pword,$email)”;
$result=mysql_query($query) or die(“There was an error, possibly because the server is overloaded or because Jacob couldn’t code a turtle.”);
mysql_close();
?>
[/code]

the $uname, $pword and $email vars are the names of the text boxes in the form that this script is the action of

to post a comment
PHP

21 Comments(s)

Copy linkTweet thisAlerts:
@NevermoreOct 01.2003 — Try:

[code=php]
<?php

$db = mysql_connect("localhost","autowar","password") or

die("Unable to connect to the server. Please try again later.");

mysql_select_db("autowar_uk_db",$db);

$query="INSERT INTO 'details' ('uname','pword','email') VALUES ('$uname','$pword','$email')";

$result=mysql_query($query) or die("There was an error, possibly because the server is overloaded or because Jacob couldn't code a turtle.");

mysql_close();

?>[/code]


I can code a turtle:


[CODE]
_____
oo/><><>
( -)><><><>
L|_|L|_|`[/CODE]
Copy linkTweet thisAlerts:
@fyrestrtrOct 07.2003 — This sounds like a register_globals issue.

Remember that in recent versions of PHP ... register_globals is set to off by default for security reasons.

So, if your form is being posted, use $_POST and if its method is GET use $_GET.

More information about [url=http://www.php.net/register_globals]register globals[/url] and the new [url=http://www.php.net/manual/en/language.variables.predefined.php]"superglobal " arrays[/url] in the php manual.
Copy linkTweet thisAlerts:
@NevermoreOct 07.2003 — Ahh, you see I have some background knowledge - he is on tripod.co.uk which has register_globals on. To me it looked like a problem with missing inverted commas.
Copy linkTweet thisAlerts:
@fyrestrtrOct 07.2003 — It also doesn't hurt to use some built-in error checking.

http://www.php.net/mysql-error
Copy linkTweet thisAlerts:
@pyroOct 07.2003 — Whether register_globals is on or off, you shouldn't use them...
Copy linkTweet thisAlerts:
@NevermoreOct 07.2003 — Since he hasn't replied I think all this is purely academic.
Copy linkTweet thisAlerts:
@tinernetauthorOct 10.2003 — iv not had any luck with that code jacob and just because u can draw one dosent mean u can code em!?

not sure what your on about im not good with php at all. do i need to set values for every field in the table or will the mysql automatically put in their default values. there more than just the 3 in the script.

thanks evry1x
Copy linkTweet thisAlerts:
@NevermoreOct 10.2003 — Is it erroring at all, or is the data just not appearing in the database?

Please post the whole code.
Copy linkTweet thisAlerts:
@tinernetauthorOct 11.2003 — wen i click submit it ses the die note thingy i.e. jacopb couldnt code a turtle

html:<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta name="Content-Script-Type" content="text/javascript"&gt;
&lt;meta name="Content-Style-Type" content="text/css"&gt;
&lt;title&gt;Autowar SignUp&lt;/title&gt;

&lt;style type="text/css"&gt;
&lt;!--
label {display:block; margin:1em 0eml; text-align:right}
input {margin:0em 1ex}
fieldset {padding:1em}
legend {font-weight:bold}
--&gt;
&lt;/style&gt;

&lt;script type="text/javascript"&gt;
&lt;!--
String.prototype.isEmailAddress = function () {return this.match(/^[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)*@[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+(?:.[^x00-x20()&lt;&gt;@,;:\".[]x7f-xff]+)+$/)}

function validate(f) {
if (f.agree.checked==false){alert('You must agree to the terms and conditions'); f.agree.focus(); return false};
if (!/S/.test(f.uname.value)) {alert ('You must enter a User Name.'); f.uname.focus(); return false};
if (!/S/.test(f.pword.value)) {alert ('You must enter a Password.'); f.pword.focus(); return false};
if (!/S/.test(f.conpword.value)) {alert ('You must confirm your password.'); f.conpword.focus(); return false};
if (f.conpword.value != f.pword.value) {alert ('Your passwords much match.'); f.conpword.focus(); return false};
if (!/S/.test(f.email.value)) {alert ('You must enter a valid email address.'); f.email.focus(); return false};
if (!/S/.test(f.conemail.value)) {alert ('You must confirm your email address.'); f.conemail.focus(); return false}; <br/>
if (f.conemail.value != f.email.value) {alert ('Your email addresses must match.'); f.conemail.focus(); return false};
}
// --&gt;
&lt;/script&gt;
&lt;div align="center"&gt;
&lt;form action="signup.php" onsubmit="return validate(this)"&gt;
&lt;div style="width:25em"&gt;
&lt;fieldset&gt;
&lt;legend&gt;Terms &amp; Conditions&lt;/legend&gt;
&lt;textarea rows="10" cols="40"&gt;1. I will not attempt to hack another username or gain enrtry to the server.
2. I will not use the game as a way of sending offencive messages with mallicious intent.
3. I will not create more than one account.
4. I agree to having my IP banned from playing the game by the webmaster for a good reason.
5. I will not copy, reproduce or sell this game in any way.
&lt;/textarea&gt;
&lt;labe style="text-align:center"&gt;&lt;input type=checkbox name="agree"&gt;I agree to the AutoWar Terms &amp; Conditions&lt;label&gt;
&lt;/fieldset&gt;
&lt;fieldset&gt;
&lt;legend&gt;Register&lt;/legend&gt;
&lt;label&gt;User Name:&lt;input type="text" size="20" name="uname"&gt;&lt;/label&gt;
&lt;label&gt;Password:&lt;input type="password" size="20" name="pword"&gt;&lt;/label&gt;
&lt;label&gt;Confirm Password:&lt;input type="password" size="20" name="conpword"&gt;&lt;/label&gt;
&lt;label&gt;Email Address:*&lt;input type="text" name="email" onchange="if (!this.value.isEmailAddress()) {alert ('That would not appear to be a valid email address.'); this.value=''; this.focus()}"&gt;&lt;/label&gt;
&lt;label&gt;Confirm Email Address:*&lt;input type="text" name="conemail" onchange="if (!this.value.isEmailAddress()) {alert ('That would not appear to be a valid email address.'); this.value=''; this.focus()}"&gt;&lt;/label&gt;
*Must be correct
&lt;/fieldset&gt;
&lt;div style="text-align:center"&gt;&lt;br&gt;
&lt;button type="submit"&gt;Sign Up&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/html&gt;

php:
[code=php]
<?php

$db = mysql_connect("localhost","autowar","password") or

die("Unable to connect to the server. Please try again later.");

mysql_select_db("autowar_uk_db",$db);

$query="INSERT INTO 'details' ('uname','pword','email') VALUES ($uname,$pword,$email)";

$result=mysql_query($query) or die("There was an error, possibly because the server is overloaded or because Jacob couldn't code a turtle.");

mysql_close();

?>
[/code]

my mysql db is act screwed atm but it didnt work when the table was workin.
Copy linkTweet thisAlerts:
@tinernetauthorOct 12.2003 — iv made a new mysql table and changed this accordingly but still havin no luck.

HELP!
[code=php]
<?php

$db = mysql_connect("localhost","epicgame","biffyclyro") or

die("Unable to connect to the server. Please try again later.");

mysql_select_db("epicgame_uk_db",$db);

$query="INSERT INTO 'epic' ('uname','pword','email') VALUES ($uname,$pword,$email)";

$result=mysql_query($query) or die("There was an error, possibly because the server is overloaded or because Jacob couldn't code a turtle.");

mysql_close();

?>
[/code]
Copy linkTweet thisAlerts:
@pyroOct 12.2003 — Are you getting an error? If so, what is it?
Copy linkTweet thisAlerts:
@tinernetauthorOct 12.2003 — wel wen i get rid of the or die("") bit after the query it gives me:

Parse error: parse error in /data/members/free/tripod/uk/e/p/i/epicgame/htdocs/signup.php on line 13

and line 13 is the mysql_close(); line

iv tried single quotes round the variables in the query but that dusnt work either.
Copy linkTweet thisAlerts:
@pyroOct 12.2003 — Try writing this line: [code=php]$query="INSERT INTO 'epic' ('uname','pword','email') VALUES ($uname,$pword,$email)";[/code] as [code=php]$query="INSERT INTO epic (uname,pword,email) VALUES ('$uname','$pword','$email')";[/code]

also, the $results = mysql_query($query) line could be written as mysql_query($query), as you don't need to return the results of that query, it appears.
Copy linkTweet thisAlerts:
@pyroOct 12.2003 — Do these variables exist? $uname, $pword, $email

You shouln't be using [url=http://www.webdevfaqs.com/php.php#globalvariables]global variables[/url] (I assumed you had defined them eariler).

If you use the POST method with your form (as you should), add this:

[code=php]$uname = $_POST['uname'];
$pword = $_POST['pword'];
$email = $_POST['email'];[/code]
Copy linkTweet thisAlerts:
@tinernetauthorOct 12.2003 — nope that dusnt work either. i tried it with and without the quotes round the variables. wtf is rong with it?
Copy linkTweet thisAlerts:
@pyroOct 12.2003 — Post your entire script -- from the <?PHP to the ?>
Copy linkTweet thisAlerts:
@tinernetauthorOct 12.2003 — [code=php]
<?php
$uname = $_POST['uname'];
$pword = $_POST['pword'];
$email = $_POST['email'];
$db = mysql_connect("localhost","epicgame","biffyclyro") or
die("Unable to connect to the server. Please try again later.");
mysql_select_db("epicgame_uk_db",$db);
$query="INSERT INTO 'epic' ('uname','pword','email') VALUES ($uname,$pword,$email)";
mysql_query($query) or die("There was an error, possibly because the server is overloaded or because Jacob couldn't code a turtle.");
mysql_close();
?>
[/code]

by the way thanks in advance what u guys do no here is great.
Copy linkTweet thisAlerts:
@pyroOct 12.2003 — You didn't use the query I gave you:

[code=php]$query="INSERT INTO epic (uname,pword,email) VALUES ('$uname','$pword','$email')";[/code]

Though for portability, you might as well use:

[code=php]$query="INSERT INTO epic (uname,pword,email) VALUES ('".$uname."','".$pword."','".$email."')";[/code]
Copy linkTweet thisAlerts:
@tinernetauthorOct 12.2003 — yay it works now thatnks so much pyro.

btw i did use ur query it just didnt work thats all......
Copy linkTweet thisAlerts:
@pyroOct 12.2003 — You're welcome...
×

Success!

Help @tinernet spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...