php coding help |
Author: |
Message: |
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: php coding help
quote: Originally posted by k776
Sorry WDZ, I meant to say infomation/text, not actual files
Well, it shouldn't be too hard. First you might want to do some validation of the values that were submitted, though that's not as important if only trusted people can submit.
code: if(!$_POST['name']) {
die("You must enter a name!");
}
You could also check to make sure the length isn't too long and stuff like that. Next you should make sure the data is ready to be used in a query by applying addslashes()...
$name = addslashes($_POST['name']);
However, this step might not be needed if your server has magic quotes enabled, because that will automatically add slashes. Running the function again will result in doubled slashes, which is annoying.
Now to actually insert the data...
mysql_query("INSERT INTO tablename (name) VALUES ('$name')");
That's an example of inserting just one field. To insert all the fields you listed, you'd do something like this...
mysql_query("INSERT INTO tablename (catid,mplink,name,dllink,desc,rate,type,pic) VALUES ('$catid', '$mplink', '$name', '$dllink', '$desc', '$rate', '$type', '$pic')");
I don't know what all your fields are for, so I can't give a perfect example. Also, you'd have to call your mysql_connect() and mysql_select_db() before mysql_query(), of course.
Google for some tutorials... I bet there's tons of them...
|
|
09-08-2004 12:51 PM |
|
|
k776
Junior Member
Posts: 98 Reputation: 3
– / / –
Joined: Aug 2004
|
RE: php coding help
I took what you said, edited it a tiny bit.
Im using my own form, set with code: method="post" action="<? echo $PHP_SELF; ?>"
with code: <?
require ("scripts/add.php");
?>
at the top of the page. Then in scripts/add.php, I have this: code: <?
require_once ("scripts/config.php");
$errors = array();
if ($_SERVER['REQUEST_METHOD']=="POST"){
$errors = process_form($errors);
}
function process_form($errors) {
$catid = $_REQUEST['catid'] ;
$mplink = $_REQUEST['mplink'] ;
$name = $_REQUEST['name'] ;
$dllink = $_REQUEST['dllink'] ;
$desc = $_REQUEST['desc'] ;
$pic = $_REQUEST['pic'] ;
if (empty($catid)) {
$errors["No catid"]="Empty catid";
}
if (empty($mplink)) {
$errors["No mplink"]="Empty mplink";
}
if (empty($name)) {
$errors["No name"]="Empty name";
}
if (empty($dllink)) {
$errors["No dllink"]="Empty dllink";
}
if (empty($desc)) {
$errors["No desc"]="Empty desc";
}
if (empty($pic)) {
$errors["No pic"]="Empty pic";
}
if (count($errors) == 0) {
mysql_query("INSERT INTO download_items (catid,mplink,name,dllink,desc,rate,type,pic) VALUES ('$catid','$mplink','$name','$dllink','$desc','$rate','$type,'$pic')");
header( "Location: scripts/add2.htm" );
}
return $errors;
}
function print_error($error) {
echo "<b><font color=red>Error/b> $error</font><br /><br />";
}
?>
but it doens't work. No errors, it does exactly what I want it to (check for missing fields, if none process, if some show error on the form). It just doesn't insert into the database, I have no idea why. Any ideas?? (rate and type dont need to be checked for missing feilds cause they are set to "Not Available by default".
(config hold the username, password and database name.) The code in it is: code: <?
$username ="******";
$password ="******";
$database="******";
$connection = mysql_connect("localhost","$username","$password");
@mysql_select_db("$database", $connection) or die( "<br><center><h2>Unable to select database, please refresh.</h2>If problem persists, please contact me using the form and report the error you are receiving if any.</center>");
?>
As for the slashes, how can I tell if I have magic quotes enabled??
So, if you find what is wrong, can you submit 2 versions, one with and one without slahes command thing, just in case.
As for what its for, adding downloads to my site.
catid = I have 8 different catagorys so catid is somewhere between 1-8
mplink = Main page link
name = Name of the download
dllink = Download link
desc = Description of the download
rate = My rating for the download
type = The type of download it is, Freeware, Shareware etc.
pic = A picture of the download
k776
This post was edited on 09-09-2004 at 12:14 AM by k776.
|
|
09-09-2004 12:10 AM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: php coding help
Hmm... I don't see where print_error() is called. Maybe one of the fields is missing for some reason but you don't know about it because the error is never echoed.
You should do something like the following...
code: foreach($errors as $error) {
print_error($error);
}
quote: Originally posted by k776
As for the slashes, how can I tell if I have magic quotes enabled??
http://php.net/get_magic_quotes_gpc
There's some nice examples there that show how to detect and handle magic quotes.
|
|
09-09-2004 02:56 AM |
|
|
k776
Junior Member
Posts: 98 Reputation: 3
– / / –
Joined: Aug 2004
|
RE: php coding help
ok, print_error() is on the form in this code: code: <? if (array_key_exists("No catid",$errors)) print_error($errors["No catid"]); ?>
One for each part of the form I want to check. It displays it on the form for me.
Next, Thanks for the link, checking now.............. dont understand. How can I check if magic quotes is enabled?? Can you use the code in add.php to give me an example??
|
|
09-09-2004 03:48 AM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: php coding help
quote: Originally posted by k776
ok, print_error() is on the form in this code:code: <? if (array_key_exists("No catid",$errors)) print_error($errors["No catid"]); ?>
One for each part of the form I want to check. It displays it on the form for me.
Oh, OK. Well, I'm stumped. I can't spot any obvious problems in your code.
Do the errors work properly? If you leave a field blank and submit the form, you see the error? Is the inserting into the database the thing that doesn't work? If so, then the problem might be that count($errors) is returning something other than 0. Maybe you should do a foreach($errors) just for debugging, and see what's making it not 0.
quote: Next, Thanks for the link, checking now.............. dont understand. How can I check if magic quotes is enabled?? Can you use the code in add.php to give me an example??
OK... I modified the code on php.net to ADD slashes if magic quotes is disabled. It will make sure all the values in $_REQUEST are ready to be used in the MySQL query.
code: if (!get_magic_quotes_gpc()) {
function addslashes_deep($value) {
$value = is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
return $value;
}
$_REQUEST = array_map('addslashes_deep', $_REQUEST);
}
$catid = $_REQUEST['catid'];
// etc...
|
|
09-09-2004 04:23 AM |
|
|
k776
Junior Member
Posts: 98 Reputation: 3
– / / –
Joined: Aug 2004
|
RE: php coding help
ok, yes, the errors do work. If I leave it blank, it does show the error. As for the foreach($errors) part, where do I put that??
added the code you gave me, uploading now................. still doesn't work, but I'll keep it there anyway.
*Chris Boulton suggested code: mysql_query("INSERT INTO download_items (catid,mplink,name,dllink,desc,rate,type,pic) VALUES ('$catid','$mplink','$name','$dllink','$desc','$rate','$type,'$pic')") or die(mysql_error());
Im gonna try that now.*
A-ha, now we have somethign to work on....... *Thank Chris* quote: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc,rate,type,pic) VALUES ('2','test','test','test','test','',
This post was edited on 09-09-2004 at 05:11 AM by k776.
|
|
09-09-2004 04:51 AM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: php coding help
Ah, OK...
code: mysql_query("INSERT INTO download_items (`catid`,`mplink`,`name`,`dllink`,`desc`,`rate`,`type`,`pic`) VALUES ('$catid','$mplink','$name','$dllink','$desc','$rate','$type,'$pic')") or die(mysql_error());
Try that... MySQL doesn't like the "desc" because it's a reserved word.
|
|
09-09-2004 05:20 AM |
|
|
k776
Junior Member
Posts: 98 Reputation: 3
– / / –
Joined: Aug 2004
|
RE: php coding help
ok, I used your suggestion and Chris Boultons ('$type, to '$type', (extra ')) and now it works. Thanks WDZ.
4 new questions: code: <option value="0" <? if ($_REQUEST['rate'] == "0") echo "checked"; ?>>0 - Not Yet Rated</option>
That code is suppose to remember the option if I forget a feild and it resets the page but it doens't. I got text feilds to work, but not select feilds. Any suggestions.
I want a password code so only I can enter the "Add download" page. Please can someone lead me to the right site or give me a code here that can do just that??
I want it to echo each part of the script, eg:
catid added.....
mplink added.....
name added.....
etc etc
And finally, I want it to redirect me back to the add download page.
Please help.
This post was edited on 09-09-2004 at 05:44 AM by k776.
|
|
09-09-2004 05:42 AM |
|
|
WDZ
Former Admin
Posts: 7106 Reputation: 107
– / /
Joined: Mar 2002
|
RE: php coding help
quote: Originally posted by k776
('$type, to '$type', (extra '))
Doh... how could I have missed that? My bug-spotting skills are getting rusty...
So there were 2 syntax errors in the query... glad to hear it's working now.
quote: Originally posted by k776
That code is suppose to remember the option if I forget a feild and it resets the page but it doens't. I got text feilds to work, but not select feilds. Any suggestions.
"checked" is for checkboxes and radio buttons. For <select> menus, use "selected" instead.
|
|
09-09-2004 12:38 PM |
|
|
k776
Junior Member
Posts: 98 Reputation: 3
– / / –
Joined: Aug 2004
|
RE: php coding help
Thanks WDZ. Its working now
New questions:
1. Is this how to put a [Delete] button next to name so it deletes the table I want without having to go into mySQL?? code: $sql = "DELETE FROM `download_items` WHERE id = '$id' LIMIT 1";
echo "[Delete]<br>\n";
2. I also want a [Edit] button that pulls the info from the download, onto a form in the right places, and allows me to edit it. How can that be done?? (just like this baord, when you click edit, it pulls the name of the thread description etc. If it is too hard, Ill just have to edit it in mySQL )
3. I have game and program downloads. I want to have a select box that has to be chosen first to open up the other selections. eg: Select1 and Select2.
Select one asks for Game Download or Item downloads. If items is chosen, it displays the catagories in select2, if game is chosen, differnet ones. See what I mean?? Currently, only 1 is possible, I dont want to have to make two forms. So until Select1 is chosen, can select 2 be greyed out somehow?? Next, I need the coding. Most probably an if statment, like code: if "select == item {
code to process form here into the right place (got this one)
} else {
the code to put it in another place (got this too)
}
Thanks for the help so far everyone.
This post was edited on 09-11-2004 at 03:47 AM by k776.
|
|
09-09-2004 08:46 PM |
|
|
Pages: (3):
« First
«
1
[ 2 ]
3
»
Last »
|
|
|