MySQL & PHP Question...

Status
Not open for further replies.

shan

Daemon Poster
Messages
1,364
Ok, on my quest of learning PHP and MySQL, i want to make a simple 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
 

shan

Daemon Poster
Messages
1,364
Ok, techies: Riddle me this.

Here's the form,
PHP:
<form action="os.php" method="post">
Submit this form

<pre>Your Name:<input type="text" name="name">

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>
</pre>
<input type="Submit" value="Submit"><input type="Reset" value="Reset"></form>
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();
?>
[url="osdb.php"]the database is here[/url]
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
 

shan

Daemon Poster
Messages
1,364
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();
?>
you may view the database here

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

shan

Daemon Poster
Messages
1,364
Hey Shan, I found this on the php website, could this be your problem?
Note: The default value for the PHP directive register_globals changed from on to off in PHP 4.2.0.
 

shan

Daemon Poster
Messages
1,364
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.
 

ikellen

Baseband Member
Messages
90
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);
?>
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 :)
 
Status
Not open for further replies.
Top