Click to See Complete Forum and Search --> : MySQL INSERT INTO query problem
tinernet
10-01-2003, 02:23 PM
im having problems getting a mysql query to add data to a table.
<?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();
?>
the $uname, $pword and $email vars are the names of the text boxes in the form that this script is the action of
Nevermore
10-01-2003, 02:38 PM
Try:
<?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();
?>
I can code a turtle:
_____
oo/><><>\
( -)><><><>
L|_|L|_|`
fyrestrtr
10-07-2003, 12:52 AM
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 register globals (http://www.php.net/register_globals) and the new "superglobal " arrays (http://www.php.net/manual/en/language.variables.predefined.php) in the php manual.
Nevermore
10-07-2003, 01:03 AM
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.
fyrestrtr
10-07-2003, 02:00 AM
It also doesn't hurt to use some built-in error checking.
http://www.php.net/mysql-error
Whether register_globals is on or off, you shouldn't use them...
Nevermore
10-07-2003, 11:50 AM
Since he hasn't replied I think all this is purely academic.
tinernet
10-10-2003, 07:44 AM
iv not had any luck with that code jacob and just because u can draw one dosent mean u can code em!:P
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
Nevermore
10-10-2003, 01:00 PM
Is it erroring at all, or is the data just not appearing in the database?
Please post the whole code.
tinernet
10-11-2003, 08:26 AM
wen i click submit it ses the die note thingy i.e. jacopb couldnt code a turtle
html:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<meta name="Content-Style-Type" content="text/css">
<title>Autowar SignUp</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0eml; text-align:right}
input {margin:0em 1ex}
fieldset {padding:1em}
legend {font-weight:bold}
-->
</style>
<script type="text/javascript">
<!--
String.prototype.isEmailAddress = function () {return this.match(/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\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};
if (f.conemail.value != f.email.value) {alert ('Your email addresses must match.'); f.conemail.focus(); return false};
}
// -->
</script>
<div align="center">
<form action="signup.php" onsubmit="return validate(this)">
<div style="width:25em">
<fieldset>
<legend>Terms & Conditions</legend>
<textarea rows="10" cols="40">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.
</textarea>
<labe style="text-align:center"><input type=checkbox name="agree">I agree to the AutoWar Terms & Conditions<label>
</fieldset>
<fieldset>
<legend>Register</legend>
<label>User Name:<input type="text" size="20" name="uname"></label>
<label>Password:<input type="password" size="20" name="pword"></label>
<label>Confirm Password:<input type="password" size="20" name="conpword"></label>
<label>Email Address:*<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()}"></label>
<label>Confirm Email Address:*<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()}"></label>
*Must be correct
</fieldset>
<div style="text-align:center"><br>
<button type="submit">Sign Up</button>
</div>
</form>
</div>
</html>
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();
?>
my mysql db is act screwed atm but it didnt work when the table was workin.
tinernet
10-12-2003, 12:32 PM
iv made a new mysql table and changed this accordingly but still havin no luck.
HELP!
<?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();
?>
Are you getting an error? If so, what is it?
tinernet
10-12-2003, 12:47 PM
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.
Try writing this line: $query="INSERT INTO 'epic' ('uname','pword','email') VALUES ($uname,$pword,$email)"; as $query="INSERT INTO `epic` (`uname`,`pword`,`email`) VALUES ('$uname','$pword','$email')";
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.
tinernet
10-12-2003, 12:55 PM
nope no luck. heres the url:
http://members.lycos.co.uk/epicgame/add.html
Do these variables exist? $uname, $pword, $email
You shouln't be using global variables (http://www.webdevfaqs.com/php.php#globalvariables) (I assumed you had defined them eariler).
If you use the POST method with your form (as you should), add this:
$uname = $_POST['uname'];
$pword = $_POST['pword'];
$email = $_POST['email'];
tinernet
10-12-2003, 01:09 PM
nope that dusnt work either. i tried it with and without the quotes round the variables. wtf is rong with it?
Post your entire script -- from the <?PHP to the ?>
tinernet
10-12-2003, 01:12 PM
<?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();
?>
by the way thanks in advance what u guys do no here is great.
You didn't use the query I gave you:
$query="INSERT INTO `epic` (`uname`,`pword`,`email`) VALUES ('$uname','$pword','$email')";
Though for portability, you might as well use:
$query="INSERT INTO `epic` (`uname`,`pword`,`email`) VALUES ('".$uname."','".$pword."','".$email."')";
tinernet
10-12-2003, 01:19 PM
yay it works now thatnks so much pyro.
btw i did use ur query it just didnt work thats all......