简体   繁体   English

如何从刚提交的表单中回显名称,价格和ID?

[英]How do I echo name, price and id from a form just submitted?

I have an issue with MySQLi and PHP . 我有MySQLi和PHP的问题。

I created a form, and once I type the desired values in and hit submit, the values are right away sent to the database. 我创建了一个表单,一旦在其中输入了所需的值并点击了Submit,这些值就会立即发送到数据库。 Nothing wrong with this. 没错。

What I want to happen is that: after hitting the submit button, PHP shall echo the result of the just-submitted entry. 我要发生的事情是:按下Submit按钮后,PHP将回显刚刚提交的条目的结果。 That is to say: 也就是说:

   `INSERT INTO table VALUES (x, x, y) -> SELECT x, x, y FROM table ORDER BY id DESC LIMIT 1`

I have tried many methods to do this, but all of them either echo the previous entry (the one before the one just submitted) or plainly don't work. 我尝试了许多方法来执行此操作,但是所有这些方法要么都回显了上一个条目(刚提交的条目之前的条目),要么显然不起作用。

I have tried mysqli_insert_id($conn) but this returns nothing. 我已经尝试了mysqli_insert_id($ conn),但是什么也没返回。

This is where my code rests at at the moment: 这是我的代码目前所在的位置:

    $conn = mysqli_connect($server, $user, $pw, $BD);
    if (!$conn) {
        die ('<span style="color: #FF0000;">"connection failed: "</span>' .  mysqli_connect_error());
    }

    $nome = $_POST['nome'];
    $preco = $_POST['preco'];

    $query = "INSERT INTO produtos(nome, preco) VALUES ('$nome', '$preco')";
    $result = mysqli_insert_id($conn);
    var_dump ($result);

    if (mysqli_query($conn, $query)){
        echo '<br>'."Succeeded!";
    } else {
        echo '<br>'."ERROR!" .'<br>'. $query ."<br>". mysqli_error($conn) .'<br><br>'. '<span style="color: #FF0000;">You have to fill all the fields.</span>';
    }

    mysqli_close($conn); 

to note, if of any help, var_dump outputs int(0) at the moment. 需要注意的是,如果有帮助,var_dump此时会输出int(0)。 Thanks in advance. 提前致谢。 I've been struggling like mad with this. 我一直为此疯狂挣扎。

You can't get mysqli_insert_id without executing the query. executing查询就无法获取mysqli_insert_id Better use prepare statement to prevent from sql injection 更好地使用prepare statement来防止sql注入

 $stmt = $conn->prepare("INSERT INTO produtos(nome, preco) VALUES (?,?)");
    $stmt->bind_param('ss', $nome, $preco);
    $stmt->execute();// execute query
    $conn->insert_id;// get last insert id

Please see that you haven't even executed your query. 请注意,您甚至没有执行查询。 On a side note, you should be aware of SQL injections and follow the below pattern: 附带说明一下,您应该了解SQL注入并遵循以下模式:

$nome  = mysqli_real_escape_string($conn, $_POST['nome']);
$preco = mysqli_real_escape_string($conn, $_POST['preco']);

$sql    = "INSERT INTO produtos (nome, preco) VALUES ('".$nome."', '".$preco."')";
$query  = mysqli_query($conn, $sql) or die(mysqli_error($conn));
$result = mysqli_insert_id($conn);

echo $result; // Check your result.

Use this: 用这个:

$query = "INSERT INTO produtos(nome, preco) VALUES ('$nome', '$preco')";
$res=mysqli_query($conn,$query);
$result = mysqli_insert_id($conn);
var_dump ($result);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM