我想弄清楚我从这个论坛得到的这段代码有什么问题基本上我试图修改它,看看我是否可以解决上面的这个错误,它也有一个 undefined index 错误。我的问题是为什么错误是因为一切似乎都很好并且绑定(bind)正常
如果我将 POST 替换为 if(isset($_POST['btn-update'])) 中的 GET 错误消失了但什么都没有发生。我试图理解为什么上面的 sqlstate[hy093] 错误 和 $id = $_POST['edit_id'] 上的 在crud函数中定义undefined index error ;
public function getID($id)
{
$stmt = $this->db->prepare("SELECT * FROM tbl_users WHERE id=:id");
$stmt->execute(array(":id"=>$id));
$editRow=$stmt->fetch(PDO::FETCH_ASSOC);
return $editRow;
}
crud 类中的绑定(bind)似乎没问题
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(E_ALL);
class crud
{
private $db;
function __construct($DB_con)
{
$this->db = $DB_con;
}
public function create($fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivity)
{
try
{
$stmt = $this->db->prepare("INSERT INTO tbl_users(first_name,last_name,employee_nrc, phone_no, business_type ,business_site ,business_activity) VALUES(:fname, :lname, :employee_nrc, :Phone, :Businesstype, :Businesssite, :Businessactivity)");
$stmt->bindparam(":fname",$fname);
$stmt->bindparam(":lname",$lname);
$stmt->bindparam(":employee_nrc",$employee_nrc);
$stmt->bindparam(":Phone",$Phone);
$stmt->bindparam(":Businesstype",$Businesstype);
$stmt->bindparam(":Businesssite",$Businesssite);
$stmt->bindparam(":Businessactivity",$Businessactivity);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
public function getID($id)
{
$stmt = $this->db->prepare("SELECT * FROM tbl_users WHERE id=:id");
$stmt->execute(array(":id"=>$id));
$editRow=$stmt->fetch(PDO::FETCH_ASSOC);
return $editRow;
}
public function update($id,$fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivityt)
{
try
{
$stmt=$this->db->prepare("UPDATE tbl_users SET first_name=:fname,
last_name=:lname,
employee_nrc=:employee_nrc,
phone_no=:Phone,
business_type=:Businesstype,
business_site=:Businesssite,
business_activity=:Businessactivity
WHERE id=:id ");
$stmt->bindparam(":fname",$fname);
$stmt->bindparam(":lname",$lname);
$stmt->bindparam(":employee_nrc",$employee_nrc);
$stmt->bindparam(":Businesstype",$Phone);
$stmt->bindparam(":Businesstype",$Businesstype);
$stmt->bindparam(":Businesssite",$Businesssite);
$stmt->bindparam(":Businessactivity",$Businessactivity);
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}
catch(PDOException $e)
{
echo $e->getMessage();
return false;
}
}
public function delete($id)
{
$stmt = $this->db->prepare("DELETE FROM tbl_users WHERE id=:id");
$stmt->bindparam(":id",$id);
$stmt->execute();
return true;
}
/* paging */
public function dataview($query)
{
$stmt = $this->db->prepare($query);
$stmt->execute();
if($stmt->rowCount()>0)
{
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php print($row['id']); ?></td>
<td><?php print($row['first_name']); ?></td>
<td><?php print($row['last_name']); ?></td>
<td><?php print($row['employee_nrc']); ?></td>
<td><?php print($row['phone_no']); ?></td>
<td><?php print($row['business_type']); ?></td>
<td><?php print($row['business_site']); ?></td>
<td><?php print($row['business_activity']); ?></td>
<td align="center">
<a href="edit-data.php?edit_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-edit"></i></a>
</td>
<td align="center">
<a href="delete.php?delete_id=<?php print($row['id']); ?>"><i class="glyphicon glyphicon-remove-circle"></i></a>
</td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td>Nothing here...</td>
</tr>
<?php
}
}
public function paging($query,$records_per_page)
{
$starting_position=0;
if(isset($_GET["page_no"]))
{
$starting_position=($_GET["page_no"]-1)*$records_per_page;
}
$query2=$query." limit $starting_position,$records_per_page";
return $query2;
}
public function paginglink($query,$records_per_page)
{
$self = $_SERVER['PHP_SELF'];
$stmt = $this->db->prepare($query);
$stmt->execute();
$total_no_of_records = $stmt->rowCount();
if($total_no_of_records > 0)
{
?><ul class="pagination"><?php
$total_no_of_pages=ceil($total_no_of_records/$records_per_page);
$current_page=1;
if(isset($_GET["page_no"]))
{
$current_page=$_GET["page_no"];
}
if($current_page!=1)
{
$previous =$current_page-1;
echo "<li><a href='".$self."?page_no=1'>First</a></li>";
echo "<li><a href='".$self."?page_no=".$previous."'>Previous</a></li>";
}
for($i=1;$i<=$total_no_of_pages;$i++)
{
if($i==$current_page)
{
echo "<li><a href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
}
else
{
echo "<li><a href='".$self."?page_no=".$i."'>".$i."</a></li>";
}
}
if($current_page!=$total_no_of_pages)
{
$next=$current_page+1;
echo "<li><a href='".$self."?page_no=".$next."'>Next</a></li>";
echo "<li><a href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
}
?></ul><?php
}
}
/* paging */
}
发生未定义索引错误的编辑页面是$id = $_POST['edit_id'];
<?php
include_once 'dbconfig.php';
$first_name= null;
$last_name=null;
$employee_nrc=null;
$phone_no=null;
$business_type=null;
$business_site=null;
$business_activity=null;
if(isset($_POST['btn-update']))
{
$id = $_POST['edit_id'];
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$employee_nrc = $_POST['employee_nrc'];
$Phone = $_POST['phone_no'];
$Businesstype = $_POST['business_type'];
$Businesssite = $_POST['business_site'];
$Businessactivity = $_POST['business_activity'];
if($crud->update($id,$fname,$lname,$employee_nrc,$Phone,$Businesstype,$Businesssite,$Businessactivity))
{
$msg = "<div class='alert alert-info'>
<strong>WOW!</strong> Record was updated successfully <a href='index.php'>HOME</a>!
</div>";
}
else
{
$msg = "<div class='alert alert-warning'>
<strong>SORRY!</strong> ERROR while updating record !
</div>";
}
}
if(isset($_GET['edit_id']))
{
$id = $_GET['edit_id'];
extract($crud->getID($id));
}
?>
<?php include_once 'header.php'; ?>
<div class="clearfix"></div>
<div class="container">
<?php
if(isset($msg))
{
echo $msg;
}
?>
</div>
<div class="clearfix"></div><br />
<div class="container">
<form class="form-horizontal" method='post'>
<fieldset>
<legend>Registration System</legend>
<div class="form-group">
<label for="inputFirstName" class="col-lg-2 control-label">First Name</label>
<div class="col-lg-10">
<input type="text" name="first_name" class="form-control" value="<?php echo $first_name; ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputLastName" class="col-lg-2 control-label">Last Name</label>
<div class="col-lg-10">
<input type="text" name="last_name" class="form-control" value="<?php echo $last_name; ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmployeeNRC" class="col-lg-2 control-label">Employee NRC</label>
<div class="col-lg-10">
<input type="text" name="employee_nrc" class="form-control" value="<?php echo $employee_nrc; ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Employee Phone Number</label>
<div class="col-lg-10">
<input type="text" name="phone_no" class='form-control' value="<?php echo $phone_no; ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Type</label>
<div class="col-lg-10">
<input type="text" name="business_type" class='form-control' value="<?php echo $business_type; ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Location</label>
<div class="col-lg-10">
<input type="text" name="business_site" class='form-control' value="<?php echo $business_site; ?>" required>
</div>
</div>
<div class="form-group">
<label for="inputEmployeePhoneNumber" class="col-lg-2 control-label">Business Activities</label>
<div class="col-lg-10">
<input type="text" name="business_activity" class='form-control' value="<?php echo $business_activity; ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary" name="btn-update">
<span class="glyphicon glyphicon-edit"></span> Update this Record
</button>
<a href="index.php" class="btn btn-large btn-success"><i class="glyphicon glyphicon-backward"></i> CANCEL</a>
</div>
</div>
</fieldset>
</form>
</div>
<?php include_once 'footer.php'; ?>
帮助识别错误和建议,以学习为目的,我将不胜感激想法和解决方案
最佳答案
有一种类型后来在这段代码上被更正了
$stmt->bindparam(":Businesstype",$Phone);
本来应该是
$stmt->bindparam(":Phone",$Phone);
谢谢。
关于php - sqlstate[hy093] 错误发生的原因?当满足所有条件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34348908/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]
尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot