Jump to content

oo PHP Form to mysql help


belthazor

Recommended Posts

Hello Guys,

 

I am new to the forum and i would be greatfull if any one can help me with this script.

 

The Script is supposed to fill that info in to mysql.

The problem is when i fil the form and i click Submit then it show me my home page it shuld show

localhost/index.php?page=addID

but it bring me back to

localhost/index.php?page=home

 

 

Index.php

<?php
include_once 'Login.php';
include_once 'admin_add.class.php';
include_once ('content_home.class.php');
include_once ('html_page.class.php');

// Create new HtmlPage to display content in browser
// Declarartion, set properties for HtmlPage object
$htmlpage = new HtmlPage();
$htmlpage->setMetaDescription("Webpage for First database OO in PHP");
$htmlpage->setMetaKeywords("php, object orientend, database");
$htmlpage->setCssFile("css/style.css");
$htmlpage->setFavicon("images/favicon.ico");

// Set content to the body of the HtmlPage object
$htmlpage->setContentPart(new ContentHome());
$htmlpage->setTitle("Homepage - Database OO PHP");
$htmlpage->setHeaderPart("Home Page");
$htmlpage->setFooterPart("Copyright 2011 by The Movies");
$htmlpage->setMenuPart("<ul>
<li><a href='index.php?page=home' class='mainlevel'>Home</a><br /></li>
<li><a href='index.php?page=addID' class='mainlevel'>Add Item</a><br /></li>
</ul>");

// Process page specifications
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if ($_GET['page'] == 'addID')
{
$htmlpage->setContentPart(new ContentAddItem());
$htmlpage->setTitle("Add item - Database OO PHP");
$htmlpage->setHeaderPart("Add Page");
}
}

 

 

Admin Add Items

admin_add.class.php

<?php
include_once ('bodypart.class.php');
include_once 'sqlhandler.class.php';
class ContentAddItem extends BodyPart
{
public function render()
{
// html variabelen ophalen
$sTitel = $_POST['titel'];
$sPrijs = $_POST['prijs'];
$sKorte_text = $_POST['korte_text'];
$sLange_text = $_POST['lange_text'];
$sBesteld = $_POST['besteld'];
$sFoto = $_POST['foto'];
$sSpecial_offer = $_POST['special_offer'];
$sType = $_POST['type'];

require "cgi-bin/connector.php";
// sql insert die je in de database gaat doen
$sql =("INSERT INTO product (id, titel, prijs, korte_text, lange_text, besteld, foto, special_offer, type) VALUES ('','".$sTitel."', '".$sPrijs."', '".$sKorte_text."', '".$sLange_text."', '".$sBesteld."', '".$sFoto."', '".$sSpecial_offer."', '".$sType."')");
//uitvoeren van de query :
$adddata = $sqlConnection->executeQuery($sql);
$result .= "<form action=\"index.php?page=addID\" method=\"POST\">
Titel:<br /> <input type=\"text\" name=\"titel\"><br /><br />
Prijs:<br /> <input type=\"text\" name=\"prijs\"><br /><br />
Korte_text:<br /> <input type=\"text\" name=\"korte_text\"><br /><br />
Lange_text:<br /> <input type=\"text\" name=\"lange_text\"><br /><br />
Besteld:<br /> <input type=\"text\" name=\"besteld\"><br /><br />
Foto:<br /> <input type=\"text\" name=\"foto\"><br /><br />
Special_offer:<br /> <input type=\"text\" name=\"special_offer\"><br /><br />
Type:<br /><br />
<input id=\"movie\" type=\"radio\" name=\"Type\" value=\"movie\" checked /><label for=\"movie\">Movie</label><br />
<input id=\"audio\" type=\"radio\" name=\"Type\" value=\"audio\" /><label for=\"audio\">Audio</label><br />
<input id=\"book\" type=\"radio\" name=\"Type\" value=\"book\" checked /><label for=\"book\">Book</label><br />
<input type =\"submit\" value=\"verzenden\"> 
</form>";
$result .= "<br>";
$result .= "<br>";
$result .= "<br>";
return $result;
}
}
?>

 

Thanks in advanced

 

Link to comment
https://forums.phpfreaks.com/topic/234271-oo-php-form-to-mysql-help/
Share on other sites

the get method on the index is just to show the index.php?page=addID

 

and the post method is to post the form info in to mysql so i need to add both?

 

get and post?

 

