Shoutbox

PHP/MySQL help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP/MySQL help (/showthread.php?tid=52814)

PHP/MySQL help by lordy on 11-10-2005 at 12:04 PM

I'm creating a MySQL database for my dad so he can catalogue his huge collection of DVD's and CD's etc. I'm doing it cause i want to learn PHP and mySQL and stuff, so i figured I may as well do something useful with it while im going. It's being accessed using PHP via our webrowser over our home network. atm i have it so the page displays all entries in the database.

My questions are: How do you make a webpage in which you can easily add a new entry into the database (ie. add a new DVD/CD), using a form, so its easier for my dad to add new dvd's.

and how do i make a search form so my dad can search for a particular DVD within the database?

Im not sure if i've given enough information here... If you need something else then let me know. and dont be worried about talking n00bish to me cos im a n00b at this sort of stuff :P (hence why i am trying to learn)


RE: PHP/MySQL help by Dempsey on 11-10-2005 at 12:09 PM

well to add stuff you need a form which will send the results to a php page which will then process the form data and add it into the DB.

I suggest following a tutorial like:

WebMonkey


RE: PHP/MySQL help by lordy on 11-10-2005 at 12:34 PM

aaah that is excellent for adding stuff :D thanks


RE: PHP/MySQL help by Dempsey on 11-10-2005 at 12:37 PM

no problem :)

And about doing the search, you will be doing something similar, except instead of using INSERT, you will use SELECT, something like:

code:
SELECT dvdName, dvdYear (etc etc) FROM TableName WHERE dvdName LIKE `%searchTerms%` ORDER BY dvdName ASC

Where dvdName, dvdYear (etc, etc) is the field list you want to return, TableName is the name of the table you are searching in and searchTerms is from the textbox you search in.
RE: PHP/MySQL help by -dt- on 11-10-2005 at 01:00 PM

also remember when using a unknown varible (from a form or something) in a sql query you should escape it with

code:
mysql_real_escape_string();


RE: PHP/MySQL help by lordy on 11-11-2005 at 12:27 PM

well, so far i have this:

code:
<html>

<body>

<?php
$db = mysql_connect("127.0.0.1", "root");
mysql_select_db("movie_library",$db);
$_POST = array_map("mysql_escape_string",$_POST);


if (isset($_POST['dvd_title']))
{
//Form has been submitted
$dvd_title = $_POST['dvd_title'];
$dvd_director = $_POST['dvd_director'];
$dvd_classification = $_POST['dvd_classification'];
$dvd_actors = $_POST['dvd_actors'];
$dvd_genre = $_POST['dvd_genre'];
$dvd_year = $_POST['dvd_year'];
$dvd_length = $_POST['dvd_length'];
//insert to db
$sql = "INSERT into movies (dvd_title,dvd_director,dvd_classification,dvd_actors,dvd_genre,dvd_year,dvd_length) VALUES ('$dvd_title','$dvd_director','$dvd_classification','$dvd_actors','$dvd_genre','$dvd_year','$dvd_length')";
$result = mysql_query($db,$sql);
}
else{

?>


<form method="post" action="?">
DVD Title:<input type="Text" name="dvd_title"><br />
DVD Director:<input type="Text" name="dvd_director"><br />
DVD Classification:<input type="Text" name="dvd_classification"><br />
DVD Actors:<input type="Text" name="dvd_actors"><br />
DVD Genre:<input type="Text" name="dvd_genre"><br />
DVD Year:<input type="Text" name="dvd_year"><br />
DVD Length:<input type="Text" name="dvd_length"><br />
<input type="Submit" name="submit" value="Sumbit">

<?php

}

mysql_close($db);
?>




</body>

</html>

when i enter the data, the page goes blank (which im pretty sure its meant to), but the data isnt entered into the MySQL database.
I've asked -dt- on MSN and he couldn't find an error, so its probably someone hugely obvious, or something frustratingly small.
RE: PHP/MySQL help by Dempsey on 11-11-2005 at 12:45 PM

after

code:
$result = mysql_query etc
add
code:
if (!$result){die('Invalid query: ' . mysql_error() . "<br>SQL: " . $sql);}

RE: PHP/MySQL help by lordy on 11-11-2005 at 12:54 PM

i just chucked in random characters and it came up with this:

Invalid query:
SQL: INSERT into movies (dvd_title,dvd_director,dvd_classification,dvd_actors,dvd_genre,dvd_year,dvd_length) VALUES ('fjidjjfids','jfiodjfdos','kf','jifojodf jgf9idf','jdioas','1425','162')


RE: PHP/MySQL help by WDZ on 11-11-2005 at 02:05 PM

http://php.net/mysql_query

resource mysql_query ( string query [, resource link_identifier] )

You have your arguments in the wrong order...

mysql_query($db,$sql)


RE: PHP/MySQL help by lordy on 11-11-2005 at 10:03 PM

OMGZORS it works :D

THANK YOU WDZ :D :D

now the next thing is to make a search form but that doesnt matter right now

thank you so much :D


RE: PHP/MySQL help by lordy on 11-18-2005 at 03:41 AM

*sigh* can someone explain to me why this wont work?

i copied this out of a magazine (the tutorial im following) and ive already fixed a few errors i found in it, but for some reason, when i load the page, nothing happens, i just get a blank screen, so can someone look at this file and see if they can find any errors in it? thanks.

Removed file, see later post


RE: PHP/MySQL help by WDZ on 11-18-2005 at 04:50 AM

quote:
Originally posted by lordy16
edit: fixed a few REALLY stupid errors but still doesnt work
There are still at least 3 stupid errors... keep working... :p

I assume you didn't type it very carefully when copying from the magazine? ^o)
RE: PHP/MySQL help by lordy on 11-18-2005 at 05:07 AM

i checked it afterwards, the magazine wasa bit dodgy with thier quotation marks and stuff, can you point out what the errors are? :P

i re-uploaded the file, with a few corrections


RE: PHP/MySQL help by WDZ on 11-18-2005 at 05:14 AM

Line 23
VALUES ("
VALUES (''
(Replace double quote with 2 single quotes)

Line 23 Again
$_GET["dvd_actors"]].
$_GET["dvd_actors"].
(Remove extra bracket)

Line 29
$_GET['dvd_id']";
$_GET['dvd_id'];
(Remove extra quote)

Line 115
print(<tr>
print("<tr>
(Quote was missing)


RE: PHP/MySQL help by lordy on 11-18-2005 at 05:21 AM

quote:
Originally posted by WDZ
Line 29
$_GET['dvd_id']";
$_GET['dvd_id'];
(Remove extra quote)


but that has to be there? ive fixed everthing else and still no deal
RE: PHP/MySQL help by WDZ on 11-18-2005 at 05:28 AM

No, it doesn't have to be there. This is one string...

"SELECT * FROM movies WHERE dvd_id = "

And $_GET['dvd_id'] is simply concatenated on to the end of that.


RE: PHP/MySQL help by lordy on 11-18-2005 at 05:32 AM

have a look at this one, it still doesnt work


RE: PHP/MySQL help by WDZ on 11-18-2005 at 05:47 AM

You didn't remove the extra bracket.

code:
"','".$_GET["dvd_actors"]]."','"
See the double brackets ]] there? Remove one. That should make the script free of all syntax errors.
RE: PHP/MySQL help by lordy on 11-18-2005 at 05:50 AM

its not in there in the page on my server and it still doesnt do anything, it just pops up with a blank page., i'm stumped


RE: PHP/MySQL help by WDZ on 11-18-2005 at 05:53 AM

Well, that's strange that it's completely blank. If you view the source of the page, is that blank? Do other PHP scripts work on your server? Do you have the MySQL configuration correct?


RE: PHP/MySQL help by lordy on 11-18-2005 at 05:56 AM

other PHP pages work fine, other pages display and insert data into it fine, the source just has <html><head></head><body></body></html> in it

k now im creeped out cos it works
lets check everything else

edit: works fine :) thanks for your help DZ