dtnmnw3697 2013-04-06 16:33
浏览 47
已采纳

随机数组并插入数据库; 在当前之前插入元素

Every time I click submit the name of the element from the array I'm voting on changes when saved into database. The element that shows up after the current echoed element is entered into the database instead of the one I clicked on. Any suggestions on how make the element from the array I click on save into the database? I can't seem to figure it out.

I've tried using lists and unshift still get the same result.

Lets say the echo shows example2 on for the value in the form. I click example2 but example1 gets saved in the database. I'm not sure how to fix this. Thanks for the help.

Here's my code:

The array setup I'm using:

$array = array("example1","example2","example3");
shuffle($array);
foreach($array as $random);

<?php echo array_pop($random);?>

HTML:

   <tbody>
         <tr>
                <form action="Voting_action.php" method="post">
            <td>
                <input type="submit" class="buttontable1" value="<?php echo $random ?>" name="name"/>
            </td>
                </form>
          </tr>
    </tbody>

PHP: Action

$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;
}
  • 写回答

1条回答 默认 最新

  • drpp5680 2013-04-06 16:46
    关注

    your html form sends your random key using name="name" (which is confusing in itself).

    Access that variable via $_POST['name'] not $random.

    Given problems like this in the future, in the first instance always do

    var_dump($_POST);
    

    in your form handler in order to see what variables are available to be used, and what values they carry.

    Ps the way you have used unfiltered and unescaped values in you db query leaves you wide open to sql injection attacks, look into Mysqli's prepared statements to sidestep this issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料