|
-
Apr 12th, 2009, 02:54 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Really new at this
Just started out with PHP.Made a nice little page in DW04,using form to send data to my database created in MYSQL.
So here is my code:
form:
Code:
<form name="form1" method="post" action="insert.php">
ime: <input type="text" name="Ime" />
prezime: <input type="text" name="Prezime" />
email: <input type="text" name="email" />
komentar: <textarea name="komentar"></textarea>
<input type="submit" name="POSALJI" value="POSALJI">
</form>
and my insert.php
Code:
<?php
$con = mysql_connect("localhost","myname","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("korisnik", $con);
$sql="INSERT INTO podaci (ime, prezime,email,komentar)
VALUES
('$_POST[ime]','$_POST[prezime]','$_POST[email]','$_POST[komentar])";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo $sql;
mysql_close($con)
?>
The thing is,nothing happens!No data is inserted,no error.Echo displays nothing.Guess I'm missing something here,so if anyone can help...thank you!
-
Apr 12th, 2009, 03:01 PM
#2
Re: Really new at this
you have a parse error. your call to mysql_close() is not terminated (ends with a semicolon); but the call to this function is also un-needed. PHP will automatically close the connection to MySQL, so you can remove that line completely.
you're also missing a single quote in your SQL query (just after $_POST[komentar]), which would make your query not run. but, before inserting any of that information into the database, you should make sure that none of it could break your script. you should never trust input from your users. you can use the function mysql_real_escape_string() to escape all of the necessary characters for your query, in case your user uses single quotes or other such characters.
PHP Code:
$ime = mysql_real_escape_string($_POST['ime']); //repeat for prezime, email, and komentar
$sql="INSERT INTO podaci (ime, prezime,email,komentar) VALUES ('$ime','$prezime','$email','$komentar')";
you can also temporarily turn on error reporting by PHP (if you have it off in your php configuration) by using the error_reporting() function at the top of your script.
PHP Code:
error_reporting(E_ALL);
Last edited by kows; Apr 12th, 2009 at 03:09 PM.
-
Apr 12th, 2009, 03:05 PM
#3
Re: Really new at this
Error reporting should always be turned on to the maximum level during development. To turn it on for every script you need to modify your php.ini file.
What OS are you running PHP on; or are you using hosting space?
-
Apr 12th, 2009, 05:22 PM
#4
Thread Starter
Hyperactive Member
Re: Really new at this
I'm running WinXP,PHP 5.2.9 and Apache HTTP Server 2.2
When I type http://localhost/insert.php into my browser i get this error:
Fatal error: Call to undefined function mysql_connect() in C:\Documents and Settings\admin\My Documents\Test\insert.php on line 3
I read somewhere that php.ini needs to be edited,but dont know how to solve that!
---edit---
Some more info(edited php.ini)
my php.ini file is in C:\WINDOWS
I uncommented the php_mysql.dll
Copied libmysql.dll to C:\Windows\System
set doc_root to: "C:\Documents and Settings\admin\My Documents\Test"
Dont know what else to do
Last edited by BlackRiver; Apr 12th, 2009 at 05:38 PM.
-
Apr 12th, 2009, 07:32 PM
#5
Re: Really new at this
in php.ini, make sure the extension_dir directive is correctly pointing at the directory you have your extensions in. by default, I think it's set to "./", which wouldn't load any of your extensions unless they were in the root php installation directory. I usually use a full path when installing on windows -- something like c:/php5/ext -- but you could also probably just change it to "./ext". alternatively, you could just move php_mysql.dll to the root PHP installation directory and everything should be fine.
-
Apr 13th, 2009, 12:20 AM
#6
Thread Starter
Hyperactive Member
Re: Really new at this
It is pointing at my ext directory and php_mysql.dll is in it!Still,I get the same error!
-
Apr 13th, 2009, 12:57 AM
#7
Re: Really new at this
you've restarted Apache since making all of these changes, right?
-
Apr 13th, 2009, 01:19 PM
#8
Thread Starter
Hyperactive Member
Re: Really new at this
Ooh,I have done everything as described in this tutorial,but when I get to the last part it starts loading and then:
"Apache HTTP Server has encountered a problem...." and it crashes without showing any data.I really dont know what else to do.Any my deadline is Thursday
-
Apr 13th, 2009, 01:58 PM
#9
Thread Starter
Hyperactive Member
Re: Really new at this
Hmmm,I got the select statement working,but my insert into doesnt work!
This is my insert.php:
Code:
<?php
error_reporting(E_ALL);
$con = mysql_connect("localhost","root","napoleon");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("korisnik", $con);
$ime = mysql_real_escape_string($_POST['ime']);
$prezime=mysql_real_escape_string($_POST['prezime'];
$email=mysql_real_escape_string($_POST['email'];
$komentar=mysql_real_escape_string($_POST['komentar'];
$sql="INSERT INTO podaci (ime,prezime,email,komentar) VALUES ('$ime','$prezime','$email','$komentar')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo $sql;
?>
and my html code is same as the above.And still nothing happens
-
Apr 13th, 2009, 01:59 PM
#10
Re: Really new at this
Make sure the libmysql.dll file is copied into either the PHP directory or directory within the PATH environment such as \Windows\System32
-
Apr 13th, 2009, 02:24 PM
#11
Thread Starter
Hyperactive Member
Re: Really new at this
That file is in PHP Directory and in \Windows\System32
Strange really
-
Apr 13th, 2009, 03:16 PM
#12
Re: Really new at this
Do you still get a startup error from Apache?
-
Apr 13th, 2009, 04:59 PM
#13
Re: Really new at this
no, he said his script was working now :x
you're missing the end parenthesis on every call you made to mysql_real_escape_string(). just add that, and your script will run. I've highlighted the lines missing it:
Code:
$ime = mysql_real_escape_string($_POST['ime']);
$prezime=mysql_real_escape_string($_POST['prezime'];
$email=mysql_real_escape_string($_POST['email'];
$komentar=mysql_real_escape_string($_POST['komentar'];
you have error reporting on, so when you run a script like this and get an error, it makes a world of difference if you would just post it as well :)
-
Apr 13th, 2009, 05:53 PM
#14
Re: Really new at this
I appeared to miss post #9 
Another option to change is display_errors = On in php.ini. This is turned off by default in the latest versions of PHP.
-
Apr 14th, 2009, 03:26 AM
#15
Thread Starter
Hyperactive Member
Re: Really new at this
Woke up this morning,looked over my code again and noticed the missing parenthesis.Fixed that!Entered data into textboxes,submited it and it works!Checked back here and smiled at kows post Anyways,thank you guys very much! Resolved!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|