[REQ][250 credits]switching content with mySQL

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
I want to do this:

I have one file, gadgets.php. When you go to gadgets.php?id=1, it shows a bit of HTML. gadget.php?id=2 shows another bit of HTML.

It will take the HTML from a table in the database. The table has 2 rows, gcode, that holds the code and id that holds the id.

Now gadgets.php?id=1 will show the code with the ID 1 in the database.

An example on how to do it with files:
http://forums.x10hosting.com/tutorials/49484-tutorial-php-switching-content-including-pages.html

But I want it done by SQL.

Like think of how a forum works. The style is stored in files but the post is stored in the database. I want it that way
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
OK, not too hard. The biggest problem I may have when coding it is properly inserting/reading the html in the database without screwing anything up. Also, make sure that the row, `id` is set to auto_increment :)

PHP:
<?php

// Display gcode if ?id=# exists and only contains numbers...
if (!empty($_GET['id']) && is_numeric($_GET['id'])) {
	$id = preg_replace("[^0-9]","", $_GET['id']);
	$result = mysql_query("SELECT gcode FROM table WHERE id='$id' LIMIT 1");
	$result = mysql_fetch_assoc($result);
	$old = array("&quot;", "&apos;");
	$new = array('"', "'");
	$gcode = str_replace($old, $new, $result['gcode']);
	echo $gcode;
} else {
	die("Error: Page number invalid.  Please only use numbers (no letters, etc...");
}
?>
PHP:
<?php

// Insert HTML into database :D  This is just the basics to make it work.  You can add more to this if you have the desire to
if (!empty($_POST['gcode']) {  // Just make sure that your field name is gcode ;)
	$old = array('"', "'");
	$new = array("&quot;", "&apos;");
	$gcode = str_replace($old, $new, $_POST['gcode']);
	$result = mysql_query("INSERT INTO table (gcode) VALUES('$gcode') LIMIT 1");
	if ($result) {
		echo "Created new page";
	} else {
		echo "There was an error creating your page.<br /><br />".mysql_error();
	}
} else {
	echo "
		<form action='?submit' method='post'><div>
		<textarea name='gcode'></textarea><br /><br />
		<input type='submit' value='Create Page' name='submit' />
		</div></form>
	";
}
?>
PHP:
<?php

// Update HTML already in the database :)
if (isset($_POST['update']) && !empty($_GET['id'])) {
	$id = preg_replace("[^0-9]","", $_GET['id']);
	$old = array('"', "'");
	$new = array("&quot;", "&apos;");
	$gcode = str_replace($old, $new, $_POST['gcode']);
	$result = mysql_query("UPDATE table SET gcode='$gcode' WHERE id='$id' LIMIT 1");
	if ($result) {
		echo "Page: $id updated successfully";
	} else {
		echo "There was a problem updating page: $id<br /><br />".mysql_error();
	}
		
} elseif (!empty($_GET['id'])) {
	$id = preg_replace("[^0-9]","", $_GET['id']);
	$result = mysql_query("SELECT gcode FROM table WHERE id='$id' LIMIT 1");
	$old = array("&quot;", "&apos;");
	$new = array('"', "'");
	$gcode = str_replace($old, $new, $result['gcode']);
	$result = mysql_fetch_assoc($result);
	echo "
		Page <b>$id</b><br /><br />
		<form action='?id=$id' method='post'><div>
		<textarea name='gcode'>$gcode</textarea><br /><br />
		<input type='submit' value='Update' name='update />
		</div></form>
	";

} else {
	$result = mysql_query("SELECT id, gcode FROM table");
	while ($result=mysql_fetch_assoc($result)) {
		echo "<a href='?id=".$result['id']."'>Page ".$result['id']."</a><br />\n";
	}
}
?>

If there's any errors, please let me know. To the best of my knowledge, there are no errors in this script, but I have not confirmed this.

-xP
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
ummm. I can't put HTML in the submit form...

"There was an error creating your page.

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 'LIMIT 1' at line 1"
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
I'm sorry, on that line, there shouldn't be a LIMIT 1. Remove that and it should be good. The only other possible thing that woud screw it up were quotes and apostrophes, which were both properly converted to avoid problems... ;)

