php mysql database not updating - confirmation

can you check my code..my database wont update....when you click the confirm user..the database is not updating..i need help..please
<?php
include('connection.php');
if(isset($_POST['id']))
{
if($_POST['status']=='confirmed')
{
$uid=$_POST['id'];
$sql_update=mysql_query("UPDATE `tmc` SET `status`='confirmed' WHERE (`mem_id`='$uid')");
}
}
$sql=mysql_query("SELECT * FROM member WHERE status='pending'");
echo "<table>";
while($result=mysql_fetch_array($sql))
{
$id=$result['mem_id'];
$username=$result['username'];
echo "<tr>
<td>
".$id."
</td>
<td>
".$username."
</td>
<td>
<a href='?id=".$id."&status='confirmed''>Confirm User</a>
</td>
</tr>";
}
?>

Try this:
....
if(isset($_GET['id']))
{
if($_GET['status']=='confirmed')
{
$uid=$_GET['id'];
$sql_update=mysql_query("UPDATE `tmc` SET `status`='confirmed' WHERE (`mem_id`='$uid')");
}
}
....

Related

Cant write data in database using codeigniter

I have a controller, model and view file. I am trying to write data to database using the form on view file. I am trying to validate the post data using a mix of Codeigniter Validation library and some methods defined by. This validation is being done in Controller. Then I am trying to pass data array to the model. In the model I am trying to read the data array and build a query to insert data in database.
The problem is that no data is being written in the database.
I dont see any visible errors in the browser. I have been stuck on this for some time now. Any help is appreciated. Thanks in advance.
Controller
function add_customer() {
$this->load->model('Customer_Model');
$data['title'] = "Add New Customer";
$this->load->view('templates/header' , $data);
$this->load->view('dashboard/add_customer' , $data);
$this->load->view('templates/footer' , $data);
if($this->input->post())
{
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|customer_email_exists');
$this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|customer_mobile_exists');
$this->form_validation->set_rules('address', 'Address', 'trim|required');
if ($this->form_validation->run()){
$customer_data = array(
'name' => $this->validation->name,
'email' => $this->validation->email,
'mobile' => $this->validation->mobile,
'address' => $this->validation->address
);
$this->Customer_Model->add_customer($customer_data);
}else{
}
}
}
public function customer_email_exists($email) {
$this->load->model('Customer_Model');
if(!$this->Customer_Model->email_exists($email)){
return true;
}else{
$this->form_validation->set_message('email_exists', 'Email already registered, try another one.');
return false;
}
}
public function customer_mobile_exists($mobile) {
$this->load->model('Customer_Model');
if(!$this->Customer_Model->mobile_exists($mobile)){
return true;
}else{
$this->form_validation->set_message('email_exists', 'Email already registered, try another one.');
return false;
}
}
}
Model
class Customer_Model extends CI_Model{
function add_customer($customer_data)
{
$data = array(
'id'=>'',
'name'=>$customer_data["name"],
'email'=>$customer_data["email"],
'mobile'=>$customer_data["mobile"],
'address'=>$customer_data["address"]
);
$this->db->insert('customer',$data);
$this->db->query($query);
}
public function email_exists($email) {
$this->db->where("email = '$email' AND email != ''");
$query = $this->db->get('customer');
if ($query->num_rows() > 0){
return true;
}else{
return false;
}
}
public function mobile_exists($mobile) {
$this->db->where('mobile',$mobile);
$query = $this->db->get('customer');
if ($query->num_rows() > 0){
return true;
}else{
return false;
}
}}
View
<section class="versir-section">
<div class="container">
<div class="row">
<div class="col-12">
<?php echo validation_errors(); ?>
<form method="post">
<table width="600" border="1" cellspacing="5" cellpadding="5">
<tr>
<td width="230">Customer Name</td>
<td width="329"><input type="text" name="name"/></td>
</tr>
<tr>
<td>Customer Email </td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td>Customer Mobile </td>
<td><input type="text" name="mobile"/></td>
</tr>
<tr>
<td>Customer Address </td>
<td><input type="text" name="address"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="save" value="Save Data"/></td>
</tr>
</table>
</form>
</div>
</div>
</div>
Hmm! I think in this code part you should get post data from input, not from validation
if ($this->form_validation->run()){
$customer_data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'address' => $this->input->post('address'),
);
$this->Customer_Model->add_customer($customer_data);
}else{
}

