Just starting in PHP...
deicist
Manchester, UK
I'm trying to teach myself PHP / MYSQl and I can't get this to work... I'm sure PHP is working, and I'm sure MYSQL is working (I have a joomla installation running fine on the same server). I've created my database using:
then I start the PHP:
this is my database setup, I do this seperately because I like modularization:
dbinit.inc.php
[php]
<?
$username="root"
$password="password"
$database="test"
?>
[/php]
then I have my add function:
add.php
[php]
<?
include("dbinit.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("unable to find database");
$query = "INSERT INTO testtable (title,book,issue,releasename,file) VALUES('$Title','$Book','$Issue','$Release_name','$File')";
mysql_query($query);
mysql_close();
?>
[/php]
and the HTML form I'm using to call the above funtion
add.html
When I hit the submit button I get a blank page (not a problem) and nothing goes into the database (problem). So, where am I going wrong?
things I've tried:
checking database connectivity from PHP: I can connect to mysql and select the database fine.
changing the query in 'add.php'. I've tried using constants instead of variables in the query and nothing happens.
I'm sure this is something really basic that I'm doing wrong but I can't see where
mysql wrote:Mysql -u root -ppassword
create database test
use test
Create table testtable (id int(50) not null auto_increment primary key, title varchar(100) not null, book varchar(100) not null, issue int(50) not null, releasename varchar(200) not null, file varchar(300) not null))
then I start the PHP:
this is my database setup, I do this seperately because I like modularization:
dbinit.inc.php
[php]
<?
$username="root"
$password="password"
$database="test"
?>
[/php]
then I have my add function:
add.php
[php]
<?
include("dbinit.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("unable to find database");
$query = "INSERT INTO testtable (title,book,issue,releasename,file) VALUES('$Title','$Book','$Issue','$Release_name','$File')";
mysql_query($query);
mysql_close();
?>
[/php]
and the HTML form I'm using to call the above funtion
add.html
<html><form action="add.php" method="post"> Title: <input type="text" name="Title"></br> Book: <input type="text" name="Book"></br> Issue: <input type="text" name="Issue"></br> Release name: <input type="text" name="Release_name"></br> File: <input type="text" name="File"></br> <input type="Submit"> </form></html>
When I hit the submit button I get a blank page (not a problem) and nothing goes into the database (problem). So, where am I going wrong?
things I've tried:
checking database connectivity from PHP: I can connect to mysql and select the database fine.
changing the query in 'add.php'. I've tried using constants instead of variables in the query and nothing happens.
I'm sure this is something really basic that I'm doing wrong but I can't see where
0
Comments
should be:
mysql_connect('localhost',$username,$password);
----
You are also trying to submit a form but have not read on SuperGlobals and PHP security
When variables are submitted through a form, they are passed into a superglobal array that matches the HTML form submission. In this case POST.
$Title should be $_POST
$Book should be $_POST
This will change the string element of your query and so should be written differently.
Try this:
[php]
$query = "INSERT INTO testtable (title,book,issue,releasename,file) VALUES('$_POST','$_POST','$_POST','$_POST','$_POST')";
[/php]
Make the changes in bold to the INSERT query (Don't forget inside VALUES() is a empty quote before title!)
To see if the variables are being passed to your code, comment out the MySQL query commands and print the query:
[php]
<?
include("dbinit.inc.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("unable to find database");
$query = "INSERT INTO testtable (title,book,issue,releasename,file) VALUES('$_POST','$_POST','$_POST','$_POST','$_POST')";
print $query;
//mysql_query($query);
//mysql_close();
?>[/php]
If it comes out as:
INSERT INTO testtable (title,book,issue,releasename,file) VALUES('','','','','')
Then you know something's not quite right.
If you're not going to test the values ($_POST) before inserting them directly into SQL then you'll need to do this:
DO THIS:
VALUES('{$_POST}','{$_POST}','{$_POST}','{$_POST}','{$_POST}')
I took it out of PHP blocks to show the bold { }. Add them in and it will work.
A good practice is to do something like:
[php]
$title = strip_tags($_POST["Title"]); // Strip HTML and PHP tags from a string
$title = mysql_real_escape_string($title); // Escapes special characters in a string for use in a SQL statement
$query = "...VALUES('{$title}',....";
[/PHP]
How are you getting on Deicist..?
@mysql_select_db('dcp') or die('unable to find database');
Line. No matter what I do I can't get it to select the database. I'm beginning to think there is a problem with my PHP installation. I'll try it again tomorrow on a working LAMP installation.
Also, try using the MySQLi extensions and see if that makes a difference (could be a bad extension).
For development, you should have it at E_ALL | E_STRICT
Also, make sure display_errors is set to on.
Both properties are set in php.ini
MySQL: MySQL 4.1.0 (limited) and below.
MySQLi: MySQL 4.1.0 (full) and above. I always turn on errors to help out. You can do it with PHP code instead of changing PHP.ini settings. Just add to the top of the PHP script!
[PHP]ini_set("error_reporting","E_ALL"); // Display all error types.
ini_set("display_errors","On"); // Display errors.[/PHP]
[php]
$link = mysqli_connect("$dbServer", "$dbUser", "$dbPass") or die("Could not connect");
print "Connected successfully<br>";
mysqli_select_db("$dbName") or die("Could not select database");
print "Database selected successfully<br>";
[/php]
gives me 'could not select database' every time. Any idea what's wrong anyone? nerfed MySQL installation? nerfed PHP installation? I can select the database from MYSQL (from the command line) just not through PHP.
MySQL and MySQLi vary a little in the commands.
For example:
[php]mysql_select_db ( string database_name [, resource link_identifier] )
mysqli_select_db ( mysqli link, string dbname )[/php]
In MySQL (no i) the name of the DB goes first, followed by the optional connection resource. In MySQLi the connection resource is required first, followed by the DB name.
Before you go switching between MySQLi and no i you should really find out what version of MySQL you are running and code accordingly. A more advanced way would to create a set of functions (or class) to handle all SQL the same and adjust automatically between MySQL and MySQLi. That's a little harder though. You just need to find which one to use and practice using it
Use that to display helpful hints and notices to make sure your code is immaculate.
Now, another related question. I want to input a lot of data, and I understand I can do this by saving SQl scripts to files and running them somehow. How would I go about doing this? What specific commands do I need to put in the file and how do I then 'run' the file?
edit: never mind, sussed it
Cheers again for the help guys
Cause people say that Java is Similar to C++, and PHP is similar to C++, so I draw the conclusion that PHP is similar to Java.
At any rate, they are all oop, and while everything I have coded in so far has been, we havent actually used classes or created objects, so I am excited to learn all that I can before going into college. only bad thing about the class is that I have to fork over $80 for the AP Exam at the end of the year. and they said public education was free...hah.
yeh, its like C++ and VB, she had us learn C++ (very little of it...) first, then we went into VB, and since I knew switches, loops, variables, and all that good stuff, I just had to learn the different command to do what I want, as I already knew its function.