[MySQL & PHP Question...] -



MySQL & PHP Question...

Discuss MySQL & PHP Question...



Posted by: shan

Ok, on my quest of learning PHP and MySQL, i want to make a [U]simple[/U] database project. Here's what I want to do:

Create a page where users just use a simple inputbox and drop down list to answer to questions 'name' and 'something (i haven't decided hahah)' and i just want to be able to store it to the database, and then on a sperate page i want to be able to call and print the database. I am good at this part, calling and printing the database. But I just want to know how I would go about taking the user input and storing it :confused:. This would be a much appreciated reply with instructions or a website or something like that :) :D

Thanks

~Shan



Posted by: þÄ®âÐÖx

You might wanna try this PHP/MySQL tutorial.. it looks pretty simple

[url]http://www.freewebmasterhelp.com/tutorials/phpmysql/1[/url]



Posted by: shan

****, looks good. Why couldn't i find that site ? :D hahaha



Posted by: shan

Ok, techies: Riddle me this.

Here's the form,
[PHP]<form action="os.php" method="post">
Submit this form<br>
<pre>Your Name:<input type="text" name="name"><br>
Your Os:<select name="os">
<option value="Windows 95">Windows 95</option>
<option value="Windows 98">Windows 98</option>
<option value="Windows ME">Windows ME</option>
<option value="Windows 2K">Windows 2K</option>
<option value="Windows XP">Windows XP</option>
<option value="RedHat Linux">RedHat Linux</option>
<option value="Mandrake Linux">Mandrake Linux</option>
<option value="Suse Linux">SuSe Linux</option>
<option value="Other Linux">Other Linux</option>
<option value="Unix">Unix</option>
<option value="Sun OS">Sun OS</option>
<option value="Other">Other</option>
</select><br></pre>
<input type="Submit" value="Submit"><input type="Reset" value="Reset"></form>
[/PHP]
Here's the PHP script from addrec.php
[PHP]
<?php
mysql_connect("localhost", "shan", "****")
or die("Could not connect, the server may be down, or bad host, user, or password");
mysql_select_db("tests") or die("Could not select database");
$query = "INSERT INTO os VALUES ('', '$os')";
$result = mysql_query($query) or die("Query failed, please hit back and try again!");
print ("<b><u>Database updated you can view it now, </b></u>");
mysql_query($query);
mysql_close();
?>
<a href="osdb.php">the database is here</a>
[/PHP]
But when I go to view the database the only thing stored is the Auto Incriment ID. It's up to 6 with six test posts, but the name and os doesn't appear anywhere in the database, any solutions?

EDIT: Actually what it's doing is creating 2 more rows (blank rows) every time you submit. So say there are 8 rows, you enter your name and choose your OS. Well on row 9 should reside the name and OS, but it is blank, along with an added row, 10, which is also blank? :confused: :confused: :confused: :confused:
Thanks for all of your help!!!! :D

~Shan



Posted by: shan

argh, I've modified the script to this now,
[PHP]
<?php

mysql_connect("localhost", "shan", "******") or die("Could not connect to database server");
mysql_select_db("tests") or die("Could not locate database");

$query = "INSERT INTO os VALUES ('','os')";

$result = mysql_query($query) or die("Failure to write to database");
print("<B><U>Database has been updated</u></b> ");

mysql_close();
?>
[/PHP]
<a href="osdb.php">you may view the database here</a>

Well, now I only get 1 blank line, i guess I'm making progress? :confused: :D ahhahaha



Posted by: shan

Hey Shan, I found this on the php website, could this be your problem?
[QUOTE]
Note: The default value for the PHP directive register_globals changed from on to off in PHP 4.2.0. [/quote]



Posted by: þÄ®âÐÖx

Hahaha, yeah that progress is called "learning"... unfortunately I can't be much help to you as I haven't really looked at MySQL much yet.

Good luck though! :)



Posted by: shan

OH WOW, it's about time i figure this out. Thanks for all you help Shan!!!

lmfao!! hahhahhaha :D :D

I'm bored at work, hahaha and I didn't sleep well last night, so I'm a little odd today. It's kinda funny I actually found this while on an ASP site researching some stuff for work. :) I'm so happy.



Posted by: ikellen

OK let me fix up your script a bit..........some of your messaging techniques are quite odd, especially because your script prints "Database updated" before the database is even queried, so if it fails, you look like an idiot. Here's my revisions:
[php]
<?php
$link = mysql_connect("localhost", "shan", "******") or die("Could not connect to database server");
mysql_select_db("tests") or die("Could not locate database");

$query = "INSERT INTO os VALUES ('0','os')";
$result = mysql_query($query) or die(mysql_error($link));
if($result)
{
print("<B><U>Database has been updated</u></b> ");
}

mysql_close($link);
?>
[/php]

OK here's what I added-First it's good to referrence your connection as a variable, that way you can just write the variable as an argument to functions. Second, if you are auto incrementing, you have to out '0' in as your id value, and it will auto-increment then. Lastly, I made your script only write out the "Database updated" message when the query is successful. Hope this helps :)



Posted by: wilfredwaterloo

You can find powerful scripts in
[url]www.hotscripts.com[/url]
There are various sample to follow.