Php 7 error for a simple form getting Object of class mysqli could not be converted to int

The code gets error saying Object of class mysqli could not be converted to int. When i try to load the page i get this error,i checked other questions but it was not helpful
<?php
$conn = mysqli_connect("localhost", "root", "", "coinlion");
if ($conn == 0) {
echo "could not connect";
} else {
mysqli_select_db("coinlion");
echo "connected";
}
?>
<form method="post">
<style>
#newlink {
width: 600px
}
</style>
<div id="newlink">
<div>
<table border=0>
<tr>
<td> Link URL:</td>
<td><input type="text" name="linkurl" value="" required></td>
</tr>
<tr>
<td> Link Description:</td>
<td><textarea name="linkdesc" cols="50" rows="5" required></textarea>
</td>
</tr>
</table>
</div>
</div>
<p>
<br>
<input type="submit" name="submit1">
<input type="reset" name="reset1">
</p>
php code
<?php
if (isset($_POST['submit1'])) {
$linkurl = $_POST['linkurl'];
$linkdesc = $_POST['linkdesc'];
mysqli_query($conn, "insert into
test(linkurl,linkdesc)values('$_POST[linkurl]','$_POST[linkdesc]')");
echo "data inserted";
}
?>
Well the issue was with the line if($conn==0) ,This value was taken as an integer so the error occured and an Connection variable to Mysqli_select_db was missing , after fixing it , the code ran successfully

Cannot bind source type Umbraco.Web.Models.RenderModel to model type Repower.Cms.Umbraco.Models.Test

First of all I am new to Umbraco so if you see some basic mistakes don't judge me.
So I am creating currently a Login form which he goes to the database (check Username and Password) and reads the value which he returns and let's him Cannot bind source type Umbraco.Web.Models.RenderModel to model type Repower.Cms.Umbraco.Models.Test.
This Is my HTML:
#inherits Umbraco.Web.Mvc.UmbracoViewPage<Repower.Cms.Umbraco.Models.Test>
#{
Layout = "Master.cshtml";
}
<style type="text/css">
.btnStyle {
border: thin solid #000000;
line-height: normal;
width: 80px;
}
</style>
#using (Html.BeginForm("Test", "MembersProtectedPage", FormMethod.Post))
{
<div class="fontStyle">
<center>
<table style="margin-top: 100px;margin-left:150px">
<tr style="height:30px">
<td align="right">
#Html.LabelFor(m => m.User)
</td>
<td style="width:200px" align="right">
#Html.TextBoxFor(m => m.User)
</td>
<td style="width:250px;color:Red" align="left">
#Html.ValidationMessageFor(m => m.User)
</td>
</tr>
<tr style="height:30px">
<td align="right">
#Html.LabelFor(m => m.Password)
</td>
<td align="right">
#Html.PasswordFor(m => m.Password)
</td>
<td style="width:250px;color:Red" align="left">
#Html.ValidationMessageFor(m => m.Password)
</td>
</tr>
<tr style="height:30px">
<td colspan="2" align="center">
<input type="submit" value="Sign In" class="btnStyle" />
</td>
</tr>
</table>
</center>
</div>
}
This is my Model:
public class Test : RenderModel
{
public Test() : this(new UmbracoHelper(UmbracoContext.Current).TypedContent(UmbracoContext.Current.PageId)) { }
public Test(IPublishedContent content, CultureInfo culture) : base(content, culture) { }
public Test(IPublishedContent content) : base(content) { }
string connString = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
SqlConnection conn;
SqlCommand sqlcomm;
public string User { get; set; }
public string Password { get; set; }
public bool IsUserExist(string emailid, string password)
{
bool flag = false;
conn = new SqlConnection(connString);
conn.Open();
sqlcomm = new SqlCommand();
sqlcomm.Connection = conn;
sqlcomm.CommandType = System.Data.CommandType.StoredProcedure;
sqlcomm.CommandText = "dbo.uspLogin";
sqlcomm.Parameters.AddWithValue("#pLoginName", User);
sqlcomm.Parameters.AddWithValue("#pPassword", Password);
SqlParameter retval = sqlcomm.Parameters.Add("#RESULT", SqlDbType.VarChar);
retval.Direction = ParameterDirection.ReturnValue;
sqlcomm.ExecuteNonQuery(); // MISSING
string retunvalue = (string)sqlcomm.Parameters["#RESULT"].Value;
switch (retunvalue)
{
case "0":
flag = true;
break;
case "1":
flag = false;
break;
case "2":
flag = false;
break;
default:
flag = false;
break;
}
return flag;
}
}
And this is my Controller:
public class TestController : Controller
{
public ViewResult Login()
{
return View();
}
[HttpPost, ValidateInput(false)]
public ActionResult Login(Test model)
{
if (ModelState.IsValid)
{
if (model.IsUserExist(model.User, model.Password))
{
ViewBag.UserName = model.User;
FormsAuthentication.RedirectFromLoginPage(model.User, false);
}
else
{
ModelState.AddModelError("", "Username or Password Incorrect.");
}
}
return View(model);
}
}
So I am inheriting a RenderModel because previously my error was "The model item passed into the dictionary is of type Repower.Cms.Umbraco.Models.Test', but this dictionary requires a model item of type 'Umbraco.Web.Models.RenderModel'."
So I changed it (searched a lot in the Internet) and now I get this error.
Is also the rest of the code correct? The way that I access the Database and everything? I am expecting a Return value from the Database (don't know if that is correct)
Could Someone please help me?
I need to get this done today.
Thanks in Advance
There's a few issues with your implementation.
Use the Umbraco MemberService
You're reinventing the wheel by building a new table which holds member information (such as username and password).
Umbraco has built in membership which can handle members of your site. You can view the GUI in Umbraco at /umbraco/#/member. Using this GUI, you can manually create and edit members.
You can also programmatically create end edit members in this section using this MemberService.
For example, register a member:
var MemberService = ApplicationContext.Current.Services.MemberService
var member = MemberService.CreateMemberWithIdentity(newEmail, newEmail, newName, "Member");
MemberService.Save(member);
MemberService.SavePassword(member, newPassword);
FormsAuthentication.SetAuthCookie(newEmail, true);
Login:
var memberService = ApplicationContext.Current.Services.MemberService;
if (memberService.Exists(email))
{
if (Membership.ValidateUser(email, password))
{
FormsAuthentication.SetAuthCookie(email, true);
}
}
You can read about what other methods are available here.
You're mixing up your MVC
Your Test model isn't purely a model, is also has some controller in it as it is handling database stuff too!
Ideally, your model should just contain the data which has been sent forward, and your TestController should handle using that data.
As for fixing your binding issue
You're currently setting your page view model to Repower.Cms.Umbraco.Models.Test, where as I think it should be left as is.
Stop this model from inheriting from RenderModel.
Instead, render a partial with your code.
For your page view:
#inherits Umbraco.Web.Mvc.UmbracoViewPage
#{
Layout = "Master.cshtml";
}
#Html.Partial("Login", new Repower.Cms.Umbraco.Models.Test())
Partial called Login.cshtml:
#model Repower.Cms.Umbraco.Models.Test
#using (Html.BeginUmbracoForm<TestController>("Login"))
{
<div class="fontStyle">
<center>
<table style="margin-top: 100px;margin-left:150px">
<tr style="height:30px">
<td align="right">
#Html.LabelFor(m => m.User)
</td>
<td style="width:200px" align="right">
#Html.TextBoxFor(m => m.User)
</td>
<td style="width:250px;color:Red" align="left">
#Html.ValidationMessageFor(m => m.User)
</td>
</tr>
<tr style="height:30px">
<td align="right">
#Html.LabelFor(m => m.Password)
</td>
<td align="right">
#Html.PasswordFor(m => m.Password)
</td>
<td style="width:250px;color:Red" align="left">
#Html.ValidationMessageFor(m => m.Password)
</td>
</tr>
<tr style="height:30px">
<td colspan="2" align="center">
<input type="submit" value="Sign In" class="btnStyle" />
</td>
</tr>
</table>
</center>
</div>
}
Finally, update your controller to inherit from SurfaceController.
I hope this helps
This isn't the complete solution but should help you get off on the right track.

MVC 4 - Return error message from Controller - Show in View

I am doing a C# project using Razor in VS2010 (MVC 4).
I need to return an error message from Controller to View and show it to the user.
What I have tried:
CONTROLLER:
[HttpPost]
public ActionResult form_edit(FormModels model)
{
model.error_msg = model.update_content(model);
ModelState.AddModelError("error", "adfdghdghgdhgdhdgda");
ViewBag.error = TempData["error"];
return RedirectToAction("Form_edit", "Form");
}
VIEW:
#model mvc_cs.Models.FormModels
#using ctrlr = mvc_cs.Controllers.FormController
#using (Html.BeginForm("form_edit", "Form", FormMethod.Post))
{
<table>
<tr>
<td>
#Html.ValidationSummary("error")
#Html.ValidationMessage("error")
</td>
</tr>
<tr>
<th>
#Html.DisplayNameFor(model => model.content_name)
#Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--")
</th>
</tr>
</table>
<table>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
Please help me to achieve this.
The Return View(model) returns you error because you don't fill the model with the values in your post method and the model data for the dropdown is empty. Please provide the Get method to explain further how to manage displaying the error. In order to the error to be shown you should use this:
[HttpPost]
public ActionResult form_edit(FormModels model)
{
if(ModelState.IsValid())
{
--- operations
return Redirect("OtherAction", "SomeController");
}
// here you can use a little trick
//fill the model property that holds the information for the dropdown with the data
// you haven't provided the get method but it should look something like this
model.Countries = ... some data goes here;
model.dd_value = ... some other data;
model.dd_text = ... other data;
ModelState.AddModelError("", "adfdghdghgdhgdhdgda");
return View(model);
}
and then in the view just use :
#model mvc_cs.Models.FormModels
#using ctrlr = mvc_cs.Controllers.FormController
#using (Html.BeginForm("form_edit", "Form", FormMethod.Post))
{
<table>
<tr>
<td>
#Html.ValidationSummary(true)
</td>
</tr>
<tr>
<th>
#Html.DisplayNameFor(model => model.content_name)
#Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--")
</th>
</tr>
</table>
<table>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
This should work okay.
If you just use RedirectToAction it will redirect you to the get method --> you will have no error but the view will be just reloaded and no error would be shown.
other way around is that you can pass the error not by ModelState.AddError, but with ViewData["error"] like this:
[HttpPost]
public ActionResult form_edit(FormModels model)
{
TempData["error"] = "someErrorMessage";
return RedirectToAction("form_Post", "Form");
}
[HttpGet]
public ActionResult form_edit()
{
do stuff here ----
ViewData["error"] = TempData["error"];
return View();
}
#model mvc_cs.Models.FormModels
#using ctrlr = mvc_cs.Controllers.FormController
#using (Html.BeginForm("form_edit", "Form", FormMethod.Post))
{
<table>
<tr>
<td>
<div>#ViewData["error"]</div>
</td>
</tr>
<tr>
<th>
#Html.DisplayNameFor(model => model.content_name)
#Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--")
</th>
</tr>
</table>
<table>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
}
Thanks for all the replies.
I was able to solve this by doing the following:
CONTROLLER:
[HttpPost]
public ActionResult form_edit(FormModels model)
{
model.error_msg = model.update_content(model);
return RedirectToAction("Form_edit", "Form", model);
}
public ActionResult form_edit(FormModels model, string searchString,string id)
{
string test = model.selectedvalue;
var bal = new FormModels();
bal.Countries = bal.get_contentdetails(searchString);
bal.selectedvalue = id;
bal.dd_text = "content_name";
bal.dd_value = "content_id";
test = model.error_msg;
ViewBag.head = "Heading";
if (model.error_msg != null)
{
ModelState.AddModelError("error_msg", test);
}
model.error_msg = "";
return View(bal);
}
VIEW:
#using (Html.BeginForm("form_edit", "Form", FormMethod.Post))
{
<table>
<tr>
<td>
#ViewBag.error
#Html.ValidationMessage("error_msg")
</td>
</tr>
<tr>
<th>
#Html.DisplayNameFor(model => model.content_name)
#Html.DropDownListFor(x => x.selectedvalue, new SelectList(Model.Countries, Model.dd_value, Model.dd_text), "-- Select Product--")
</th>
</tr>
</table>
}
If you want to do a redirect, you can either:
ViewBag.Error = "error message";
or
TempData["Error"] = "error message";
You can add this to your _Layout.cshtml:
#using MyProj.ViewModels;
...
#if (TempData["UserMessage"] != null)
{
var message = (MessageViewModel)TempData["UserMessage"];
<div class="alert #message.CssClassName" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>#message.Title</strong>
#message.Message
</div>
}
Then if you want to throw an error message in your controller:
TempData["UserMessage"] = new MessageViewModel() { CssClassName = "alert-danger alert-dismissible", Title = "Error", Message = "This is an error message" };
MessageViewModel.cs:
public class MessageViewModel
{
public string CssClassName { get; set; }
public string Title { get; set; }
public string Message { get; set; }
}
Note: Using Bootstrap 4 classes.

Redirecting from one CGI page to another

Merged with How can I redirect the client from one CGI page to another using Perl?.
I am a nembie to CGI with Perl, please do help me out and please do correct me wherever I have committed a mistake.
This is the code. The problem is that after the password is validated and found as correct, instead of redirecting to the next page it is giving this message:
Status: 302 Found
Location: http://localhost/cgi-bin/Main.cgi
#!C:\perl\bin\perl.exe
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use DBI;
use strict;
use DBI;
use strict;
use warnings;
my $q = new CGI;
print "Content-Type: text/html\n\n";
if ($q->param("Login")) {
my $Password = param('Password');
if (!$Password) {
print "Please Enter the Password";
} else {
my $dbh = DBI->connect(
"dbi:SQLite:DEVICE.db",
"", "",
{
RaiseError => 1,
AutoCommit => 1
}
);
my $sth = $dbh->prepare("select * from Settings where Password = ?");
$sth->execute($Password);
if (my $pass = $sth->fetchrow_hashref) {
print redirect(-url => 'http://localhost/cgi-bin/Main.cgi');
} else {
print "Invalid Password";
display_form();
}
$dbh->disconnect;
}
}
if (($q->param("submit"))) {
process_form();
} else {
display_form();
}
sub process_form {
if (validate_form()) {
}
}
sub validate_form {
my $User_Password = $q->param("Password");
my $error_message = "";
$error_message .= "Please enter the Password<br/>" if (!$User_Password);
if ($error_message) {
display_form($error_message, $User_Password);
return 0;
}
}
sub display_form {
my $error_message = shift;
print <<END_HTML;
<html>
<head><title>Form Validation</title></head>
<body>
<form method="post">
<input type="hidden" name="submit" value="Submit">
<TABLE align="center" bgcolor=#B0C4DE>
<TR>
<TD> Enter The Password And Click Login</TD>
</TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR>
<TD><b>PASSWORD</b> : <input type="password" name="Password" size="20" maxlength="15" /></TD>
</TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR></TR>
<TR>
<TD align="center" colspan="2">
<input type="submit" name="Login" value="Login">
<input type="reset" name="Cancel" value="Cancel">
</TD>
</TR>
</TABLE
</form>
</body>
</html>
END_HTML
}