提交数组中的随机字符串,在返回字符串之后提交字符串


Submitting random string from an array, submits the string after echoed string

每次单击提交元素的名称时,我都会对输入数据库时显示在当前回显元素之后的元素的更改进行投票。关于如何解决这个问题,有什么建议吗?我好像想不通。

比方说,echo显示了表单中值的example2。我单击example2,但example1被保存在数据库中。我不知道该怎么解决。谢谢你的帮助。

这是我的代码:

我使用的阵列设置:

$array = array("example1","example2","example3");
$random = $array;
shuffle($random);
<?php echo array_pop($random);?>

PHP:动作

我在表单中使用POST方法。

$mysqli = new mysqli("", "", "", "");
if ($mysqli->connect_error) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_error . ") " . $mysqli->connect_error;
}
if (!$mysqli->query("INSERT INTO table(id, name, votes) VALUES (id, '".$random."', '".$votes."')")) {
    echo "Multi-INSERT failed: (" . $mysqli->errno . ") " . $mysqli->error;
}

为了让你的问题清楚,我相信你想要以下内容:

  • 从数组中拾取一个随机元素
  • 在网页中显示
  • 将其插入数据库

所以,

$array = array("example1","example2","example3");
shuffle($array);
$element = array_pop($array);    
echo $element;

然后在数据库中插入$element

上面是你想要的。您的其余代码没有任何意义。

if (!$stmt->bind_param("s", $id, $votes))

应该是

if (!$stmt->bind_param("ss", $id, $votes)) because the number of strings and variables have to match.