PHP/MySQL What have I done wrong?
hey guys,
This is my first venture into php/mysql but I have found a couple of good tutorials on the net which I have been using. I've got mysql setup as localhost and the username is user, the password is pass. DB name is test and the table I'm using is called testtable. Basically all I'm trying to do at the moment is create a form in html which will submit its contents to the table. This is what I have so far:
PHP Script (test.php)
Quote:
<?
$username="user";
$password="pass";
$database="test";
$forename=$_POST['forename'];
$surname=$_POST['surname'];
$address=$_POST['address'];
$postcode=$_POST['postcode'];
mysql_connect(localhost,$username,$password) or die('Error connecting to mysql');
@mysql_select_db($database) or die("Unable to select database");
$query = "INSERT INTO testtable VALUES ('','$forename','$surname','$address','$postcode')";
mysql_query($query);
mysql_close();
?>
HTML
Quote:
<form action="test.php" method="post">
<input name="forename" type="text" /><br />
<input name="surname" type="text" /><br />
<input name="address" type="text" /><br />
<input name="postcode" type="text" /><br />
<input name="submit" type="submit" value="submit" /></form>
All that happens is when I click submit a blank tab opens titled test.php and thats all that happens. It's probably something stupid but I read the tutorial real close and I can't see what I have done wrong. Any help would be appreciated. Thanks
Re: PHP/MySQL What have I done wrong?
It's probably a null exception or unescaped string. Try echoing out mysql_error() after the mysql_query();
You could also try providing the field names to the query, ie, the full query syntax. eg,
Code:
$query = "INSERT INTO testtable (id, forename, surname, address, postcode) VALUES ('','$forename','$surname','$address','$postcode')";
Better yet, if the id field is auto-incrementing let the database do it;
Code:
$query = "INSERT INTO testtable (forename, surname, address, postcode) VALUES ('$forename','$surname','$address','$postcode')";
Re: PHP/MySQL What have I done wrong?
Two things: firstly your php script doesn't contain any code to output anything to a browser, so it will always show a blank page in a browser.
Secondly, what web server are you using, and are you sure you've set it up correctly to work with php? It might be worth trying something simple like a php Hello World example to make sure that your php scripts are actually running, before you start trying to query databases!
Re: PHP/MySQL What have I done wrong?
ok i did the hello world and php is working correctly using xampp
i did echo mysql_error() as well and I get a blank page again, no error message, nothing entered into the db, i'm not sure whats happening as the php script should output a message if it cant connect to the db or find the table
EDIT: I just tried to run the script with the username, password and dbname empty and it did the same thing, no message like its supposed to... I dunno whats going on:(
Re: PHP/MySQL What have I done wrong?
Two things need fixing.
1. Your PHP appears to configured to only allow <?php - it won't work with just <? unless you enable set short_open_tag = On in your php.ini
2. mysql_connect(localhost - that's not going to work. It'll time out. It should be either mysql_connect("127.0.0.1" or at least put "localhost" in quotes - "127.0.0.1" is safest though.
Add print "affected rows: " . mysql_affected_rows(); under the query to quickly be told that it's working okay.
I tend to put header("Content-type: text/plain"); at the top of the script during diagnostics - it stops the browser trying to interpret the results as HTML.
Re: PHP/MySQL What have I done wrong?
Not wanting to throw more stuff at you, but as soon as you've got this bit working, spend some time reading about SQL injection attacks and why your code is dangerous right now :)
Re: PHP/MySQL What have I done wrong?
Quote:
Originally Posted by
Steve
Not wanting to throw more stuff at you, but as soon as you've got this bit working, spend some time reading about SQL injection attacks and why your code is dangerous right now :)
Magic Quotes Runtime.... Isn't that disabled by default now and probably will stop the script running?
I don't know, because I would sooner write pearl than PHP with MySQL.
Re: PHP/MySQL What have I done wrong?
I'd never trust the server configuration over my code, and yes I believe magic quotes tends to be disabled now. Prepared statements are better too.
Re: PHP/MySQL What have I done wrong?
well it turns out it was the php tags... I knew it would be something stupid like that
As fan as the code being dangerous atm I'm just playing around with it, now that I'm getting the basics I can start looking into that... thanks for all the advice and help guys
Re: PHP/MySQL What have I done wrong?
Good stuff. Validating input is an essential part of any program/script, so once you've got the basic syntax sorted, it should always be next on your list of things to get to grips with. It's too easy to miss things if you leave it too late.
Re: PHP/MySQL What have I done wrong?
Re: PHP/MySQL What have I done wrong?
Re: PHP/MySQL What have I done wrong?
hey guys,
Same project, question is a php/java one but didn't reallly want to open a new thread jus for it as it is a pretty simple one.
Basically I have a text box called tb and I want to get the value it contains to use in a java method.
So far I've got:
Quote:
var tbox = document.getElementById("tb").value;
However the java code stops once it gets here without any error message so obviously this cant be right. Any ideas, or do you need all the html/java code?
Re: PHP/MySQL What have I done wrong?
[pedant]It's javascript not java - very different[/pedant] ;)
If you can't find the element in question or it doesn't have a value property, it's likely to bomb out. My suggestion is get the Firebug plugin for Firefox - its error console should tell you precisely what's going wrong.
Re: PHP/MySQL What have I done wrong?
odds are, and this is just guess work (was years ago i did any jscript!) the document hasn't finished loading, so the element isn't there yet.
Re: PHP/MySQL What have I done wrong?
Make sure that the textbox has the id tb, not the name of it, as getElementById has a clue in the last two letters ;)
Or failing that, as already said it is running too soon, and the document is not fully loaded.