Create a Php file with code - php

What I'm trying to do is to make an installation file where the user enters the database, username, password, and host as a first step in a php system installation.
Also I want to create a page where user give his model page name and variable names ( $name,$email ) then automatically create a file with php code like
class Admin extends MyBase
{
private $name,$email;
public function setName($v)
{
$this->name = $v;
}
public function setEmail($v)
{
$this->email = $v;
}
public function getName()
{
return $this->name;
}
public function getEmail()
{
return $this->email;
}
public function Insert()
{
$sql = "insert into
admin(name,email)
values('".$this->ms($this->name)."','".$this->ms($this->email)."')";
return $this->MyCommand($sql);
}
}
with code format.

Finally I can do a example
<?php
if (isset($_POST["Submit"])) {
$string = '<?php
$dbhost = "'. $_POST["dbhost"]. '";
$dbuname = "'. $_POST["dbuname"]. '";
$dbpass = "'. $_POST["dbpass"]. '";
$dbname = "'. $_POST["dbname"]. '";
$prefix = "'. $_POST["prefix"]. '";
$user_prefix = "'. $_POST["user_prefix"]. '";
$dbtype = "'. $_POST["dbtype"]. '";
?>';
$fp = fopen("config.php", "w");
fwrite($fp, $string);
fclose($fp);
}
?>
<form action="" method="post" name="install" id="install">
<p>
<input name="dbhost" type="text" id="dbhost" value="">
DB Host
</p>
<p>
<input name="dbuname" type="text" id="dbuname">
DB Username
</p>
<p>
<input name="dbpass" type="password" id="dbpass">
DB Pass </p>
<p>
<input name="dbname" type="text" id="dbname">
DB Name </p>
<p>
<input name="prefix" type="text" id="prefix">
DB Prefix</p>
<p>
<input name="user_prefix" type="text" id="user_prefix">
Userprefix</p>
<p>
<input name="dbtype" type="text" id="dbtype">
DB Type </p>
<p>
<input type="submit" name="Submit" value="Install">
</p>
</form>

Related

Codeigniter: Controller issues with Closure table [duplicate]

I'm trying to add a new descendant, but having difficulties achieving it, it displays some error, Would be grateful if you could take time to review what I've done thus far.
Here's
Controller
public function index() {
$this->load->view('closure_view');
}
public function add() {
$add_new = array(
'ancestor' => $this->input->post('ancestor'),
'descendant' => $this->input->post('descendant'),
'lvl' => $this->input->post('lvl'),
'id' => $this->input->post('id')
);
$cust_id = $this->closure->add($add_new);
redirect('http://localhost/kgmerchant/index.php/welcome');
}
Model
public $table;
public $closure_table = 'closures';
public function __construct($table_name = NULL, $closure_table = NULL){
parent::__construct();
$this->table = $table_name;
if ($closure_table !== NULL) {
$this->closure_table = $closure_table;
}
}
public function add($node_id, $target_id = 0) {
$sql = 'SELECT ancestor, '.$node_id.', lvl+1
FROM '.$this->closure_table.'
WHERE descendant = '.$target_id.'
UNION
SELECT '.$node_id.','.$node_id.',0';
$query = 'INSERT INTO '.$this->closure_table.' (ancestor, descendant, lvl) ('.$sql.')';
$result = $this->db->query($query);
return $result;
}
View
<form name="home" method="post" action="<?php echo base_url().'index.php/home/add' ?>" >
<input placeholder="ancestor" type="text" name="ancestor" required/>
<input placeholder="descendant" type="text" name="descendant" required/>
<input placeholder="lvl" type="text" name="lvl" required />
<input placeholder="id" type="hidden" name="id" value="" />
<input type="submit" value="Okay" />
</form>
Thanks.
Error
Message: Array to string conversion
Your model can't be called "Closure" because that's a reserved name in PHP. Change the name to something else and it should work.

codeigniter: closure table - add descendant

