Just starting in PHP...

deicistdeicist Manchester, UK
edited August 2006 in Internet & Media
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:
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 :(

Comments

  • ShortyShorty Manchester, UK Icrontian
    edited August 2006
    mysql_connect(localhost,$username,$password);

    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]
  • deicistdeicist Manchester, UK
    edited August 2006
    Cheers shorty, made those changes but still the same result :(
  • Park_7677Park_7677 Missouri Member
    edited August 2006
    deicist wrote:

    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))

    ...

    $query = "INSERT INTO testtable (id,title,book,issue,releasename,file) VALUES('','$Title','$Book','$Issue','$Release_name','$File')";

    Make the changes in bold to the INSERT query :) (Don't forget inside VALUES() is a empty quote before title!)
  • KwitkoKwitko Sheriff of Banning (Retired) By the thing near the stuff Icrontian
    edited August 2006
    Park, you don't want to include the ID in the insert, since it's an auto-incrementing primary key.

    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.
  • Park_7677Park_7677 Missouri Member
    edited August 2006
    KwitCo™ wrote:
    Park, you don't want to include the ID in the insert, since it's an auto-incrementing primary key.
    I've always done it with. Works both ways I suppose, just whatever you're used to :cool:

    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]
  • ShortyShorty Manchester, UK Icrontian
    edited August 2006
    Park is absolutely on sanitising the data passed from the browser/form! :)

    How are you getting on Deicist..? :)
  • deicistdeicist Manchester, UK
    edited August 2006
    right... I decided to put a print line in after every line of code just to see where it was falling down. The 'include' line seemed to be causing problems so I eventually just put the username and password in the connect line. Now it's falling down at the:

    @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.
  • jhenryjhenry California's Wine Country
    edited August 2006
    Just a quick thought, are you sure the mysql account you are using has read permissions to that database. (I hope and assume you're not using root)

    Also, try using the MySQLi extensions and see if that makes a difference (could be a bad extension).
  • deicistdeicist Manchester, UK
    edited August 2006
    I'm using root at the moment, obviously when I'm doing something properly I'll use a limited account but while I'm getting this working I'm just using root.
  • jhenryjhenry California's Wine Country
    edited August 2006
    What is your PHP error display level at?

    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
  • Park_7677Park_7677 Missouri Member
    edited August 2006
    jhenry wrote:
    Also, try using the MySQLi extensions and see if that makes a difference (could be a bad extension).
    This is a possibility deicist.

    MySQL: MySQL 4.1.0 (limited) and below.
    MySQLi: MySQL 4.1.0 (full) and above.
    jhenry wrote:
    What is your PHP error display level at?

    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
    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]
  • deicistdeicist Manchester, UK
    edited August 2006
    Okay, I've narrowed the problem down. For some reason I can't select the database. I've switched to MySQLi but that gives the same problem. I tried creating a new database, same problem. running this:

    [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.
  • Park_7677Park_7677 Missouri Member
    edited August 2006
    What version of MySQL are you using?

    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 :)
  • jhenryjhenry California's Wine Country
    edited August 2006
    ini_set("error_reporting","E_ALL | E_STRICT");  // Display all error types. 
    ini_set("display_errors","On");  // Display errors.
    

    Use that to display helpful hints and notices to make sure your code is immaculate.
  • deicistdeicist Manchester, UK
    edited August 2006
    okay, cheers for all the help guys, I have it working now after much trial and error :)

    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 :)
  • airbornflghtairbornflght Houston, TX Icrontian
    edited August 2006
    I can wait till after this year, then I should be able to pick up php fairly easy, at least I figure.

    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.
  • deicistdeicist Manchester, UK
    edited August 2006
    Well I've gone from the problems at the start of this thread to programming loops and formatting data output in a day or so. It seems fairly close to C++ from a syntax point of view. The thing about programming languages is once you understand what they do and learn a couple of them it's incredibly easy to pick up others as you go along.
  • airbornflghtairbornflght Houston, TX Icrontian
    edited August 2006
    deicist wrote:
    Well I've gone from the problems at the start of this thread to programming loops and formatting data output in a day or so. It seems fairly close to C++ from a syntax point of view. The thing about programming languages is once you understand what they do and learn a couple of them it's incredibly easy to pick up others as you go along.


    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.
Sign In or Register to comment.