【问题标题】:how to fetch data from sql using form $_Post id in where clause如何在where子句中使用表单$_Post id从sql中获取数据
【发布时间】:2014-02-09 12:27:53
【问题描述】:

我正在使用带有 javascript 的表单,该表单用于动态添加 n 行并将数据发布到 mysql。 现在我想在 sql 语句中使用 where 子句(表单数据)向 mysql 发布更多信息。

这是我提交和发布数据的代码。

<script  src="jquery.min.js"></script>       
<script type="text/javascript">
$(function() {
        var addDiv = $('#addinput');
        var i = $('#addinput p').size() + 1;

        $('#addNew').live('click', function() {
                $('<p><select name="stockid[]' + i +'" onchange="showUser(this.value)"> <?php echo $item; ?></select> <select name="desc[]' + i +'" id="txtHint"> <?php echo $description; ?></ </select><a href="#" id="remNew">Remove</a> </p>').appendTo(addDiv);
                i++;


                return false;
        });

        $('#remNew').live('click', function() { 
                if( i > 2 ) {
                        $(this).parents('p').remove();
                        i--;
                }
                return false;
        });
});

</script>

<body>


<?php if (!isset($_POST['submit_val'])) { ?>
 <h1>Add your Hobbies</h1>
 <form method="post" action="">

 <div id="container">
 <p id="addNew"><a href="#"><span>Add New</span></a></p>
 <div id="addinput">

 <input type="submit" name="submit_val" value="Submit" />
 </form>
<?php } ?>
<?php

?>


<?php
    if (isset($_POST['submit_val']))
    {
        $stockid = $_POST["stockid"];
        $desc = $_POST["desc"];
        foreach($stockid as $a => $B)
        {
            $query = mysql_query("INSERT INTO 0_stock_master (stock_id,description) VALUES ('$stockid[$a]','$desc[$a]')", $connection );
        }
        echo "<i><h2><strong>" . count($_POST['stockid']) . "</strong> Hobbies Added</h2></i>";
    }
?>

当我尝试使用 select 语句并将数据发布到 mysql 时,它现在工作正常,但它不工作 这是代码

<?php
    $con=mysqli_connect("localhost","root","","inventory");

    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM 0_stock_master where id = '".$$_POST['stockid']."'");

    while($row = mysqli_fetch_array($result))
    {
      echo $row['price'];
    }

    mysqli_close($con);
?>

然后我像这样修改上述文件的邮政编码

<?php    
    if (isset($_POST['submit_val']))
    {
        $stockid = $_POST["stockid"];
        $desc = $_POST["desc"];
        $price = $row['price'];
        foreach($stockid as $a => $B)
        {
            $query = mysql_query("INSERT INTO 0_stock_master (stock_id,description,price) VALUES ('$stockid[$a]','$desc[$a]','$price[$a]')", $connection);
        }
        echo "<i><h2><strong>" . count($_POST['stockid']) . "</strong> Hobbies Added</h2></i>";
    }
?>

但价格列中的数据库中没有插入任何内容

【问题讨论】:

  • $$_POST 是故意的?

标签: javascript php mysql sql


【解决方案1】:

正如 aconrad 在 cmets 中所说,将 $$_POST 替换为 $_POST 可能会解决您的问题。

但我建议您将 mysqli_query() 更改为 mysqli_prepare (并通过等效的 mysqli_* 函数更改所有 mysql_*

我建议您将所有转换为mysqli_ 并使用准备好的语句而不是像这样的直接查询:

改变这个:

<?php
$result = mysqli_query($con,"SELECT * FROM 0_stock_master where id = '".$$_POST['stockid']."'");

while($row = mysqli_fetch_array($result))

到这里:

<?php
$stmt = mysqli_prepare($con,"SELECT price FROM 0_stock_master where id = ?");
mysqli_stmt_bind_param($stmt, 'i', $_POST['stockid']);
$result = mysqli_stmt_execute($stmt);
if (!$result)
  echo 'Mysql error : '.mysqli_stmt_error($stmt);
mysqli_stmt_bind_result($stmt, $price); // values will 
mysqli_stmt_fetch($stmt); // this call send the result in $price
mysqli_stmt_close($stmt);

改变这个:

<?php
$query = mysql_query("INSERT INTO 0_stock_master (stock_id,description,price) VALUES ('$stockid[$a]','$desc[$a]','$price[$a]')", $connection );

到这个:

<?php
$stmt = mysqli_prepare($connection, "INSERT INTO 0_stock_master (stock_id,description,price) VALUES (?, ?, ?)");
// I assume stock_id must be int, desc must be string, and price must be float
mysqli_stmt_bind_param($stmt, 'isf', $stockid[$a],$desc[$a],$price[$a]);
$query = mysqli_stmt_execute($stmt);
$affected_rows = mysqli_stmt_affected_rows($stmt);

编辑: 一些文档: MySQLi

mysqli_prepare(sql 查询更受 sql 注入保护)

mysqli_stmt_bind_param

mysqli_stmt_execute

mysqli_stmt_bind_result

mysqli_stmt_fetch

【讨论】:

  • 嘿,谢谢,这是迄今为止我见过的准备好的语句最清晰的例子!这也是我需要自己开始使用的东西 =)
  • 刚刚针对这种情况调整了文档示例。我个人强烈建议使用面向对象的风格,$my = new MySQLi($host, $user, $pass); 而不是 mysqli_connect
  • mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in
  • 我完成了答案(并修改了选择查询)。抱歉,我忘了告诉你必须使用 mysql_stmt_fetch 和 mysql_stmt_bind_result 而不是 fetch_array。我还添加了 mysql_stmt_error 的用法。请参阅我在帖子末尾添加的 php 文档
【解决方案2】:

更改代码以将价格值存储在新变量中:-

<?php
    $con=mysqli_connect("localhost","root","","inventory");
    $price = array(); //declare

    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $result = mysqli_query($con,"SELECT * FROM 0_stock_master where id = '".$_POST['stockid']."'");

    while($row = mysqli_fetch_array($result))
    {
      echo $row['price'];
      $price = $row['price']; //initiate
    }

    mysqli_close($con);
?>

<?php    
    if (isset($_POST['submit_val']))
    {
        $stockid = $_POST["stockid"];
        $desc = $_POST["desc"];


            $query = mysql_query("INSERT INTO 0_stock_master (stock_id,description,price) VALUES ('$stockid','$desc','$price')", $connection);

    }
?>

您的 $row['price'] 变量将仅存在于 while 循环中,因此您必须将其存储在预先存在的东西中并改用该变量。

假设两个代码sn-ps都在同一个文件中,也就是说。查看代码并查看第 3 行和第 27 行的更改。

另外,正如其他人所说,删除双 $$ 并在这一行上使用一个:-

$result = mysqli_query($con,"SELECT * FROM 0_stock_master where id = '".$$_POST['stockid']."'");

希望对你有所帮助:)

【讨论】:

  • 它显示错误Uninitialized string offset: 0 in C:\xampp\htdocs\abc\add2\index.php on line 128 该行是$query = mysql_query("INSERT INTO 0_stock_master (stock_id,description,price) VALUES ('$stockid[$a]','$desc[$a]','$price[$a]')", $connection);
  • 查看我的编辑回答,应该会为您插入 1 条记录,然后从那里开始 =)
  • 它正在插入记录,但文本数组已插入数据库中用于 stockid 和 description 并且 $price 为空。
  • 如果您的价格仍然为空,您需要像发送stockiddescription 值一样发送值。
猜你喜欢
  • 2020-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
  • 1970-01-01
  • 2020-07-18
  • 1970-01-01
相关资源
最近更新 更多