sorry but i am new to php this is a school project and i spended like 3 days on this and still i cant make it work

Oke,

 

I want to make a page where i can add items in to mysql with a form.

 

but i use render function and it makes the script alitle harder normally with out the render it works perfect but with it, it dont work.

 

so i made a index.php

<?php
//include_once ('content_add.class.php');
include_once 'admin_add.class.php';
include_once 'product_info.class.php';
include_once ('content_home.class.php');
include_once ('content_products.class.php');
include_once ('content_cart.class.php');
include_once ('content_contact.class.php');
include_once ('html_page.class.php');

// Create new HtmlPage to display content in browser
// Declarartion, set properties for HtmlPage object
$htmlpage = new HtmlPage();
$htmlpage->setMetaDescription("Webpage for First database OO in PHP");
$htmlpage->setMetaKeywords("php, object orientend, database");
$htmlpage->setCssFile("css/style.css");
$htmlpage->setFavicon("images/favicon.ico");

$htmlpage->setScriptFile("");

// Set content to the body of the HtmlPage object
$htmlpage->setContentPart(new ContentHome());
$htmlpage->setTitle("Homepage - Database OO PHP");
$htmlpage->setHeaderPart("Home Page");
$htmlpage->setFooterPart("Copyright 2011 by The Movies");
$htmlpage->setMenuPart("<ul>
<li><a href='index.php?page=home' >Home</a><br /></li>
<li><a href='index.php?page=producten' >Producten</a><br /></li>
<li><a href='index.php?page=cart' >Winkel Mandje</a><br /></li>
<li><a href='index.php?page=login' >Login</a><br /></li>
<li><a href='index.php?page=deleteID' >Delete Item</a><br /></li>
<li><a href='index.php?page=addID' >Add Item</a><br /></li>
<li><a href='index.php?page=contact' >Contact</a><br /></li>
</ul>");

// Process page specifications
if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
//Create contentPage for link site
if($_GET['page'] == "producten")
{
	// Set ContentPart for contentperson
	$htmlpage->setContentPart(new ContentItems());
	$htmlpage->setTitle("Personpage - Database OO PHP");
	$htmlpage->setHeaderPart("Person Page");
	$htmlpage->setScriptFile("js/sorteble.js");
}
if($_GET['page'] == "cart")
{
	// Set ContentPart for contentperson
	$htmlpage->setContentPart(new ContentCart());
	$htmlpage->setTitle("Winkel Mandje - Database OO PHP");
	$htmlpage->setHeaderPart("Winkel Mandje");
		if($_GET['action'] == "add")
		{
			// Set ContentPart for contentperson
			$htmlpage->setContentPart(new ContentCart());
			$htmlpage->setTitle("Winkel Mandje - Database OO PHP");
		$htmlpage->setHeaderPart("Winkel Mandje");
		}
		if($_GET['action'] == "remove")
		{
			// Set ContentPart for contentperson
			$htmlpage->setContentPart(new ContentCart());
			$htmlpage->setTitle("Winkel Mandje - Database OO PHP");
		$htmlpage->setHeaderPart("Winkel Mandje");
		}
		if($_GET['action'] == "empty")
		{
			// Set ContentPart for contentperson
			$htmlpage->setContentPart(new ContentCart());
			$htmlpage->setTitle("Winkel Mandje - Database OO PHP");
		$htmlpage->setHeaderPart("Winkel Mandje");
		}
} 
if($_GET['page'] == "id")
{
	// Set ContentPart for contentperson
	$htmlpage->setContentPart(new ContentDeleteItem());
	$htmlpage->setTitle("Contactpage - Database OO PHP");
	$htmlpage->setHeaderPart("Contact Page");
}
 if ($_GET['page'] == 'addID')
{
$htmlpage->setContentPart(new ContentAddItem());
$htmlpage->setTitle("Add item - Database OO PHP");
$htmlpage->setHeaderPart("Add Page");
}
}

// Send and show HtmlPage object to the browser
$htmlpage->render();
?>

 

So when i to localhost:10088/shop

it auto lead me to my home and the link becomes like this

localhost:10088/shop/index.php?page=home

 

now i want to add another content page that is called addID

so when i to go to that page i shuld make the link like this

localhost:10088/shop/index.php?page=addID

and it works fine.

 

Now comes the crap part

the content of page addID is this admin_add.class.php