I'm trying to add a new descendant, but having difficulties achieving it, it displays some error, Would be grateful if you could take time to review what I've done thus far.
Here's
Controller
public function index() {
$this->load->view('closure_view');
}
public function add() {
$add_new = array(
'ancestor' => $this->input->post('ancestor'),
'descendant' => $this->input->post('descendant'),
'lvl' => $this->input->post('lvl'),
'id' => $this->input->post('id')
);
$cust_id = $this->closure->add($add_new);
redirect('http://localhost/kgmerchant/index.php/welcome');
}
Model
public $table;
public $closure_table = 'closures';
public function __construct($table_name = NULL, $closure_table = NULL){
parent::__construct();
$this->table = $table_name;
if ($closure_table !== NULL) {
$this->closure_table = $closure_table;
}
}
public function add($node_id, $target_id = 0) {
$sql = 'SELECT ancestor, '.$node_id.', lvl+1
FROM '.$this->closure_table.'
WHERE descendant = '.$target_id.'
UNION
SELECT '.$node_id.','.$node_id.',0';
$query = 'INSERT INTO '.$this->closure_table.' (ancestor, descendant, lvl) ('.$sql.')';
$result = $this->db->query($query);
return $result;
}
View
<form name="home" method="post" action="<?php echo base_url().'index.php/home/add' ?>" >
<input placeholder="ancestor" type="text" name="ancestor" required/>
<input placeholder="descendant" type="text" name="descendant" required/>
<input placeholder="lvl" type="text" name="lvl" required />
<input placeholder="id" type="hidden" name="id" value="" />
<input type="submit" value="Okay" />
</form>
Thanks.
Error
Message: Array to string conversion
Your model can't be called "Closure" because that's a reserved name in PHP. Change the name to something else and it should work.

Verify custom table in magento for custom module

