Join 307,157 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,547 people online right now. Registration is fast and FREE... Join Now!
$sql = sprintf("UPDATE WHERE Numero = users.Wing AND users.username = '" . $last ."' wings (CC1,GB1,Radar1,Runway1,Factory1,NP1,Residence1,PP1,Oil1) VALUES ('".$_POST['amount1']."','".$_POST['amount2']."','".$_POST['amount3']."','".$_POST['amount4']."','".$_POST['amount5']."','".$_POST['amount6']."','".$_POST['amount7']."','".$_POST['amount8']."','".$_POST['amount9']."')");
Kinda experimenting here.... Does this look ok?
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Numero = users.Wing AND users.username = 'astin' wings (CC1,GB1,Radar1,Run' at line 1
This post has been edited by shygirl15: 22 Nov, 2008 - 01:53 PM
You also might want to look up SQL injection. As it's written, that line is an invitation to hack your site. Prepared statemetns are your friend in this regard.
You may also want to review the sprintf() function. The purpose of sprintf() is to substitute parameters into a string. However, your entire string is built with straight concatenation, so sprintf() isn't necessary.
QUOTE(shygirl15 @ 22 Nov, 2008 - 03:59 PM)
But I dont want to set it. I want to ADD to what is already in the database. How do I do that please?
Oh well in that case you don't do an update. You do an insert.
Insert into tablename (column list) Values (values list)
No where clause needed here, because you are inserting something brand new into the table so you just need to specify the columns you want to add values for and then the corresponding values to match the columns.
Tip: Remember that the number of values you list in the values list should match the number of columns you specify in the columns list. No more, no less.
astin is my account. under astin, I have the ID "12", and the wing im in "2"
This is the wing database. My wing is "lalala" Notice it is wing #"2", and the commander is ID "12"
notice under CC1, the value "21"
This is the actual website. Notice in the top left sector there is 21 command centers. That is CC1.
The form on the bottom is saposed to insert buildings. Like when i type 5 into the text field next to command centers, and press constuct, it should say "26" in the database.
Can you please tell me how to do this?
This post has been edited by shygirl15: 22 Nov, 2008 - 10:27 PM
White screen means an error, and as you have error_reporting on, there is probably a white text in it
The list updates you create, are all overwriting the previous value in the $query variable _before_ being excecuted. I don't expect you wany only UPDATE wings SET Oil1=Oil1+$add9 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' to be executed.
Furthermore, on the bottom of your code, you: $result = mysql_query($sql)or die (mysql_error());, but you never set $sql.
And finally, there is a redundant (erroneous) <?php in the middle of the pasted code.
Can you stop talking like an absolute geek. You are not at work with colegues who went to college for this stuff just like you. Your talking to a 15 year old who is working her tail off to understand this, and thinks its really impolite for condescending know it alls to explain things in needless big words just to boost there own self asteem.
Also, it would be nice if your could show me how to correct the errors rather then just shooting my code down.
This post has been edited by shygirl15: 23 Nov, 2008 - 03:02 AM
Thids is not geek, this is basic english, I'm sorry. I only copied some of your code, maybe that's what is setting you of?
* You have a number of "$query="UPDATE wings SET". This command puts the string that is behind the =sign into the variable $query. The line after it, you put another string in it, and you ditch the old one before using it. * In the middle of your code, there is a "<?php" that shouldn't be there at all. * You issue a mysql_query, and you pass the variable $sql, in "$result = mysql_query($sql)or die (mysql_error());". But you never put any value into $sql, you probably wanted to use "$query"?
I'd appreciate you read the answers carefully before attacking your helpers, there is _nothing_ geek about this, it is just plain text about what you did.
Explanation of YOUR code::
Set errors on and connect to database (while ignoring warning messages using the @):
CODE
<?php
error_reporting(E_ALL);
$username="151296"; $password="puppypuppy"; $database="151296"; mysql_connect($localhost,$username,$password); @mysql_select_db($database) or die( "Unable to select database");
Read some cookie:
CODE
$last = $_COOKIE['ID_my_site'];
Did you set that cookie? You cannot read a cookie, the name of it looks somewhat like an example as well...
Retrieve all combinations of users and wings, where the wing number is the user's wing, and the username is the same as set in the cookie ($last). And retrieve 0: you select the wing column from all users, where that wing is 0. The result is a list of 0's, as long as the number of 0's in the wing column.
CODE
$query = "SELECT * FROM wings, users where wings.Numero = users.Wing AND users.username = '" . $last ."'"; $query2 = "SELECT Wing From users where users.Wing = 0"; $result = mysql_query($query) or die(mysql_error()); $result2 = mysql_query($query2) or die(mysql_error());
Start PHP? (ERROR: You already did!), remove this.
CODE
<?php
Read 9 values from a form that is POSTed into this page. You'd better use other names for those fields (<input>'s), as no one will ever know what "amount4" will be, just reading the code. "numberWings" is easier to understand.
Put some characters into the variable $query. You dump the contents and put another value in it 9 times.
CODE
$query="UPDATE wings SET CC1=CC1+$add1 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' "; $query="UPDATE wings SET GB1=GB1+$add2 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' "; $query="UPDATE wings SET Radar1=Radar1+$add3 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' "; $query="UPDATE wings SET Runway1=Runway1+$add4 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' "; $query="UPDATE wings SET Factory1=Factory1+$add5 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' "; $query="UPDATE wings SET NP1=NP1+$add6 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' "; $query="UPDATE wings SET Residence1=Residence1+$add7 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' "; $query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' "; $query="UPDATE wings SET Oil1=Oil1+$add9 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' ";
You probably want to do a mysql_query($query) after each line, issueing that query to the database, and storing the values!
Issue a query to the database: $sql. But $sql is not set.
CODE
$result = mysql_query($sql)or die (mysql_error());
Forward the page:
CODE
header("Location: arming.php");
This won't work as it should be the first thing sent to the client. As there were errors in the previous code, a error message has been sent and you cannot change the headers anymore.
Please look at the page's source to see the errors it generates, you use a stylesheet with white text, so you just can't see the errors. They are there! (selecting everything on the page with your mouse should work as well)
This post has been edited by Hary: 23 Nov, 2008 - 03:27 AM
$query="UPDATE wings SET CC1=CC1+$add1 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query="UPDATE wings SET GB1=GB1+$add2 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query="UPDATE wings SET Radar1=Radar1+$add3 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query="UPDATE wings SET Runway1=Runway1+$add4 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query="UPDATE wings SET Factory1=Factory1+$add5 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query="UPDATE wings SET NP1=NP1+$add6 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query="UPDATE wings SET Residence1=Residence1+$add7 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query="UPDATE wings SET Oil1=Oil1+$add9 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$result = $database->query($query);
header("Location: arming.php");
?>
Ok. When you login to the site. You enter your username, and password.
Then it gives you a cookie. This cookie is your username.
for example. My username is "astin"
so
CODE
" '. $last .' "
is reading the cookie, and saying ok THIS PERSON is astin.
now, inside the database, there is a number that identifies the "wing" im in. in my case.. the number is "2".
So that means inside the "users" table.. there is a colum with the username "astin", and the wing "2"
NOW. Inside the "wings" table is where I want the data from the form to be stored. But I need the php to know WHICH column to put the new data in
so this query is saying... update the "wings" database.. the "CC1" row.... and add what is already there.. and add what was entered into the form.. the "WHERE" part of the code.. is basicly saying.. this is column you are going to put it in... its going in "wings".. where the "Numero" which is the same as the "Wing #" (or 2 in this case) so its saying the "Numero" inside the "wings" table should be the same as "Wing" inside the "User" table.... and the colum that were getting the "username" from is "Astin" which the site identifies from the cookie.
$query="UPDATE wings SET CC1=CC1+$add1 WHERE wings.Numero = users.Wing AND users.username = " '. $last .' ";
Now... Knowing what im TRYING and failing to do.. can you please tell me how to do this the correct way?
Read up, I edited my previous post (it took a while ), lining up some errors in your code. I hope you understand what went wrong, be my guest to ask questions if you do not understand my comments.
Read up, I edited my previous post (it took a while ), lining up some errors in your code. I hope you understand what went wrong, be my guest to ask questions if you do not understand my comments.
Thank you for that. *pressed thanks*
Basicly, it's doing what its saposed to.
Its connecting. It's reading the form "thats on another page", and its telling the database where the imput should go "query1"
can you please elaborate this comment?
(Put some characters into the variable $query. You dump the contents and put another value in it 9 times.)
Lastly, are you suggesting I put
CODE
mysql_query($query)
infront of each
CODE
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'";
$query is a variable you place a SQL query in. However, you don't excecute that query on the database before overwriting it again. (You assign a new value (line of text) to it).
You're almost right. I suggest you put mysql_query($query); _after_ each $query="<SQL>";. This way you put a command in a variable, and pass the contents of that variable to the database, using the mysql_query function
CODE
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'"; mysql_query($query) or die ("Error:" . mysql_error());
is the same as:
CODE
mysql_query("UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
$query is a variable you place a SQL query in. However, you don't excecute that query on the database before overwriting it again. (You assign a new value (line of text) to it).
You're almost right. I suggest you put mysql_query($query); _after_ each $query="<SQL>";. This way you put a command in a variable, and pass the contents of that variable to the database, using the mysql_query function
CODE
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'"; mysql_query($query) or die ("Error:" . mysql_error());
is the same as:
CODE
mysql_query("UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
lol, if its the same it wont work anyways, so why does it matter if i change it
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'"; mysql_query($query) or die ("Error:" . mysql_error());
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'"; mysql_query($query) or die ("Error:" . mysql_error());
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'"; mysql_query($query) or die ("Error:" . mysql_error());
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'"; mysql_query($query) or die ("Error:" . mysql_error());
$query="UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'"; mysql_query($query) or die ("Error:" . mysql_error());
Is what you shpould change it to (too lazy to copy the correct code)
mysql_query("UPDATE wings SET CC1=CC1+$add1 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
mysql_query("UPDATE wings SET GB1=GB1+$add2 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
mysql_query("UPDATE wings SET Radar1=Radar1+$add3 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
mysql_query("UPDATE wings SET Runway1=Runway1+$add4 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
mysql_query("UPDATE wings SET Factory1=Factory1+$add5 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
mysql_query("UPDATE wings SET NP1=NP1+$add6 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
mysql_query("UPDATE wings SET Residence1=Residence1+$add7 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
mysql_query("UPDATE wings SET PP1=PP1+$add8 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
mysql_query("UPDATE wings SET Oil1=Oil1+$add9 WHERE wings.Numero = users.Wing AND users.username = '" . $last ."'") or die ("Error:" . mysql_error());
$result=mysql_query($query) or die ("error: ".mysql_error());