<?php
include_once ('bodypart.class.php');
include_once 'sqlhandler.class.php';
class ContentAddItem extends BodyPart
{
public function render()
{
// html variabelen ophalen
$sTitel = $_POST['titel'];
$sPrijs = $_POST['prijs'];
$sKorte_text = $_POST['korte_text'];
$sLange_text = $_POST['lange_text'];
$sBesteld = $_POST['besteld'];
$sFoto = $_POST['foto'];
$sSpecial_offer = $_POST['special_offer'];
$sType = $_POST['type'];

require "cgi-bin/connector.php";
// sql insert die je in de database gaat doen
$sql =("INSERT INTO product (id, titel, prijs, korte_text, lange_text, besteld, foto, special_offer, type) VALUES ('','".$sTitel."', '".$sPrijs."', '".$sKorte_text."', '".$sLange_text."', '".$sBesteld."', '".$sFoto."', '".$sSpecial_offer."', '".$sType."')");
//uitvoeren van de query :
$adddata = $sqlConnection->executeQuery($sql);
$result .= "<form action=\"$_SERVER[php_SELF]\" method=\"POST\">
Titel:<br /> <input type=\"text\" name=\"titel\"><br /><br />
Prijs:<br /> <input type=\"text\" name=\"prijs\"><br /><br />
Korte_text:<br /> <input type=\"text\" name=\"korte_text\"><br /><br />
Lange_text:<br /> <input type=\"text\" name=\"lange_text\"><br /><br />
Besteld:<br /> <input type=\"text\" name=\"besteld\"><br /><br />
Foto:<br /> <input type=\"text\" name=\"foto\"><br /><br />
Special_offer:<br /> <input type=\"text\" name=\"special_offer\"><br /><br />
Type:<br /><br />
<input id=\"movie\" type=\"radio\" name=\"Type\" value=\"movie\" checked /><label for=\"movie\">Movie</label><br />
<input id=\"audio\" type=\"radio\" name=\"Type\" value=\"audio\" /><label for=\"audio\">Audio</label><br />
<input id=\"book\" type=\"radio\" name=\"Type\" value=\"book\" checked /><label for=\"book\">Book</label><br />
<input type =\"hidden\" name\"page\" value=\"addID\">
<input type =\"submit\" value=\"verzenden\"> 
</form>";
$result .= "<br>";
$result .= "<br>";
$result .= "<br>";
return $result;
}
}
?>

 

So when i visit localhost:10088/shop/index.php?page=addID it shows me the form see attach picture

and when i fill in the form with my info and press Submit it brings me back to

localhost:10088/shop/index.php and the info in the form are not stored in mysql.

 

When i dont use my index.php?page=blabal function it works perfect see down.

 

Test1.php the form

<html>
<body>
<form action="test2.php" method="post">
Titel:<br /> <input type="text" name="titel"><br /><br />
Prijs:<br /> <input type="text" name="prijs"><br /><br />
Korte_text:<br /> <input type="text" name="korte_text"><br /><br />
Lange_text:<br /> <input type="text" name="lange_text"><br /><br />
Besteld:<br /> <input type="text" name="besteld"><br /><br />
Foto:<br /> <input type="text" name="foto"><br /><br />
Special_offer:<br /> <input type="text" name="special_offer"><br /><br />
<input type ="submit" value="verzenden"> 
</form>
</body>
</html>

 

Test2.php the script

<?php
// html variabelen ophalen
$sTitel = $_POST['titel'];
$sPrijs = $_POST['prijs'];
$sKorte_text = $_POST['korte_text'];
$sLange_text = $_POST['lange_text'];
$sBesteld = $_POST['besteld'];
$sFoto = $_POST['foto'];
$sSpecial_offer = $_POST['special_offer'];
$sType = $_POST['type'];

mysql_connect("localhost", "root", "") or die(mysql_error());
$checkDB = "Connected to MySQL<br />";
mysql_select_db("shop") or die(mysql_error());
$checkDB += "Connected to Database<br>";
// sql insert die je in de database gaat doen
$sql =("INSERT INTO product (titel, prijs, korte_text, lange_text, besteld, foto, special_offer) VALUES ('".$sTitel."', '".$sPrijs."', '".$sKorte_text."', '".$sLange_text."', '".$sBesteld."', '".$sFoto."', '".$sSpecial_offer."')");
//uitvoeren van de query :
$data = mysql_query($sql) or die(mysql_error());
echo "Worked";
?>

 

 

[attachment deleted by admin]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.