I have made the redirect from the question i have posted here
Now i have another form in the redirected page where i need to enter name and mobile number and if it matches in my db table helloworld it will go to one page or else to another
<p>You have successfully registered</p>
<div>
<label>Login</label>
</div>
<div>
<form action="" method="post">
<label> Username </label>
<strong>:</strong>
<input class="input-text required-entry" type="text" name="fname" maxlength="20">
<label>Mobile No</label>
<strong>:</strong>
<input class="required-entry" type="number" maxlength="10" name="mobileno">
<input type="submit" name="login" value="Login">
<input type="button" name="cancel" value="Cancel">
</form>
</div>
Can any one help me how no i can verify it with existing data in db and make this work ? Shall I start it with using index controller or is there any magento way?
Update:
this is my Indexcontroler.php after below answer update
<?php
class MyCustom_Helloworld_IndexController extends Mage_Core_Controller_Front_Action
{
/*
* this method privides default action.
*/
public function indexAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
echo $firstname = $param['fname'];
$lastname = $param['lname'];
$address = $param['address'];
$state = $param['state'];
$city = $param['city'];
$mobile = $param['mobileno'];
$model = Mage::getModel('helloworld/helloworld');
// $model->setTitle($title);
$model->setFirstname($firstname);
$model->setLastname($lastname);
$model->setAddress($address);
$model->setState($state);
$model->setCity($city);
$model->setMobileno($mobile);
$model->save();
$this->_redirect('helloworld/index/login');
// $this->_redirectReferer();
}else {
/*
* Initialization of Mage_Core_Model_Layout model
*/
$this->loadLayout();
/*
* Building page according to layout confuration
*/
$this->renderLayout();
}
}
public function loginAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function loginnAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
$username = $param['fname'];
$mobile = $param['mobileno'];
$check = Mage::getModel('helloworld/helloworld')
->AddFieldToFilter('mobileno', array('eq' => $mobile))
->AddFieldToFilter('fname', array('eq' => $username));
if(count($check)==1) {
$this->_redirectReferer();
}else {
$this->_redirect('helloworld/index/login');
}
}
}
}
you can add check like that
public function loginAction()
{
if($this->getRequest()->getParams()) {
$param = $this->getRequest()->getParams();
$username = $param['fname'];
$mobile = $param['mobile'];
$connectionresource = Mage::getSingleton('core/resource');
$readconnection = $connectionresource->getConnection('core_read');
$table = $connectionresource->getTableName('helloworld/helloworld');
$allrecord = $readconnection->select()->from(array('helloworld'=>$table))->where('helloworld.mobileno=?', $mobileno)
->where('helloworld.fname=?', $username);
$alldata =$readconnection->fetchAll($allrecord);
if(count($alldata)==1) {
$this->_redirect('home');
}else {
$this->_redirect('customer/account');
}
}

How to call class function via button click

I've got a PHP class which has 1 method for inserting data. Once I create my instance of the class, I'm not sure how to call the method. I've seen other questions about how to accomplish calling a method, but none deal with calling the method of a class instance.
My PHP is:
<?php
$servername = "localhost";
$username = "kevin";
$password = "ally";
$database = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['btnInsert']) && ($_POST['btnInsert'] == "Insert"))
{
$Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txttype'], $_POST['txtDescription']);
}
class Dog
{
public $name = "Dog Name";
public $size = 0;
public $color = "255:255:255";
public $typeName = "type Name";
public $typeDescription = "type Description";
public function Dog($name, $size, $color, $type, $description){
$this->name = $name;
$this->size = $size;
$this->color = $color;
$this->typeName = $type;
$this->typeDescription = $description;
}
public function InsertDog(){
$query = "SELECT Id from tbl_type WHERE Name = $typeName";
$result = mysqli_query($conn, "INSERT INTO tbl_type($this->typeName, $this->$typeDescription)") or die("Query fail: " . mysqli_error());
$result2 = mysqli_query($conn, "INSERT INTO tbl_Dog($this->$name, $this->$size, $this->$color, $query)") or die("Query fail: " . mysqli_error());
}
public function UpdateDog(){
}
}
?>
And my form:
<form action="index.php" method="post">
Dog Name:<br />
<input name="txtName" type="text" /><br />
<br />
Size:<br />
<input name="txtSize" type="text" /><br />
<br />
Color:<br />
<input name="txtColor" type="text" /><br />
<br />
type Name:<br />
<input name="txttype" type="text" /><br />
<br />
type Description:<br />
<input name="txtDescription" style="width: 419px; height: 125px" type="text" /><br />
<br />
<input name="btnInsert" type="submit" value="Insert" />
</form>
How can I call the InsertDog() method for the $Dog instance?
Simply use:
...
$Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txttype'], $_POST['txtDescription']);
$Dog->InsertDog();
:)
You can simply have all your codes in one file (index.php) and then you can use:
$Dog = new Dog($_POST['txtName'], $_POST['txtSize'], $_POST['txtColor'], $_POST['txttype'], $_POST['txtDescription']);
$Dog->InsertDog();
Or second solution is add ajax to your code. So link jquery:
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
edit
<input name="btnInsert" type="submit" value="Insert" />
to
<input name="btnInsert" type="submit" value="Insert" id="buttonId" />
and add:
<script>
$( document ).ready(function() {
$( "#buttonId" ).click(function() {
$.ajax({
type: "POST",
url: "index.php",
data: { txtName: "DogsName", txtSize: "DogSize", txtColor: "color", txttype: "type", txtDescription: "dscr"v}
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
});
</script>
url to your index.php must really server/index.php - so if you use localhost, it must be http://localhost/index.php
I didn't test it, but it should works

mysql post missing data from SERVER REQUEST_METHOD

I have the following code, whenever I try to insert the data, the $content is not being inserted, where might the problem be? I am using the function test input for security related issues.
<?php
// define variables and set to empty values
$title = $content = $path = $file_type ="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$title = test_input($_POST["title"]);
$content = test_input($_POST["content"]);
$path = test_input($_POST["path"]);
$file_type = test_input($_POST["file_type"]);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$con=mysqli_connect("localhost","---","---","---");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO articles (ArtID,Title,Content,Image_VideoLink_Path,file_type)
VALUES
('','$title','$content','$path',' $file_type')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "<h2 >Article Published</h2>";
echo"<a href='../mag/index.php'> View it Now </a>";
mysqli_close($con);
?>
here is the code for the form:
<form method="POST" action="artPro.php">
<fieldset>
<legend>Create New Article</legend>
<br/>
Article Title:
<input type="text" placeholder="enter title here" class="span3" name="title" required>
Image/Video Path :
<input type="text" placeholder="enter image name e.g k.jpg or k.mp4 for video" name="path" class="span3" required/>
File Type :
<input type="text" name="file_type" class="span3" required placeholder="e.g: image or video"/>
<br/>
<label>Article Content:</label>
<textarea name="content" rows="20" class="jqte-test span12" required id="txtmsg"></textarea>
<br><button type="submit" class="btn btn-primary btn-large pull-right">Publish</button>
</fieldset>
</form>
Try this query:
$sql="INSERT INTO articles
(Title,Content,Image_VideoLink_Path,file_type) VALUES
('$title','$content','$path',' $file_type')";
$content was not being inserted because you are giving Article ID. And I am sure that Article ID will your primary key and this will auto incremented. So, you did not need to mention Article id in sql query (statement).

Categories