ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need help with PHP again. I'll post all my steps, so you can understand better my background. I wanted to create a page, with one single <table> to display my list of Xbox games. That is easy to do, but I wanted to create also another page where I could update the database, without the need to fireup mysql. Here is what I did:
1 - create a database called "gamelist". In that database, I created a table with some initial values:
Code:
mysql> show tables;
+--------------------+
| Tables_in_gamelist |
+--------------------+
| xbox |
+--------------------+
1 row in set (0.00 sec)
Code:
mysql> show columns from xbox;
+-------------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------+----------------+
| id | tinyint(4) | | PRI | NULL | auto_increment |
| title | char(255) | | | | |
| manufacture | char(80) | | | | |
| genre | char(30) | | | | |
+-------------+------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
Code:
mysql> select * from xbox;
+----+-------------+-------------+---------------------------+
| id | title | manufacture | genre |
+----+-------------+-------------+---------------------------+
| 1 | Blood Rayne | Majesto | 3rd Person shooter/Action |
| 2 | Halo | Bungie | 1st Person shooter |
+----+-------------+-------------+---------------------------+
2 rows in set (0.00 sec)
Cool, now yo've an idea how the database is organized. Now to the coding. Here is a simple page which displays the above database results:
That works just fine. The problem is here... I've a simple page, called update.php with one form and 3 fields, plus a submit button. Whenever I fill the fields and submit the data, mysql is not updated, not I get error messages....
Originally posted by Megaman X Now I get the error: Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /srv/www/htdocs/update.php on line 14
where line 14 is:
$sql = "INSERT INTO xbox (title, manufacture, genre) VALUES
($_POST['title'], $_POST['manufacture'], $_POST['genre'])";
You get an error because in the sql statement, variables have to be between ' '.
Now either define a temp variable before the statement ($genre = $_POST['genre']) and insert that into the statement or insert the POST varibale like that into the statement
"INSERT INTO xbox (title, manufacture, genre) VALUES ('".$_POST['title']."', '".$_POST['manufacture']."', '".$_POST['genre'])."' "
by Charalambos You get an error because in the sql statement, variables have to be between ' '.
i think you'll find although that is a problem its not the problem as that would have caused the query to fail which wouldnt have bothered php in the slightest. the actual problem is double quotes dont evaluate arrays properly, that is why(imho) you should use sprintf to do your queries, not only does it avoid this problem but i think it makes them more readable too.
PHP Code:
<?php
if ($_POST['submit']){ //this ones correct
$db = mysql_connect("localhost", "root");
mysql_select_db("gamelist", $db);
$sql = sprintf("INSERT INTO xbox (title, manufacture, genre) VALUES ('%s', '%s', '%s')", $_POST['title'], $_POST['manufacture'], $_POST['genre']); //now the arrays are not in double quotes
OMG, Charalambos and kev82, you are both great at this. First, I know how difficult it is to read such a terrible written code as mine...
I've tried Charalambos method, and I got a simple parsing error instead. Still, with all my incompetence, I could not fix it. kev82's method does, works amazingly well .
I cannot thank you guys enough. Thanks!
P.S: PHP is harder then I though it would be, but cool nonetheless
by Megaman X PHP is harder then I though it would be, but cool nonetheless
my first PHP thing was a table of links, just like bookmarks but you can access it from any computer/browser combination, and its still my favourite. one of the most useful things i ever wrote.
im slowly trying to re-create something like this for a set of games called suikoden but doing it (well) is a bit out of my league at the moment i need to write a set of classes to let me produce html constructs on the fly but im no good at design which is what that needs.
I looked into the site. Very good idea, if I could, I'd do that with Megaman series . But besides being also out of my league (if it's far from yours, think how far from my league that is...ghehe), I also dont have much time to play with my computer atm
I've one more question though... a silly one . How would I go by displaying the game list in an inverted order. I mean, where the newest game come on the top of the list. I believe that something should be done at this line:
PHP Code:
$result = mysql_query("SELECT * FROM xbox",$db);
at the first page, something like "WHERE id=?" maybe...
the statement your looking for is order by, but the only field you have that is suitable for your purpose is id making the query
select * from xbox order by id descending
but this is a bad idea as it induces a dependance on id which i assume is to be used for joining, doing this can make migration to another db very difficult.
what you need to add is another column, something like date_purchased and then make your query
select * from xbox order by date_purchased descending
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.