Php 我想把代码变成一个函数,并让它与表单文件分开调用,但我无法让它工作
其目的是将下面的php用作编辑表单中记录的函数,而不是将php保存在与表单相同的文件中(如下所示)。这段代码在同一个文件中工作得非常好,但我想将php变成一个函数,并从函数文件中使用它。这就是问题所在 我想整理我的代码,并为每件事提供函数。我的问题是,当我移动它并将其称为函数时,如果代码与表单位于同一个文件中,它就无法工作Php 我想把代码变成一个函数,并让它与表单文件分开调用,但我无法让它工作,php,html,forms,Php,Html,Forms,其目的是将下面的php用作编辑表单中记录的函数,而不是将php保存在与表单相同的文件中(如下所示)。这段代码在同一个文件中工作得非常好,但我想将php变成一个函数,并从函数文件中使用它。这就是问题所在 我想整理我的代码,并为每件事提供函数。我的问题是,当我移动它并将其称为函数时,如果代码与表单位于同一个文件中,它就无法工作 <?php require_once('../db.php'); $upload_dir = '../uploads'; //All this ph
<?php
require_once('../db.php');
$upload_dir = '../uploads';
//All this php is what I tried to put into a function in a separate
file.
//The $row variable and $id variable are not recognised unless I leave
the php
//in the same file.
//functions file would use this code and i then made the function using
the php below.
/* if (isset($_POST['edit_dog_btn'])) {
** edit_dog_record();
*/ }
if (isset($_GET['id'])) {
$id = $_GET['id'];
$id=mysqli_real_escape_string($conn,trim($_GET['id']));
echo $id;
$sql = "SELECT * from contacts where id=".$id;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
}else {
$errorMsg = 'Could not Find Any Record';
}
}
if(isset($_POST['Submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$breed = $_POST['breed'];
$description = $_POST['description'];
$imgName = $_FILES['image']['name'];
$imgTmp = $_FILES['image']['tmp_name'];
$imgSize = $_FILES['image']['size'];
if($imgName){
$imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));
$allowExt = array('jpeg', 'jpg', 'png', 'gif');
$userPic = time().'_'.rand(1000,9999).'.'.$imgExt;
if(in_array($imgExt, $allowExt)){
if($imgSize < 5000000){
unlink($upload_dir.$row['image']);
move_uploaded_file($imgTmp ,$upload_dir.$userPic);
}else{
$errorMsg = 'Image too large';
}
}else{
$errorMsg = 'Please select a valid image';
}
}else{
$userPic = $row['image'];
}
if(!isset($errorMsg)){
$sql = "update contacts set name = '".$name."',
age = '".$age."',
gender = '".$gender."',
breed= '".$breed."',
description= '".$description."',
image = '".$userPic."'
where id=".$id;
$result = mysqli_query($conn, $sql);
if($result){
$successMsg = 'New record updated successfully';
header('Location:index_admin.php');
}else{
$errorMsg = 'Error '.mysqli_error($conn);
}
}
}
?>
//I think something else is required in the form, in order to use the
function //as a separate file.
<form class="" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter
Name" value="<?php echo $row['name']; ?>">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="text" class="form-control" name="age" placeholder="Enter
Age" value="<?php echo $row['age']; ?>">
</div>
<div class="form-group">
<label for="gender">Gender:</label>
<input type="text" class="form-control" name="gender" placeholder="Enter
Gender" value="<?php echo $row['gender']; ?>">
</div>
<div class="form-group">
<label for="breed">Breed</label>
<input type="text" class="form-control" name="breed"
placeholder="Enter Breed" value="<?php echo $row['breed']; ?>">
</div>
<div class="form-group">
<label for="description">Breed</label>
<input type="text" class="form-control" name="description"
placeholder="Enter a Description" value="<?php echo
$row['description']; ?>">
</div>
<div class="form-group">
<label for="image">Choose Image</label>
<div class="col-md-4">
<img src="<?php echo $upload_dir.$row['image'] ?>" width="100">
<input type="file" class="form-control" name="image" value="">
</div>
</div>
<div class="form-group">
<button type="submit" name="edit_dog_btn" class="btn btn-primary
waves">Submit</button>
</div>
</form>
if (isset($_POST['edit_dog_btn'])) {
edit_dog_record($arg1, $arg2, ...);
}
我尝试了各种方法,但不确定如何让表单识别$row变量或$id变量作为函数
<?php
require_once('../db.php');
$upload_dir = '../uploads';
//All this php is what I tried to put into a function in a separate
file.
//The $row variable and $id variable are not recognised unless I leave
the php
//in the same file.
//functions file would use this code and i then made the function using
the php below.
/* if (isset($_POST['edit_dog_btn'])) {
** edit_dog_record();
*/ }
if (isset($_GET['id'])) {
$id = $_GET['id'];
$id=mysqli_real_escape_string($conn,trim($_GET['id']));
echo $id;
$sql = "SELECT * from contacts where id=".$id;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
}else {
$errorMsg = 'Could not Find Any Record';
}
}
if(isset($_POST['Submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$breed = $_POST['breed'];
$description = $_POST['description'];
$imgName = $_FILES['image']['name'];
$imgTmp = $_FILES['image']['tmp_name'];
$imgSize = $_FILES['image']['size'];
if($imgName){
$imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));
$allowExt = array('jpeg', 'jpg', 'png', 'gif');
$userPic = time().'_'.rand(1000,9999).'.'.$imgExt;
if(in_array($imgExt, $allowExt)){
if($imgSize < 5000000){
unlink($upload_dir.$row['image']);
move_uploaded_file($imgTmp ,$upload_dir.$userPic);
}else{
$errorMsg = 'Image too large';
}
}else{
$errorMsg = 'Please select a valid image';
}
}else{
$userPic = $row['image'];
}
if(!isset($errorMsg)){
$sql = "update contacts set name = '".$name."',
age = '".$age."',
gender = '".$gender."',
breed= '".$breed."',
description= '".$description."',
image = '".$userPic."'
where id=".$id;
$result = mysqli_query($conn, $sql);
if($result){
$successMsg = 'New record updated successfully';
header('Location:index_admin.php');
}else{
$errorMsg = 'Error '.mysqli_error($conn);
}
}
}
?>
//I think something else is required in the form, in order to use the
function //as a separate file.
<form class="" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter
Name" value="<?php echo $row['name']; ?>">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="text" class="form-control" name="age" placeholder="Enter
Age" value="<?php echo $row['age']; ?>">
</div>
<div class="form-group">
<label for="gender">Gender:</label>
<input type="text" class="form-control" name="gender" placeholder="Enter
Gender" value="<?php echo $row['gender']; ?>">
</div>
<div class="form-group">
<label for="breed">Breed</label>
<input type="text" class="form-control" name="breed"
placeholder="Enter Breed" value="<?php echo $row['breed']; ?>">
</div>
<div class="form-group">
<label for="description">Breed</label>
<input type="text" class="form-control" name="description"
placeholder="Enter a Description" value="<?php echo
$row['description']; ?>">
</div>
<div class="form-group">
<label for="image">Choose Image</label>
<div class="col-md-4">
<img src="<?php echo $upload_dir.$row['image'] ?>" width="100">
<input type="file" class="form-control" name="image" value="">
</div>
</div>
<div class="form-group">
<button type="submit" name="edit_dog_btn" class="btn btn-primary
waves">Submit</button>
</div>
</form>
if (isset($_POST['edit_dog_btn'])) {
edit_dog_record($arg1, $arg2, ...);
}
//我认为表单中还需要其他内容,以便使用
函数//作为一个单独的文件。
名称
当使用函数时,我们需要向它传递参数。我从你的问题中怀疑,你很快就到了,但可能没有把论点传递进去
在表单页面中,所有变量都可用于函数进程,但是当您从另一个页面调用函数时,除非您将这些变量作为函数参数传递,否则该函数没有可使用的变量
内联代码(例如您的示例)可能如下所示
$drink = "coffee";
echo $drink;
这将呼应咖啡这个词。但是如果我们在别处创建一个函数并调用它,我们可以这样编写它
function outputDrink() {
echo $drink;
}
我们可以在我们的页面中这样称呼它:
$drink = "coffee";
outputDrink();
但这不会显示什么。。。因为$drink没有被传递到函数中。为了纠正这一点,我们需要一些修正
function outputDrink($drink) {
echo $drink;
}
这样称呼它
$drink = "coffee";
outputDrink($drink);
最后,如果我们需要传递更多信息,我们可以传递一个数组或逗号分隔我们的值。事实上,我们可以设置默认值以及更多。但也许这些选择对于另一个答案来说更好
祝你好运 使用函数时,我们需要向其传递参数。我从你的问题中怀疑,你很快就到了,但可能没有把论点传递进去
在表单页面中,所有变量都可用于函数进程,但是当您从另一个页面调用函数时,除非您将这些变量作为函数参数传递,否则该函数没有可使用的变量
内联代码(例如您的示例)可能如下所示
$drink = "coffee";
echo $drink;
这将呼应咖啡这个词。但是如果我们在别处创建一个函数并调用它,我们可以这样编写它
function outputDrink() {
echo $drink;
}
我们可以在我们的页面中这样称呼它:
$drink = "coffee";
outputDrink();
但这不会显示什么。。。因为$drink没有被传递到函数中。为了纠正这一点,我们需要一些修正
function outputDrink($drink) {
echo $drink;
}
这样称呼它
$drink = "coffee";
outputDrink($drink);
最后,如果我们需要传递更多信息,我们可以传递一个数组或逗号分隔我们的值。事实上,我们可以设置默认值以及更多。但也许这些选择对于另一个答案来说更好
祝你好运 将参数传递到函数中
<?php
require_once('../db.php');
$upload_dir = '../uploads';
//All this php is what I tried to put into a function in a separate
file.
//The $row variable and $id variable are not recognised unless I leave
the php
//in the same file.
//functions file would use this code and i then made the function using
the php below.
/* if (isset($_POST['edit_dog_btn'])) {
** edit_dog_record();
*/ }
if (isset($_GET['id'])) {
$id = $_GET['id'];
$id=mysqli_real_escape_string($conn,trim($_GET['id']));
echo $id;
$sql = "SELECT * from contacts where id=".$id;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
}else {
$errorMsg = 'Could not Find Any Record';
}
}
if(isset($_POST['Submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$breed = $_POST['breed'];
$description = $_POST['description'];
$imgName = $_FILES['image']['name'];
$imgTmp = $_FILES['image']['tmp_name'];
$imgSize = $_FILES['image']['size'];
if($imgName){
$imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));
$allowExt = array('jpeg', 'jpg', 'png', 'gif');
$userPic = time().'_'.rand(1000,9999).'.'.$imgExt;
if(in_array($imgExt, $allowExt)){
if($imgSize < 5000000){
unlink($upload_dir.$row['image']);
move_uploaded_file($imgTmp ,$upload_dir.$userPic);
}else{
$errorMsg = 'Image too large';
}
}else{
$errorMsg = 'Please select a valid image';
}
}else{
$userPic = $row['image'];
}
if(!isset($errorMsg)){
$sql = "update contacts set name = '".$name."',
age = '".$age."',
gender = '".$gender."',
breed= '".$breed."',
description= '".$description."',
image = '".$userPic."'
where id=".$id;
$result = mysqli_query($conn, $sql);
if($result){
$successMsg = 'New record updated successfully';
header('Location:index_admin.php');
}else{
$errorMsg = 'Error '.mysqli_error($conn);
}
}
}
?>
//I think something else is required in the form, in order to use the
function //as a separate file.
<form class="" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter
Name" value="<?php echo $row['name']; ?>">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="text" class="form-control" name="age" placeholder="Enter
Age" value="<?php echo $row['age']; ?>">
</div>
<div class="form-group">
<label for="gender">Gender:</label>
<input type="text" class="form-control" name="gender" placeholder="Enter
Gender" value="<?php echo $row['gender']; ?>">
</div>
<div class="form-group">
<label for="breed">Breed</label>
<input type="text" class="form-control" name="breed"
placeholder="Enter Breed" value="<?php echo $row['breed']; ?>">
</div>
<div class="form-group">
<label for="description">Breed</label>
<input type="text" class="form-control" name="description"
placeholder="Enter a Description" value="<?php echo
$row['description']; ?>">
</div>
<div class="form-group">
<label for="image">Choose Image</label>
<div class="col-md-4">
<img src="<?php echo $upload_dir.$row['image'] ?>" width="100">
<input type="file" class="form-control" name="image" value="">
</div>
</div>
<div class="form-group">
<button type="submit" name="edit_dog_btn" class="btn btn-primary
waves">Submit</button>
</div>
</form>
if (isset($_POST['edit_dog_btn'])) {
edit_dog_record($arg1, $arg2, ...);
}
这样,变量将可用于函数。因为它位于不同的页面上,所以它不仅可以访问值
function edit_dog_record($arg1, $arg2){
//you can use $arg1 and $arg2 now
}
将参数传递到函数中
<?php
require_once('../db.php');
$upload_dir = '../uploads';
//All this php is what I tried to put into a function in a separate
file.
//The $row variable and $id variable are not recognised unless I leave
the php
//in the same file.
//functions file would use this code and i then made the function using
the php below.
/* if (isset($_POST['edit_dog_btn'])) {
** edit_dog_record();
*/ }
if (isset($_GET['id'])) {
$id = $_GET['id'];
$id=mysqli_real_escape_string($conn,trim($_GET['id']));
echo $id;
$sql = "SELECT * from contacts where id=".$id;
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
}else {
$errorMsg = 'Could not Find Any Record';
}
}
if(isset($_POST['Submit'])){
$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$breed = $_POST['breed'];
$description = $_POST['description'];
$imgName = $_FILES['image']['name'];
$imgTmp = $_FILES['image']['tmp_name'];
$imgSize = $_FILES['image']['size'];
if($imgName){
$imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));
$allowExt = array('jpeg', 'jpg', 'png', 'gif');
$userPic = time().'_'.rand(1000,9999).'.'.$imgExt;
if(in_array($imgExt, $allowExt)){
if($imgSize < 5000000){
unlink($upload_dir.$row['image']);
move_uploaded_file($imgTmp ,$upload_dir.$userPic);
}else{
$errorMsg = 'Image too large';
}
}else{
$errorMsg = 'Please select a valid image';
}
}else{
$userPic = $row['image'];
}
if(!isset($errorMsg)){
$sql = "update contacts set name = '".$name."',
age = '".$age."',
gender = '".$gender."',
breed= '".$breed."',
description= '".$description."',
image = '".$userPic."'
where id=".$id;
$result = mysqli_query($conn, $sql);
if($result){
$successMsg = 'New record updated successfully';
header('Location:index_admin.php');
}else{
$errorMsg = 'Error '.mysqli_error($conn);
}
}
}
?>
//I think something else is required in the form, in order to use the
function //as a separate file.
<form class="" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter
Name" value="<?php echo $row['name']; ?>">
</div>
<div class="form-group">
<label for="age">Age:</label>
<input type="text" class="form-control" name="age" placeholder="Enter
Age" value="<?php echo $row['age']; ?>">
</div>
<div class="form-group">
<label for="gender">Gender:</label>
<input type="text" class="form-control" name="gender" placeholder="Enter
Gender" value="<?php echo $row['gender']; ?>">
</div>
<div class="form-group">
<label for="breed">Breed</label>
<input type="text" class="form-control" name="breed"
placeholder="Enter Breed" value="<?php echo $row['breed']; ?>">
</div>
<div class="form-group">
<label for="description">Breed</label>
<input type="text" class="form-control" name="description"
placeholder="Enter a Description" value="<?php echo
$row['description']; ?>">
</div>
<div class="form-group">
<label for="image">Choose Image</label>
<div class="col-md-4">
<img src="<?php echo $upload_dir.$row['image'] ?>" width="100">
<input type="file" class="form-control" name="image" value="">
</div>
</div>
<div class="form-group">
<button type="submit" name="edit_dog_btn" class="btn btn-primary
waves">Submit</button>
</div>
</form>
if (isset($_POST['edit_dog_btn'])) {
edit_dog_record($arg1, $arg2, ...);
}
这样,变量将可用于函数。因为它位于不同的页面上,所以它不仅可以访问值
function edit_dog_record($arg1, $arg2){
//you can use $arg1 and $arg2 now
}
您的代码易受SQL注入攻击。您应该使用准备好的语句。您的代码易受SQL注入攻击。你应该使用事先准备好的陈述。谢谢。。将需要进一步研究此功能。还是没法用,谢谢。。将需要进一步研究此功能。还是无法让它工作。