-xP
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
try this
PHP:
    $result = mysql_query("INSERT INTO table (id, gcode) VALUES(NULL, '$gcode')");
and make sure that id in phpMyAdmin is an integer (int) and that it's set to Primary and auto_increment...

I'm sorry that there's problems. From now on, I will always test out my code before I post it up in the Marketplace ;)
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Well, it worked this time. But I assume it gets id 1 and I need to do gadget.php?id=1. When I do that I get a blank page
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
check phpMyAdmin and see if it went into the database properly...
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
yes it did add it. Got it! in http:// // comments out the rest of the code! because the source of the page is:

<html>
<head>
</head>
<body>

<script src="http
</body>
</html>

or, it comments it out upon adding it :/
 

xPlozion

New Member
Messages
868
Reaction score
1
Points
0
ok,

where it says
Code:
    echo $gcode;
, change that to
Code:
    echo str_replace('/', '\/', $gcode);
that should escape the slash, preventing it from thinking it's a comment.
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
ok,

where it says
Code:
    echo $gcode;
, change that to
Code:
    echo str_replace('/', '\/', $gcode);
that should escape the slash, preventing it from thinking it's a comment.

it comments out the code in the form aswell so everything added to the database is "<script src=&quot;http:".
 

edu2004eu

New Member
Messages
128
Reaction score
0
Points
0
if not working try this:

search-for:
Code:
echo $gcode;

replace-with:
Code:
echo addslashes($gcode);
 

edu2004eu

New Member
Messages
128
Reaction score
0
Points
0
Wait. On which page are you having this problem? On the display page right? Then the code can't be interpreted as a comment until that echo $gcode command... it's the only place. Could you post exactly what you have in your table?
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
Wait. On which page are you having this problem? On the display page right? Then the code can't be interpreted as a comment until that echo $gcode command... it's the only place. Could you post exactly what you have in your table?

I have <script src=&quot;http: in the table, and obviously the rest after // is missing
 

edu2004eu

New Member
Messages
128
Reaction score
0
Points
0
Then you had a problem on the insert page. Do you know how to add the content manually through phpMyAdmin? If yes then do so, if not, hang on, I am trying to solve the mystery behind the insert code :p

EDIT: OK that code needed some work, cause it gave a error, but other than that I find it very clean. Here's what I modified on it...

PHP:
<?php

// Insert HTML into database :D  This is just the basics to make it work.  You can add more to this if you have the desire to
if (!empty($_POST['gcode'])) {  // Just make sure that your field name is gcode ;)
    $old = array('"', "'");
    $new = array("&quot;", "&apos;");
    $gcode = str_replace($old, $new, $_POST['gcode']);
    $result = mysql_query("INSERT INTO `table` VALUES(NULL,'$gcode')");
    if ($result) {
        echo "Created new page";
    } else {
        echo "There was an error creating your page.<br /><br />".mysql_error();
    }
} else {
    echo "
        <form action='?submit' method='post'><div>
        <textarea name='gcode'></textarea><br /><br />
        <input type='submit' value='Create Page' name='submit' />
        </div></form>
    ";
}
?>

(I assume you only have 2 fields in your table: ID and GCODE, else this wont work...
PHP:
 
Last edited:

edu2004eu

New Member
Messages
128
Reaction score
0
Points
0
This is getting hard... lol... if I'd only be there... OK... can you click the Structure button on phpMyAdmin and make a screenshot and post it here? I know it's pretty much to do but... if you want it done, you'll have to try everything...
 

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
This is getting hard... lol... if I'd only be there... OK... can you click the Structure button on phpMyAdmin and make a screenshot and post it here? I know it's pretty much to do but... if you want it done, you'll have to try everything...

I added " $result = mysql_query("INSERT INTO table (id, gcode) VALUES(NULL, '$gcode')"); "
Again and now it created the page but I still don't get the whole code...

Untitled-5.jpg

 

edu2004eu

New Member
Messages
128
Reaction score
0
Points
0
Idk then, as a last solution I'd check if MySQL is version 5... but at me it works just fine with the same code... so the problem isn't with the code...
 
Top