Novahq.net Forum

Novahq.net Forum (https://novahq.net/forum/index.php)
-   Web design and Programming (https://novahq.net/forum/forumdisplay.php?f=32)
-   -   Sql (https://novahq.net/forum/showthread.php?t=26098)

atholon 05-27-2005 08:14 PM

Sql
 
Hey, what's wrong with this? It won't input the values!

PHP Code:

<?
include("./config.php");

mysql_query("INSERT INTO tutorials VALUES ('1', 'test1', 'test2', 'test3') or die ("<br 

/><br />Mysql Error: ".mysql_error());
?>

Also can someone tell me how I can choose a specific column and row that I want to select. Thanks!

atholon 05-27-2005 08:17 PM

Ha! Ye Beggers I found out how to select certain columns I hope it is kinda the same as the rows......

PHP Code:

INSERT INTO `tutorials` (`sid`, `title`, `body`) VALUES ('ghgfh''fghgfhfg''fhg'); 


atholon 05-27-2005 08:20 PM

NM it still is not working.

This is what I am using:
PHP Code:

<?
include("./config.php");

mysql_query("INSERT INTO `tutorials` (`title`, `body`) VALUES ('fghgfhfg', 'fhg')");
?>


IcIshoot 05-27-2005 10:23 PM

is any error message being displayed?

IcI

atholon 05-27-2005 11:09 PM

Negative.

I even put this on the end:
PHP Code:

 or die ("<br /><br />Mysql Error: ".mysql_error()); 


IcIshoot 05-28-2005 12:09 AM

how many fields are there in the table?

You may want to read through this and see if any thing would apply to help out: http://dev.mysql.com/doc/mysql/en/insert.html

Also, if you want to select specific columns from do


SELECT field1, field2 FROM table_name


To limit it to a specific row (such as a particulare user):
//Assume $userID = 100


SELECT username, userpassword FROM user_table WHERE user_ID ='$userID'

That will provide the username and password for the user who's id is 100

IcI

JonM 05-28-2005 12:25 AM

hmm i run into this some times...copy what PHP MY ADMIN does when your doing it, and put in some of those post variables or whatever :)

atholon 05-28-2005 08:20 AM

It is workin' fine now, I was told I need to use replace with " and '. Is there another character that I need to replace? I should make an array or function out of it :p

atholon 05-28-2005 08:23 AM

BTw that is right. The first variable is SID so I can use that as refrence I guess...time to get the exporting part done. Then I can start adding html and stuff to the fields...this tutorial script is going to rock.

IcIshoot 05-28-2005 09:30 AM

It doesn't have to be the first variable. You can use any field.

And you can even be more selective using more WHERE clauses, using AND or OR:

Select * from user_table WHERE is_admin = '1' AND WHERE password = '$password'


IcI

atholon 05-28-2005 09:31 AM

Yeah but if password has ' in it then it would be password = ' ' ' which would screw it up. That is why I need to use the replace command. I guess you can do the same thing with html stuff if you want to disable html.

Scott 05-28-2005 11:26 AM

you should only use WHERE once, AND WHERE could confuse with bigger querys.. like

SELECT * FROM tbl_name WHERE user='user' AND password='password' AND (email='email' OR phone='phone')");

using a bracket will group it together, so either one has to be true, hence the OR.

atholon 05-28-2005 11:42 AM

Cool :)

Scott 05-28-2005 11:57 AM

and for you user=' ' ' problem..

INSERT INTO tbl_name VALUES ('".addslashes($data)."','".addslashes($data2)." ')

Scott 05-28-2005 12:00 PM

if you get sick of typing addslashes all the time.. make a function...

function as($key) {

return addslashes($key);

}

then use as($data)




.. I do this for stripslashes and htmlspecialchars.. so comminly used but there so long to type out..

I use

function slash($key) {

$key=stripslashes($key);

return stripslashes($key);

}



function html($key) {
$key=htmlspecialchars($key);

return slash($key); //for output

}

Scott 05-28-2005 12:03 PM

you can save alot of time by writing functions ath.. like or mysql_query() or die ("theres an error yada yada".mysql_error());

function my_error() {
$key="theres an error yada yada".mysql_error();
return $key;
}

now it's just mysql_query() or die(my_error());


I write alot of functions just to shorten the name of a long function.. like number_format() would be nf() etc etc.

atholon 05-28-2005 12:39 PM

:) I am looking at SS site trying to use the select query and it aint working.
PHP Code:

<?
include("./config.php");
$nmi=mysql_fetch_array(mysql_query("SELECT * FROM tutorials WHERE id='1'"));
$title=htmlspecialchars(stripslashes($nmi["title"]));
$body=htmlspecialchars(stripslashes($nmi["body"]));
Echo("<b>Title:</b><br />\n");
Echo("$title<br />\n");
Echo("<b>Body:</b><br />\n");
Echo("$body<br />\n");
?>


atholon 05-28-2005 12:45 PM

I get this:

Quote:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\easyphp\www\ath\test2.php on line 3
Title:

Body:

Scott 05-28-2005 02:25 PM

well theres nothing wrong there so it's either the name of your table or id..

atholon 05-28-2005 03:20 PM

I'll take a screen of it from phpmyadmin

Scott 05-28-2005 04:22 PM

show us config.php also..

atholon 05-28-2005 04:48 PM

Roger, :D

Here is config:
PHP Code:

<?
/*Mysql Values*/

$global[host] = "localhost";  // Mysql host, Most of the time this is localhost
$global[user] = "root"; // Your mysql user name
$global[pass] = ""; // Your mysql password
$global[db] = "test"; // What database do you want to use?
$global[pre] = "simple"; // What do you want the table pre to be? Useful for installing multiple scripts on one db.


$global[connect]=mysql_connect("$global[host]", "$global[user]", "$global[pass]");
mysql_select_db("$global[db]", $global[connect]);
?>

Install:
PHP Code:

<?
include("./config.php");
mysql_query("CREATE TABLE tutorials (
  sid int(11) NOT NULL auto_increment,
  title varchar(255) default NULL,
  body varchar(255) default NULL,
  PRIMARY KEY  (sid)
) TYPE=MyISAM");
?>

http://nerdhq.net/pics/screenofsql.gif

Scott 05-28-2005 05:00 PM

$nmi=mysql_fetch_array(mysql_query("SELECT * FROM tutorials WHERE sid='1'"));

use sid not id :)

