更新数据库时缺少参数错误[重复]

时间:2022-02-07 02:24:39

This question already has an answer here:

这个问题在这里已有答案:

Here i am trying to pass the values of text area and hidden field to php through the modal popup form.

在这里,我试图通过模态弹出窗体将文本区域和隐藏字段的值传递给php。

i have a hidden field containing the product ID and text area contains some text. when i click submit button it should be passed to php and update in database. when i fill in the form and click submit i am getting error

我有一个包含产品ID的隐藏字段,文本区域包含一些文本。当我点击提交按钮时,它应该传递给PHP并在数据库中更新。当我填写表格并点击提交我收到错误

Notice: Undefined variable: prod_id 

Warning: Missing argument 1 for updateProduct()

html

HTML

<div class="modal fade" id="rejectModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">Are you sure to reject this product?</h4>
      </div>
      <div class="modal-body">
       <h4>Why do you want to reject this product!</h4>
       <form action="product-reject.php" method="get"  role="form" enctype="multipart/form-data">
       <?php
       echo '<input type="hidden" class="form-control" name="product_id" id="user_role" value="'.$qs.'">';
       echo '<textarea cols="43" rows="5" class="reject-textarea" name="reject_reason"></textarea>';
       ?>                 
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <input type="submit" class="btn btn-primary" value="OK">
      </div>
      </form>
    </div>
  </div>
</div>

Php

<?php 
    require_once('configuration.php');
    $product_id = $_GET['product_id'];
    $reject_for = $_GET['reject_reason'];    

            if (isset($product_id,$reject_for)){  
                rejectProduct($product_id,$reject_for);
            } else {  
                echo "Are you trying to do something nasty??";
            }

            function rejectProduct($product_id,$reject_for)
            {
                $conn = new mysqli(db_host, db_user, db_password, db_name);
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                } 
                $sql = "INSERT INTO rejected_products(product_id,reason_to_reject) VALUES ($product_id,'$reject_for')";
                $result = $conn->query($sql);
                if ($conn->query($sql) === TRUE) {
                    updateProduct($prod_id);        
                } else {
                    echo "Error updating record: " . $conn->error;
                }
                $conn->close(); 
            }


            function updateProduct($prod_id)
            {
                $conn = new mysqli(db_host, db_user, db_password, db_name);
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                } 
                $sql = "UPDATE list SET product_publish_status = '2' WHERE product_id = '$prod_id'";    
                $result = $conn->query($sql);
                if ($conn->query($sql) === TRUE) {
                    updateProduct();
                    header("Location: index.php");
                } else {
                    echo "Error updating record: " . $conn->error;
                }
                $conn->close(); 
            }
?>

how can i resolve this?

我怎么解决这个?

4 个解决方案

#1


1  

$prod_id is undefined in your php code.

您的PHP代码中未定义$ prod_id。

If you want the product id to be passed as argument, then replace

如果您希望将产品ID作为参数传递,则替换

updateProduct($prod_id); 

with

updateProduct($product_id);

#2


0  

You are using $prod_id when you should be calling

你应该打电话时使用$ prod_id

updateProduct($product_id);

#3


0  

Replace $prod_id with $product_id.

用$ product_id替换$ prod_id。

function updateProduct($product_id) // Make Changes Here
{
    $conn = new mysqli(db_host, db_user, db_password, db_name);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "UPDATE list SET product_publish_status = '2' WHERE product_id = '$prod_id'";    
    $result = $conn->query($sql);
    if ($conn->query($sql) === TRUE) {
        updateProduct();
        header("Location: index.php");
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $conn->close(); 
}

#4


0  

You have done this here:

你在这里完成了这个:

function updateProduct($prod_id)
{
    $conn = new mysqli(db_host, db_user, db_password, db_name);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "UPDATE list SET product_publish_status = '2' WHERE product_id = '$prod_id'";    
    $result = $conn->query($sql);
    if ($conn->query($sql) === TRUE) {
        updateProduct();
        header("Location: index.php");
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $conn->close(); 
}

See the if ($conn->query($sql) === TRUE) { updateProduct(); header("Location: index.php"); }

请参阅if($ conn-> query($ sql)=== TRUE){updateProduct(); header(“Location:index.php”); }

It should have the $prod_id in there also, that's where it is missing.

它应该还有$ prod_id,这就是它缺失的地方。

If I can make a suggestion, to help you with this I would suggest using an IDE with syntax highlighting, it will show up errors like this easily and quickly, its how I found it :)

如果我可以提出建议,为了帮助你,我会建议使用带有语法高亮的IDE,它会很容易和快速地显示这样的错误,我的发现方式:)

And also, you don't need to do $conn->query($sql) === TRUE, the if handles the true/false comparison for you.

而且,你不需要做$ conn-> query($ sql)=== TRUE,if为你处理真/假比较。

Also in the first function you do

同样在你的第一个功能

$result = $conn->query($sql);
if ($conn->query($sql) === TRUE) {

2 database trips? Isn't that rather expensive?

2次数据库旅行?这不是很贵吗?

Also you are using (again first function)

你也在使用(再次第一个功能)

    updateProduct($prod_id);  

When the param that is given is $product_id (I think another user has mentioned this).

当给出的参数是$ product_id时(我认为另一个用户已经提到过这个)。

Another one! When you do the table and column names in a query, it is best to use back-ticks around them "** ` **" (key under the Esc key), and surround your strings in single quotes " ' " and escape your variables, e.g.:

另一个!当您在查询中执行表名和列名时,最好使用它们后面的“**”**(Esc键下的键),并用单引号“'”包围您的字符串并转义变量,例如:

$sql = "INSERT INTO `rejected_products`(`product_id`, `reason_to_reject`) VALUES ('{$product_id}','{$reject_for}');";

A well structured query is a happy query :)

结构良好的查询是一个快乐的查询:)

#1


1  

$prod_id is undefined in your php code.

您的PHP代码中未定义$ prod_id。

If you want the product id to be passed as argument, then replace

如果您希望将产品ID作为参数传递,则替换

updateProduct($prod_id); 

with

updateProduct($product_id);

#2


0  

You are using $prod_id when you should be calling

你应该打电话时使用$ prod_id

updateProduct($product_id);

#3


0  

Replace $prod_id with $product_id.

用$ product_id替换$ prod_id。

function updateProduct($product_id) // Make Changes Here
{
    $conn = new mysqli(db_host, db_user, db_password, db_name);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "UPDATE list SET product_publish_status = '2' WHERE product_id = '$prod_id'";    
    $result = $conn->query($sql);
    if ($conn->query($sql) === TRUE) {
        updateProduct();
        header("Location: index.php");
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $conn->close(); 
}

#4


0  

You have done this here:

你在这里完成了这个:

function updateProduct($prod_id)
{
    $conn = new mysqli(db_host, db_user, db_password, db_name);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $sql = "UPDATE list SET product_publish_status = '2' WHERE product_id = '$prod_id'";    
    $result = $conn->query($sql);
    if ($conn->query($sql) === TRUE) {
        updateProduct();
        header("Location: index.php");
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $conn->close(); 
}

See the if ($conn->query($sql) === TRUE) { updateProduct(); header("Location: index.php"); }

请参阅if($ conn-> query($ sql)=== TRUE){updateProduct(); header(“Location:index.php”); }

It should have the $prod_id in there also, that's where it is missing.

它应该还有$ prod_id,这就是它缺失的地方。

If I can make a suggestion, to help you with this I would suggest using an IDE with syntax highlighting, it will show up errors like this easily and quickly, its how I found it :)

如果我可以提出建议,为了帮助你,我会建议使用带有语法高亮的IDE,它会很容易和快速地显示这样的错误,我的发现方式:)

And also, you don't need to do $conn->query($sql) === TRUE, the if handles the true/false comparison for you.

而且,你不需要做$ conn-> query($ sql)=== TRUE,if为你处理真/假比较。

Also in the first function you do

同样在你的第一个功能

$result = $conn->query($sql);
if ($conn->query($sql) === TRUE) {

2 database trips? Isn't that rather expensive?

2次数据库旅行?这不是很贵吗?

Also you are using (again first function)

你也在使用(再次第一个功能)

    updateProduct($prod_id);  

When the param that is given is $product_id (I think another user has mentioned this).

当给出的参数是$ product_id时(我认为另一个用户已经提到过这个)。

Another one! When you do the table and column names in a query, it is best to use back-ticks around them "** ` **" (key under the Esc key), and surround your strings in single quotes " ' " and escape your variables, e.g.:

另一个!当您在查询中执行表名和列名时,最好使用它们后面的“**”**(Esc键下的键),并用单引号“'”包围您的字符串并转义变量,例如:

$sql = "INSERT INTO `rejected_products`(`product_id`, `reason_to_reject`) VALUES ('{$product_id}','{$reject_for}');";

A well structured query is a happy query :)

结构良好的查询是一个快乐的查询:)