atholon 05-28-2005 05:13 PM

Tried that.

I swear I did.

OMG that was all it is.

Any good ideas on how to make it in a while loop and have it terminate when it gets to then end of the SID's?

Scott 05-28-2005 06:42 PM

yes,
$query=mysql_query("SELECT * FROM tutorials");

while($result=mysql_fetch_array($query)) {
//echo your data here, $result[body], $result[title] etc.
}
mysql_free_result($query);

it will automatically stop when it reaches the last sid :)

JonM 05-28-2005 07:46 PM

those lil errors always get me ;)

atholon 05-28-2005 08:21 PM

Yeah, I am just too stupid to figure them out :)

SO can I do the same thing you put on there with Stripslashes and HtmlChars?

Scott 05-28-2005 09:23 PM

yes

atholon 05-28-2005 10:05 PM

cool I think I have most of this figured out. I just need to get the script to work again ;)

I'll show yah when I get it done :)

atholon 05-28-2005 11:23 PM

Thanks for the help you guys.

I am close to having it done :)

atholon 06-02-2005 07:33 PM

Hey, one more question.

When I input all this text into sql it does make new lines where you press enter is there a way to do that?

atholon 06-02-2005 07:36 PM

Nevermind...panther's SSS saved me again.

JonM 06-02-2005 10:51 PM

how? i wanna now! :p

atholon 06-02-2005 10:53 PM

It is a function...

nl2br()

JonM 06-02-2005 10:54 PM

so like nl2br($_POST['hi']); ?

atholon 06-03-2005 01:18 AM

yeah :D

QuicK Silv3r 06-03-2005 02:03 AM

its kinda easy once u look at it and use it :D.

atholon 06-03-2005 11:11 AM

Yes,

I have another question. If you include the config file that connect you to mysql...... then can't someone else use a script to get in and hack your database if they know where the config file is?

DevilDog#1 06-03-2005 11:18 AM

I think there's a way to hide the location. It shows up as #@&*#&@#*& or something similar. ;)

atholon 06-03-2005 11:20 AM

or you can just rename it to something weird.


All times are GMT -5. The time now is 11:53 AM.

Powered by vBulletin®